#1

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.
Reply
#2

https://sampwiki.blast.hk/wiki/Control_Structures#Loops Seriously? I don't think there's an easier explanation than that
Reply
#3

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)