Raised This Month: $51 Target: $400
 12% 

Solved loop entitys


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 11-15-2022 , 21:23   loop entitys
Reply With Quote #1

hi, i wanted to know how can i loop these entitys, i wanted to remove it, and reeplace the setentremove function

here is my actual code
PHP Code:
remove_entsClientWarrior )
{
          switch(
Warrior)
          {
                case 
0:
                {
                             
setEntRemove__int_TransEnts[Client][0] );
                             
setEntRemove__int_TransEnts[Client][1] ); 
                             
setEntRemove__int_TransEnts[Client][2] ); 

                             
// here will go the loop function
                
}
                case 
1:
                {
                             
setEntRemove__int_TransEnts[Client][0] );
                             
setEntRemove__int_TransEnts[Client][4] ); 
                             
setEntRemove__int_TransEnts[Client][3] ); 
                }
                case 
2:
                {
                             
setEntRemove__int_TransEnts[Client][6] ); 
                             
setEntRemove__int_TransEnts[Client][5] );
                             
setEntRemove__int_TransEnts[Client][4] ); 
                             
setEntRemove__int_TransEnts[Client][3] ); 
                }
         }
}
// make a loop function
setEntRemove( &Entity )
{
     if(
is_valid_ent(Entity))
     {
         
remove_entityEntity );
         
Entity 0;
     }


Edit. Solution:

PHP Code:

removeTransEnts
(Client__int_Entitys[] )  
{
        for( new 
0__int_Entitys[i] < 100i++)
        { 
              if(
is_valid_ent(__int_TransEnts[Client][__int_Entitys[i]]))
              {
                    
remove_entity__int_TransEnts[Client][__int_Entitys[i]] );
                    
__int_TransEnts[Client][__int_Entitys[i]] = 0;
                    
client_printClientprint_chat"Entity Removed = [Client][%d]"__int_Entitys[i] );

              }
              
client_printClientprint_chat"Entity Checked = [Client][%d]"__int_Entitys[i] );             
        }
}
remove_entsClientWarrior )
{
          switch(
Warrior)
          {
                case 
0:
                {
                             
removeTransEntsClient, {0,1,2,100} );

                             
// here will go the loop function
                
}
                case 
1:
                {
                             
removeTransEntsClient, {0,4,3,100} );
                }
                case 
2:
                {
                             
removeTransEntsClient, {6,5,4,3,100} );
                }
         }


Last edited by MrPickles; 11-17-2022 at 20:05.
MrPickles is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-16-2022 , 00:16   Re: loop entitys
Reply With Quote #2

I'm not sure how you don't get any index out-of-bounds errors because you're trying to index an array from 0 to 99 where the array is much much smaller than 100 (in your case 4 or 5 depending on which function call you're using).

You can actually determine the size of the provided array by using the sizeof directive as the default for a "length" parameter:

PHP Code:
myFunction(myArray[], len sizeof myArray)
{
    
server_print("Array Size: %d"len)

where

PHP Code:
    myFunction({1,3,5})
    
myFunction({1})
    
myFunction({2,34,3.34,3.0,34.0,34,0.0}) 
results in:

Code:
Array Size: 3
Array Size: 1
Array Size: 7
__________________
fysiks is offline
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 11-16-2022 , 02:04   Re: loop entitys
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
I'm not sure how you don't get any index out-of-bounds errors because you're trying to index an array from 0 to 99 where the array is much much smaller than 100 (in your case 4 or 5 depending on which function call you're using).

You can actually determine the size of the provided array by using the sizeof directive as the default for a "length" parameter:

PHP Code:
myFunction(myArray[], len sizeof myArray)
{
    
server_print("Array Size: %d"len)

where

PHP Code:
    myFunction({1,3,5})
    
myFunction({1})
    
myFunction({2,34,3.34,3.0,34.0,34,0.0}) 
results in:

Code:
Array Size: 3
Array Size: 1
Array Size: 7

nop, u are wrong, the loop doesnt count from 0 to 100, look:

if this is my array:
PHP Code:
{2,4,6,100
the loop do this:

PHP Code:
     // i        i++
Array[0] = // i = 0
Array[1] = // i = 1
Array[2] = // i = 2
Array[3] = 100 // i = 3, oops this cell contain 100, so we can stop 
thats why the condition,
PHP Code:
__int_Entitys[i] < 100 
if it was counting from 0 to 100, it will be
PHP Code:
100 
, but isnt the case, u can check it by yourself with the prints that i setted

check it with this:

PHP Code:
public TestClient )
{
        
removeTransEntsClient, {2,8,15,100} ); 
        return 
1;
}
removeTransEnts(Client__int_Entitys[] )  
{
        for( new 
0__int_Entitys[i] < 100i++)
        { 
               
client_printClientprint_chat"The Cell [%d] have a value of  = %d"i__int_Entitys[i] );          
        }
        
client_printClientprint_chat"End of the Loop" );


Last edited by MrPickles; 11-16-2022 at 02:45.
MrPickles is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 11-16-2022 , 05:48   Re: loop entitys
Reply With Quote #4

Why don't you initiate the array as the following it will be less complex and easier to understand .

PHP Code:

#define MAX_WARRIORS_TYPES      3
#define MAX_ENT_PER_WARRIOR    4

new __int_TransEnts[MAX_PLAYERS+1][MAX_WARRIORS_TYPES][MAX_ENT_PER_WARRIOR]


RemovePlayerTransEntitiesidWarriorType )
{
     for(new 
iMAX_ENT_PER_WARRIORi++)
     {
              if( 
__int_TransEnts[id][WarriorType][i] > )
              {
                       
remove_entity(__int_TransEnts[id][WarriorType][i]);
                       
__int_TransEnts[id][WarriorType][i] = 0;
              }
      }
}
RemovePlayerTransEntitiesByArrayidWarriorType, const Array[] )
{
     for(new 
ijsizeofArray strlen(Array); MAX_ENT_PER_WARRIORi++)
     {
           for(
0sizeofArrayj++)
           {
              if( 
__int_TransEnts[id][WarriorType][i] == Array[j]  )
              {
                       
remove_entity(__int_TransEnts[id][WarriorType][i]);
                       
__int_TransEnts[id][WarriorType][i] = 0;
                       break;
              }
            }
      }

Again it will be much simpler if you explained what are you trying to achieve....
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 11-16-2022 at 14:40.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 11-16-2022 , 13:12   Re: loop entitys
Reply With Quote #5

Quote:
Originally Posted by Natsheh View Post
Why don't you initiate the array as the following it will be less complex and easier to understand .

PHP Code:

#define MAX_WARRIORS_TYPES      3
#define MAX_ENT_PER_WARRIOR    4

new __int_TransEnts[MAX_PLAYERS+1][MAX_WARRIORS_TYPES][MAX_ENT_PER_WARRIOR]


RemovePlayerTransEntitiesidWarriorType )
{
     for(new 
iMAX_ENT_PER_WARRIORi++)
     {
              if( 
__int_TransEnts[id][WarriorType][i] > )
              {
                       
remove_entity(__int_TransEnts[id][WarriorType][i]);
                       
__int_TransEnts[id][WarriorType][i] = 0;
              }
      }
}
RemovePlayerTransEntitiesByArrayidWarriorType, const Array[] )
{
     for(new 
ijsizeofArray sizeof Array; MAX_ENT_PER_WARRIORi++)
     {
           for(
0sizeofArrayj++)
           {
              if( 
__int_TransEnts[id][WarriorType][i] == Array[j]  )
              {
                       
remove_entity(__int_TransEnts[id][WarriorType][i]);
                       
__int_TransEnts[id][WarriorType][i] = 0;
                       break;
              }
            }
      }

Again it will be much simpler if you explained what are you trying to achieve....
First of all, the code you wrote has nothing to do with what I did, second, I already solved it 1000 times simpler than that, I see that in all the posts you don't understand such simple things, and not only mine.

Last edited by MrPickles; 11-16-2022 at 13:15.
MrPickles is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 11-16-2022 , 14:09   Re: loop entitys
Reply With Quote #6

What simpler with this ?

PHP Code:
removeTransEntsClient, {0,1,2,3,100} ); 
You're hardcoding some values in the script which its not needed at all and those numbers have absolute no meaning atleast try to make them readable so people understand what is happening in your code.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 11-16-2022 at 14:12.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 11-16-2022 , 14:23   Re: loop entitys
Reply With Quote #7

Quote:
Originally Posted by Natsheh View Post
What simpler with this ?

PHP Code:
removeTransEntsClient, {0,1,2,3,100} ); 
You're hardcoding some values in the script which its not needed at all and those numbers have absolute no meaning atleast try to make them readable so people understand what is happening in your code.
They are the cells that it will read, 100 is to tell it that I have reached this far, so, end of the loop, too simple

PHP Code:

                             removeTransEnts
Client, {0,1,2,100} );

                             
setEntRemove__int_TransEnts[Client][0] );
                             
setEntRemove__int_TransEnts[Client][1] ); 
                             
setEntRemove__int_TransEnts[Client][2] ); 
other example:

PHP Code:
                             removeTransEntsClient, {6,5,4,3,100} );

                             
setEntRemove__int_TransEnts[Client][6] ); 
                             
setEntRemove__int_TransEnts[Client][5] );
                             
setEntRemove__int_TransEnts[Client][4] ); 
                             
setEntRemove__int_TransEnts[Client][3] ); 
Well if you don't understand it's obvious that it wouldn't make sense

If you still do not understand you should read better, I explain it in detail above
https://forums.alliedmods.net/showpo...00&postcount=3

Last edited by MrPickles; 11-16-2022 at 14:28.
MrPickles is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 11-16-2022 , 14:30   Re: loop entitys
Reply With Quote #8

Why don't you make the loop as following it will be rather than using 100 to stop the loop.

PHP Code:
for( new 0__int_Entitys[i] < 100i++)[ 

PHP Code:
for( new 0maxloop strlen(__int_Entitys) - 1maxloopi++) 
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 11-16-2022 at 14:35.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
MrPickles
Senior Member
Join Date: Aug 2022
Location: Colombia
Old 11-16-2022 , 14:59   Re: loop entitys
Reply With Quote #9

Quote:
Originally Posted by MrPickles View Post
nop, u are wrong, the loop doesnt count from 0 to 100, look:

if this is my array:
PHP Code:
{2,4,6,100
the loop do this:

PHP Code:
     // i        i++
Array[0] = // i = 0
Array[1] = // i = 1
Array[2] = // i = 2
Array[3] = 100 // i = 3, oops this cell contain 100, so we can stop 
thats why the condition,
PHP Code:
__int_Entitys[i] < 100 
if it was counting from 0 to 100, it will be
PHP Code:
100 
, but isnt the case, u can check it by yourself with the prints that i setted

check it with this:

PHP Code:
public TestClient )
{
        
removeTransEntsClient, {2,8,15,100} ); 
        return 
1;
}
removeTransEnts(Client__int_Entitys[] )  
{
        for( new 
0__int_Entitys[i] < 100i++)
        { 
               
client_printClientprint_chat"The Cell [%d] have a value of  = %d"i__int_Entitys[i] );          
        }
        
client_printClientprint_chat"End of the Loop" );

Quote:
Originally Posted by Natsheh View Post
Why don't you make the loop as following it will be rather than using 100 to stop the loop.

PHP Code:
for( new 0__int_Entitys[i] < 100i++)[ 

PHP Code:
for( new 0maxloop strlen(__int_Entitys) - 1maxloopi++) 
it is more simple to set 100, thats all, more operations are saved
in a loop the simpler is better, think that each time you will be checking and obtaining the length of the array (-1), instead with 100, it only checks if it is the cell contain 100

Last edited by MrPickles; 11-16-2022 at 15:08.
MrPickles is offline
Celena Luna
Veteran Member
Join Date: Aug 2013
Location: Nagazora
Old 11-16-2022 , 20:33   Re: loop entitys
Reply With Quote #10

Quote:
Originally Posted by MrPickles View Post
it is more simple to set 100, thats all, more operations are saved
in a loop the simpler is better, think that each time you will be checking and obtaining the length of the array (-1), instead with 100, it only checks if it is the cell contain 100
1. I disagree with this
What if the number of loop is smaller, for example, only 10 is required.
But it have to run 100 times every single time because you hardcode it to 100
Isn't that worse?
Not to mention, you are using a giantic array2D with the size of 3100+ (100 entity x 31 clients) so that would cost much more performen than that small check in for-loop is

2. The anwser code feel really weird to me.
Tell me if I am wrong:
With this as example
PHP Code:
removeTransEntsClient, {0,1,2,100} ); 
You are trying to remove any entity at the index contain inside __int_Entitys[]
=> you want to get this
  • __int_TransEnts[Client][0]
  • __int_TransEnts[Client][1]
  • __int_TransEnts[Client][2]
  • __int_TransEnts[Client][100]

If that the case, what you want to loop is __int_Entitys[]'s size NOT the value insde it.

The code can change it into this
PHP Code:
removeTransEnts(Client__int_Entitys[], max sizeof __int_Entitys)  
{
        for( new 
0maxi++)
        { 
              if(
is_valid_ent(__int_TransEnts[Client][__int_Entitys[i]]))
              {
                    
remove_entity__int_TransEnts[Client][__int_Entitys[i]] );
                    
__int_TransEnts[Client][__int_Entitys[i]] = 0;
                    
client_printClientprint_chat"Entity Removed = [Client][%d]"__int_Entitys[i] );

              }
              
client_printClientprint_chat"Entity Checked = [Client][%d]"__int_Entitys[i] );             
        }

__________________
My plugin:

Last edited by Celena Luna; 11-16-2022 at 20:52.
Celena Luna is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:43.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode