SA-MP Forums Archive
About using 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: About using loops (/showthread.php?tid=226538)



About using loops - BuLLeT[LTU] - 15.02.2011

Hello. I wanted to as about using loop in this situation:

pawn Код:
public OnPlayerPickUpPickup(playerid,pickupid)
{
     Loop(i,150)
     {
          if(pickupid == Pickup[i])
          {
               code..
          }
     }
     Loop(i,76)
     {
          if(pickupid == EntrancePickup[i])
          {
               code..
          }
     }
     return 1;
}
Will it make a lag to players ?
is it better to use
if(pickupid == Pickip[0]) till Pickup[149] ?
o use switch(pickupid) ?
But it will take a lot of space as a code..


Re: About using loops - Krx17 - 15.02.2011

No. It won't cause 'lag.' Using a loop is the best way of doing it.


AW: About using loops - Nero_3D - 15.02.2011

Quote:
Originally Posted by BuLLeT[LTU]
Посмотреть сообщение
if(pickupid == Pickip[0]) till Pickup[149] ?
Checking for each item would be slightly faster but you would need lot more text which would make your file unused big

A switch wont work, switch only work with constant values

Just use a loop like you already did and add a return 1 in it because an item can only be found once
Or add a break (which stops the loop) if some other code should be executed in the callback

pawn Код:
public OnPlayerPickUpPickup(playerid,pickupid)
{
    Loop(i, sizeof Pickup)
    {
        if(pickupid == Pickup[i])
        {
            //code
            return 1;
        }
    }
    Loop(i, sizeof EntrancePickup)
    {
        if(pickupid == EntrancePickup[i])
        {
            //code
            return 1;
        }
    }
    return 1;
}



Re: About using loops - BuLLeT[LTU] - 16.02.2011

I was thinking that i need to use return 1;

Thanks for answers