random from loop
#1

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

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
Reply
#3

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

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


Forum Jump:


Users browsing this thread: 1 Guest(s)