SA-MP Forums Archive
Can we do something like this? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Can we do something like this? (/showthread.php?tid=486535)



Can we do something like this? - newbienoob - 09.01.2014

pawn Код:
new ConnectedPlayers;

public OnPlayerConnect(playerid)
{
    ConnectedPlayers++;
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    ConnectedPlayers--;
    return 1;
}

public OnSomethingHappens(playerid)
{
    for(new a = 0, b = ConnectedPlayers; a < b; a++)
    {
        SendClientMessage(a, -1, "hello");
    }
    return 1;
}



Re: Can we do something like this? - amirab - 09.01.2014

yes why not


Re: Can we do something like this? - Konstantinos - 09.01.2014

But it's not recommended.

Let's say 5 players join and the IDs 0, 1, 2, 3, 4 are taken. Another 5 players join and the IDs 5, 6, 7, 8, 9 are taken as well. The first 5 players joined leave the game and ConnectedPlayers is 5 and the players (connected players) are: 5, 6, 7, 8, 9.

Your loop will go from 0 to 4 and it will NOT go through the players who are connected.

If you want to optimize the loops for the players, then use foreach. It's really powerful and it loops through the online players ONLY!


Re: Can we do something like this? - newbienoob - 09.01.2014

Ahh.. Never thought of that.