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++ )Code:
for( new i = 0; i < playerCount; i++ ) |
Re: for loop question.
Could you show us how you obtained the value of playerCount and the first line of your loop?
|
Re: for loop question.
This is the original stock:
Code:
stock update_alive_count( )Code:
stock update_alive_count( ) |
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.
|
Re: for loop question.
Code:
|
Re: for loop question.
Thanks Alucard and Wrecked. i understand how it works now.
|
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++ ) |
Re: for loop question.
Thanks bugsy, very usefull explanation
|
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. |
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.