SA-MP Forums Archive
random from loop - 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: random from loop (/showthread.php?tid=608052)



random from loop - TwinkiDaBoss - 27.05.2016

alright so this might be a bit complex

I want of the player to spectate player that is alive (well his vehicle) which I got working. Now my main problem is, how do I randomize it.

For example: players alive ID's are 1,2,3,4. If I use my loop, its just gonna spectate first one it finds, I want of it to spectate random alive player, not first in line

This is a working code, I just need to get random from all those results or something... I though about making an array and storing all ID's in it and then just randomEx to get the result but Im not sure how it should work basically because I have to switch some ID's etc.
PHP код:
    new savedid;
    for(new 
0MAX_PLAYERSi++) {
        if(
IsPlayerConnected(i)) {
            if(
IsPlayerInAnyVehicle(i)) {
                if(
playerid != i) {
                    
savedid i;
                }
            }
        }
    }
    
PlayerSpectateVehicle(playeridGetPlayerVehicleID(savedid)); 
So my main problem is, how do I randomize it?
Simple randomEx wouldn't work this way simply because some of the ID's wouldnt be alive etc


Re: random from loop - Kaliber - 27.05.2016

Here you go:

PHP код:
new player[MAX_PLAYERS-1],tmp;
for(new 
i=GetPlayerPoolSize(); i!=-1i--)
{
    if(
IsPlayerConnected(i) && i!=playerid && IsPlayerInAnyVehicle(i)) player[tmp++] = i;
}
PlayerSpectateVehicle(playeridGetPlayerVehicleID(player[random(tmp)])); 
Greekz


Re: random from loop - Vince - 27.05.2016

I would suggest using foreach and using custom iterator as that will make it a lot easier. You don't have to install the entire YSI package to use foreach, either.


Re: random from loop - TwinkiDaBoss - 27.05.2016

Yeah, thanks a lot. I didnt know I could just do it like that. Thanks brothers!