AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   [Help] "FOR" Cycle. (https://forums.alliedmods.net/showthread.php?t=82527)

Xenon 12-24-2008 22:44

[Help] "FOR" Cycle.
 
Hi i've seen a lot of plugins wich s contains this code:
PHP Code:

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

,and i dont know what really means,example.

PHP Code:

new players[32], num
    get_players
(playersnum)
    new 
i
    
for (i=0;i<numi++)
    {
         
cs_set_user_money(players[i], 16000)
    } 

i know that code gives 16.000$ to all players,but i dont know what "for (i=0;i<num; i++)" means,thank you.

PD: Sorry for my bad english.
PD2: That code ,its made by BAILOPAN.Thanks you.

Exolent[jNr] 12-24-2008 23:21

Re: [Help] "FOR" Cycle.
 
http://wiki.amxmodx.org/Pawn_Tutorial#For_Loops

Xenon 12-24-2008 23:37

Another question :)
 
Thank you,i dont really understand all about FOR Cycle still,but i have other problem im trying to make a plugin wich s in eventstartround,force to all the
players to put on Counter-Terrorist Team,and with a "set_task", on 5 seconds,one player will be selected randomized and will be forced to Terrorist Team,this Randomized terrorist player,will have special habilities,thank you. :)

anakin_cstrike 12-25-2008 12:53

Re: [Help] "FOR" Cycle.
 
FOR - repetitive function with known number of steps.
General form:
PHP Code:

for( inittestincrement ); 

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 10;

while( 
<= 10 )
{
    
i;
    
// s += i;
    
    
i++;


For:
PHP Code:

new 0;

for( new 
1<= 10i++)
    
+= i

Both examples are storing int variabile 's' the sum from 1 to 10.

Or a countdown:
PHP Code:

// 1. While
new 10;

while(
0)
{
    
server_print("x = %i"x);
    
x--;
}

//2. For
for( 100x-- )
    
server_print("x = "x); 

In your example above:
PHP Code:

new players32 ], numi;
get_playersplayersnum );

for( 
0numi++ )
    
cs_set_user_moneyplayers], 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_Special33 ];
new 
index32 ], playersidi;
new 
g_maxplayersg_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 gplayers32 ], playernumj;
    
get_playersgplayersnum"h" ); // h -  skip HLTV
    
    
for( 0numj++ )
    {
        
player gplayers];
        
        if( 
is_user_connectedplayer ) )
        {
            
indexplayers ] = player;
            
players++;
        }
        
        if( 
get_user_teamplayer ) != )
            continue;
        
        
fm_set_user_teamplayerFM_TEAM_CT );
        
g_Specialplayer ] = false;
    }
    
    
set_task5.0"choose" );
}

public 
choose()
{
    
id indexrandom_num0players ) ];
    
    if( 
get_user_teamid ) == )
    {
        
fm_set_user_teamidFM_TEAM_T );
        
g_Specialid ] = true;
        
        
client_printidprint_chat"You're the special guy!" );
    }
}

fm_set_user_teamindexteam 
{
    
set_pdata_intindex OFFSET_TEAMteam );

    static const 
TeamInfoFM_TEAM_MAX ][ ] =
    {
        
"UNASSIGNED",
        
"TERRORIST",
        
"CT",
        
"SPECTATOR"
    
};

    
message_beginMSG_ALLg_msgteaminfo );
    
write_byteindex );
    
write_stringTeamInfoteam ] );
    
message_end();



Xenon 12-25-2008 18:16

Re: [Help] "FOR" Cycle.
 
Thank you very much !,im understanding,your code works perfect but, i play with Bots for test plugins,and when the round s starts,everybody goes to CT team and one stays in Terrorist team,then when the public function CHOOSE its activated with the set_task in 5.0 seconds,one CT goes to TT,and then
in terrorist team instead be 1 terrorist there are 2 terrorists,and i dont know why but sometimes the TERRORIST transfered to COUNTER-TERRORIST,gots TERRORIST MODEL,instead CT MODEL,and sometimes too all the CTs shoot his own team CT VS CT because after the team change,they were terroist i think that is because they are bots and when the round starts they got the teammates names and enemies names.Im trying to do ,on startround all the players go to CT team and with the set task,one of them transfer to TT,each round each player TT most be transfered to CT,and one new is selected and transfered to TT team,thank very much i appreciate your work thank you very much thank u thank u thank u.Sorry for my bad English,


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

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