Loops - 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: Loops (
/showthread.php?tid=425744)
Loops -
Syntax - 26.03.2013
Sorry I am bothering you, but although I have read a lot of tutorials so far (from wiki), none of them teached me well. So, I would you like to tell me what these loops actually represent and when they are used. I would really appreciate that.
Also, I would love to see some examples.
Re: Loops -
Misiur - 26.03.2013
https://sampwiki.blast.hk/wiki/Control_Structures#Loops Seriously? I don't think there's an easier explanation than that
Re: Loops -
Jeffry - 26.03.2013
Loops are used when you want to do the same thing over and over again, for example showing a message to all players in one virtual world. This would look like this:
pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i) && GetPlayerVirtualWorld(i) == 1337)
{
SendClientMessage(i, 0xFF00FFFF, "Message shown to all players in virtual world 1337.");
}
}
For sure you can add more things there.
i represents playerid, but as we prefer using i in loops, we also use it here.
Another example could be, that you want to check if a player is at a specific place, and if that's the case and the player is not RCON admin, then kill the player:
pawn Код:
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i))
{
if(IsPlayerInRangeOfPoint(i, 15.0, 1234.0, 1234.0, 10.0) && !IsPlayerAdmin(i))
{
SendClientMessage(i, 0xFF00FFFF, "Only Admins are allowed to come here.");
SetPlayerHealth(i, 0.0);
}
}
}
For sure you can do a lot more with loops, those are just two easy examples.