AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   for loop question. (https://forums.alliedmods.net/showthread.php?t=132319)

drekes 07-13-2010 22:29

for loop question.
 
I thought these 2 loops do the same thing. But the first one doesn't work like it should and the second one works fine. Could someone explain this?

1
Code:

for( new i = 1; i <= playerCount; i++ )
2
Code:

for( new i = 0; i < playerCount; i++ )

wrecked_ 07-13-2010 22:49

Re: for loop question.
 
Could you show us how you obtained the value of playerCount and the first line of your loop?

drekes 07-13-2010 22:51

Re: for loop question.
 
This is the original stock:
Code:

stock update_alive_count( )
{
    g_iTerroristCount = 0;
    g_iCTCount = 0;
   
    new Players[32], playerCount, id;
    get_players( Players, playerCount );
   
    for( new i = 1; i <= playerCount; i++ )
    {
        id = Players[i];
       
        if( !is_user_alive( id ) )
            continue;
       
        switch( cs_get_user_team( id ) )
        {
            case CS_TEAM_T:
            {
                g_iTerroristCount++;
            }
            case CS_TEAM_CT:
            {
                g_iCTCount++;
            }
        }
    }
}

But because that one wasn't working i made the second loop and then it worked. So i added some client_print's to see what's wrong and ended up with this.
Code:

stock update_alive_count( )
{
    g_iTerroristCount = 0;
    g_iCTCount = 0;
   
    new Players[32], playerCount, id;
    get_players( Players, playerCount );
   
    for( new i = 1; i <= playerCount; i++ )
    {
        id = Players[i];
       
        if( !is_user_alive( id ) )
            continue;
       
        switch( cs_get_user_team( id ) )
        {
            case CS_TEAM_T:
            {
                g_iTerroristCount++;
            }
            case CS_TEAM_CT:
            {
                g_iCTCount++;
            }
        }
    }
    client_print(0, print_chat, "loop 1 %i ct and %i terro", g_iCTCount, g_iTerroristCount);
   
    for( new i = 0; i < playerCount; i++ )
    {
        id = Players[i];
       
        if( !is_user_alive( id ) )
            continue;
       
        switch( cs_get_user_team( id ) )
        {
            case CS_TEAM_T:
            {
                g_iTerroristCount++;
            }
            case CS_TEAM_CT:
            {
                g_iCTCount++;
            }
        }
    }
    client_print(0, print_chat, "loop 2 %i ct and %i terro", g_iCTCount, g_iTerroristCount);
}


wrecked_ 07-13-2010 22:53

Re: for loop question.
 
You're setting i to 1. That means you're indexing the array with 1 first, and array indices begin at 0. This means you're skipping the first cell in Players[]. So, in short, you're skipping a player in the first method.

Alucard^ 07-13-2010 23:47

Re: for loop question.
 
Code:
for( new i = 1; i <= playerCount; i++ )     // if playercount is 32 for example... the reuslt is // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10... 32, when "i" reach // to 33, the loop stop for( new i = 0; i < playerCount; i++ )     // if playercount is 32 for example... the result is // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10... 31, when "i" reach // to 32, the loop stop

drekes 07-13-2010 23:52

Re: for loop question.
 
Thanks Alucard and Wrecked. i understand how it works now.

Bugsy 07-14-2010 00:15

Re: for loop question.
 
There are two methods commonly used to iterate through player indexes and the loops used for each are different;this may be what is confusing you. The two methods use the exact for-loops that you posted above.

1. Iterate through all valid player indexes (1 to maxplayers() [max of 32])
for ( i = 1 ; i <= maxplayers ; i++ )

2. Iterate through player indexes retrieved with get_players()
When iterating through player indexes retrieved with get_players, a loop from 0 to Num-1 is done (Num being the # of players retrieved with get_players()). This is because get_players() populates an array with player indexes and the array elements are accessed starting with 0 to num-1.
for ( i = 0 ; i < num ; i++ )

drekes 07-14-2010 00:45

Re: for loop question.
 
Thanks bugsy, very usefull explanation

fysiks 07-14-2010 00:48

Re: for loop question.
 
To generalize what Bugsy said, the way you set up you iterator ("i" in this case) will be determined by how you use it. The majority of the time if you are going to use it directly in an array you will start with i = 0 because in Pawn all arrays start at index zero.

So, you can start at i = 5 if you want to but, again, that depends on what you are going to use i for.

drekes 07-14-2010 00:50

Re: for loop question.
 
Thanks Fysiks. i see what was wrong with it.


All times are GMT -4. The time now is 07:05.

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