[Help] Simple LITTLE question.
#1

Hey,
I'm new at pawno scripting and still learning it .

I've seen this many times and i still don't know what it means :

Код:
for(new i = 0; i < MAX_PLAYERS; i++)
Could anyone please explain me what does it exactly mean and what is it used for? i'm pretty sure it's something i must know

Thanks in advance .
Reply
#2

This code: ( example )
pawn Код:
for( new i; i < MAX_PLAYERS; i ++ )
{
    if( IsPlayerConnected( i ) ) Kick( i );
}
is a replacement for
pawn Код:
if( IsPlayerConnected( 0 ) ) Kick( 0 );
if( IsPlayerConnected( 1 ) ) Kick( 1 );
if( IsPlayerConnected( 2 ) ) Kick( 2 );
if( IsPlayerConnected( 3 ) ) Kick( 3 );
//etc
if( IsPlayerConnected( 499 ) ) Kick( 499 );
Reply
#3

Quote:
Originally Posted by costel_nistor96
Посмотреть сообщение
This code: ( example )
pawn Код:
for( new i; i < MAX_PLAYERS; i ++ )
{
    if( IsPlayerConnected( i ) ) Kick( i );
}
is a replacement for
pawn Код:
if( IsPlayerConnected( 0 ) ) Kick( 0 );
if( IsPlayerConnected( 1 ) ) Kick( 1 );
if( IsPlayerConnected( 2 ) ) Kick( 2 );
if( IsPlayerConnected( 3 ) ) Kick( 3 );
//etc
if( IsPlayerConnected( 499 ) ) Kick( 499 );


I was trying to script an /a CMD for admin chat that when you excute it , it sends the text to all online admins , only admins .

This is what i got so far , not sure if this works but let's give it a shot .
pawn Код:
CMD:a(playerid, o[])
{
new aname[MAX_PLAYER_NAME],string[128];
if(PlayerInfo[playerid][pAdmin]<1) return SendClientMessage(playerid, -1, "You are not authorized to use this command!");
if(isnull(o)) return SendClientMessage(playerid, -1, "Usage: /a(dmin) [text]");
else
{
GetPlayerName(playerid, aname, sizeof(aname));

for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
if(PlayerInfo[i][pAdmin]>=1)
{
format(string, sizeof(string),"Admin %s: %s", aname, o);
SendClientMessage(PlayerInfo[i][pAdmin]>=1, -1, string);
SendClientMessage(playerid, -1, string);
}
}
}
}
return 1;
}
I'm not sure if i scripted it the right way using the "for(new i = 0; i < MAX_PLAYERS; i++)" , i don't get any error or warning after compling aswell , if something is wrong , few tips could be helpful , thanks .
Reply
#4

just put this
pawn Код:
SendClientMessage(i, -1, string);
Reply
#5

The for statement in basic terms is a loop. It has a initialisation, comparison and update.

pawn Код:
for( new variable = 0; variable != 2; variable++)
/*Putting this into word terms. Firstly we define a new variable. If the variable does not equal to two then we add another loop to it. Everything in the for loop is called how large the comparison is. This will go through the loop 3 times (0, 1, 2).
The following will print 0, 1, 2, 3.

pawn Код:
for( new variable; variable < 3; variable ++) printf("%d", variable);
So pretty much, the players loop just loops through every single player. This is useful to find if a player has a certain variable that we want to find.
Reply
#6

I'm using this:
pawn Код:
stock SendMessageToAdmins(color, string[])
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i) && PlayerInfo[i][Adminlevel] > 0) // Own vars.
        {
            SendClientMessage(i, color, string);
        }
    }
    return 1;
}
Then you can use:
pawn Код:
SendMessageToAdmins( ...
Reply
#7

Quote:
Originally Posted by moadi
Посмотреть сообщение
Hey,

Код:
for(new i = 0; i < MAX_PLAYERS; i++)
Thanks in advance .
This is a 'For Loop' where ' i ' is your starting value , MAX_PLAYERS is till where u want ur loop to run.. that means.. if max players is 10... this loop will run 10 times.. 0 to 10 .. and value of i changes by 1 because of ' i++ ' which is shorter form of i = i+1

eg:-

ur loop has started NOW

i = 0
it checks if 0< Max players.. if true
then i++

now i = 1
check again.. if true
i++

in short this loop will go on till 'i' = Max Players

u can use this at many places in pawno like if you want check for player being admin u run this loop.. since i<maxplayers.. Ids of players start from 0 will go on till Maxplayers.. that is total players on ur server..

for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerAdmin(i)) SendClientMessage(blhablha);
}

i = 0 goes to id 0 checks if hes admin.. answer no but i is < maxplayers is true so i++
i=1 goest to id 1 checks if hes admin.. answer yes,, send client message , i < maxplayers is true so i++

in short this is a 'LOOP'
i = starting point
i<'x' = limit of loop
i++ = increase counter
Reply
#8

Quote:
Originally Posted by moadi
Посмотреть сообщение
Hey,
I'm new at pawno scripting and still learning it .

I've seen this many times and i still don't know what it means :

Код:
for(new i = 0; i < MAX_PLAYERS; i++)
Could anyone please explain me what does it exactly mean and what is it used for? i'm pretty sure it's something i must know

Thanks in advance .
Imagine that. Your MAX_PLAYERS define variabile it's setted to 500. You have to check each player to do something for it. To check if a player it's on server, you must use the IsPlayerConnected function.
Let's say you have 4 players connected:
Player ID 0: Jack
Player ID 1: Dean
Player ID 2: Paul
Player ID 3: Sam
The rest of slots are empty slot. So, to don't check each player in 500 lines, like:
PHP код:
if(IsPlayerConnected(0)) /* Do code */
if(IsPlayerConnected(1)) /* Do code */
if(IsPlayerConnected(2)) /* Do code */
if(IsPlayerConnected(3)) /* Do code */
if(IsPlayerConnected(4)) /* Do code */
/*..................................*/
if(IsPlayerConnected(99)) /* Do code */
if(IsPlayerConnected(100)) /* Do code */
/*..................................*/
if(IsPlayerConnected(300)) /* Do code */
if(IsPlayerConnected(301)) /* Do code */
/*..................................*/
if(IsPlayerConnected(499)) /* Do code */
if(IsPlayerConnected(500)) /* Do code */ 
You can do that with for loop:
PHP код:
for(new 0MAX_PLAYERSi++)
    if(
IsPlayerConnected(i))
        
/* Do code */ 
First, he checks the first index, = 0. After that, the for loop checks the next index, by incrementing variabile i to 1 ( i++ = (i = i + 1) = (i += 1) ). When i = MAX_PLAYERS, the for loop is break.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)