FOR - repetitive function with known number of steps.
General form:
PHP Code:
for( init; test; increment );
1. 'init' is evaluated
2. 'test' is evaluated
3. 'increment' is evaluated
4. If 'test' is true, the function goes to nr 2, 'test'.
For instance, there are equal:
While:
PHP Code:
new i = 1, s = 0;
while( i <= 10 )
{
s = s + i;
// s += i;
i++;
}
For:
PHP Code:
new s = 0;
for( new i = 1; i <= 10; i++)
s += i;
Both examples are storing int variabile 's' the sum from 1 to 10.
Or a countdown:
PHP Code:
// 1. While
new x = 10;
while(x > 0)
{
server_print("x = %i", x);
x--;
}
//2. For
for( x = 10; x > 0; x-- )
server_print("x = ", x);
In your example above:
PHP Code:
new players[ 32 ], num, i;
get_players( players, num );
for( i = 0; i < num; i++ )
cs_set_user_money( players[ i ], 16000);
The loop/cycle stars from 0 to num- 1...because is an array, wich starts from 0 (first slot) to last_slot - 1. If you use >= last_slot, you'll get 'index out bounds'.
So, it takes every player and sets his cash to 16000.
I hope you understood a bit.
Know, your request.
PHP Code:
#include <amxmodx>
#include <fakemeta>
#define OFFSET_TEAM 114
enum
{
FM_TEAM_UNASSIGNED,
FM_TEAM_T,
FM_TEAM_CT,
FM_TEAM_SPECTATOR,
FM_TEAM_MAX
};
new bool:g_Special[ 33 ];
new index[ 32 ], players, id, i;
new g_maxplayers, g_msgteaminfo;
public plugin_init()
{
register_logevent( "roundstart", 2, "1=Round_Start" );
g_msgteaminfo = get_user_msgid( "TeamInfo" );
g_maxplayers = get_maxplayers();
}
public roundstart()
{
/* the loop stars from 1, not an array
for(i = 1; i <= g_maxplayers; i++)
{
if( is_user_connected( i ) )
{
index[ players ] = i;
players++;
}
if( get_user_team( i ) != 1 )
continue;
fm_set_user_team( i, FM_TEAM_CT );
g_Special[ i ] = false;
}*/
new gplayers[ 32 ], player, num, j;
get_players( gplayers, num, "h" ); // h - skip HLTV
for( j = 0; j < num; j++ )
{
player = gplayers[ j ];
if( is_user_connected( player ) )
{
index[ players ] = player;
players++;
}
if( get_user_team( player ) != 1 )
continue;
fm_set_user_team( player, FM_TEAM_CT );
g_Special[ player ] = false;
}
set_task( 5.0, "choose" );
}
public choose()
{
id = index[ random_num( 0, players - 1 ) ];
if( get_user_team( id ) == 2 )
{
fm_set_user_team( id, FM_TEAM_T );
g_Special[ id ] = true;
client_print( id, print_chat, "You're the special guy!" );
}
}
fm_set_user_team( index, team )
{
set_pdata_int( index , OFFSET_TEAM, team );
static const TeamInfo[ FM_TEAM_MAX ][ ] =
{
"UNASSIGNED",
"TERRORIST",
"CT",
"SPECTATOR"
};
message_begin( MSG_ALL, g_msgteaminfo );
write_byte( index );
write_string( TeamInfo[ team ] );
message_end();
}
__________________