AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved loop entitys (https://forums.alliedmods.net/showthread.php?t=340393)

MrPickles 11-15-2022 21:23

loop entitys
 
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} );
                }
         }



fysiks 11-16-2022 00:16

Re: loop entitys
 
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


MrPickles 11-16-2022 02:04

Re: loop entitys
 
Quote:

Originally Posted by fysiks (Post 2792896)
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" );



Natsheh 11-16-2022 05:48

Re: loop entitys
 
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....

MrPickles 11-16-2022 13:12

Re: loop entitys
 
Quote:

Originally Posted by Natsheh (Post 2792914)
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.

Natsheh 11-16-2022 14:09

Re: loop entitys
 
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.

MrPickles 11-16-2022 14:23

Re: loop entitys
 
Quote:

Originally Posted by Natsheh (Post 2792946)
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

Natsheh 11-16-2022 14:30

Re: loop entitys
 
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++)[ 

:arrow:
PHP Code:

for( new 0maxloop strlen(__int_Entitys) - 1maxloopi++) 


MrPickles 11-16-2022 14:59

Re: loop entitys
 
Quote:

Originally Posted by MrPickles (Post 2792900)
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 (Post 2792948)
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++)[ 

:arrow:
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

Celena Luna 11-16-2022 20:33

Re: loop entitys
 
Quote:

Originally Posted by MrPickles (Post 2792952)
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] );             
        }




All times are GMT -4. The time now is 09:28.

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