SA-MP Forums Archive
put player in random car - 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: put player in random car (/showthread.php?tid=281045)



put player in random car - SpankMe2 - 04.09.2011

hey,

is there a easyerway to put a player in a random vehicle than doing case 1: createvehicle.. etc

this is for a minigame so it can only include minigame cars

also is there a easyerway than using a varible and a timer to check if he is the only player?


Re: put player in random car - =WoR=Varth - 04.09.2011

pawn Код:
new Float:Pos[3];
for(new i;i<MAX_VEHICLES;i++)
{
    GetVehiclePos(random(MAX_VEHICLES + 1),Pos[0],Pos[1],Pos[2]);
    if(Pos[0] == 0 && Pos[1] == 0 && Pos[2] == 0) continue;
    PutPlayerInVehicle(playerid,i);
    return 1;
}



Re: put player in random car - Phanto90 - 04.09.2011

Why a for? Isn't better a while?
Take the case (also if a bit impossibile) all the random take an invalid car id.
Also the code has no sense... why getting a pos of a vehicle that isn't "i" and then teleporting to "i"?
Код:
new Float:XPos, Float:YPos, Float:ZPos, bool:a, veh_id;  //Don't use arrays, if it's not strictly necessary
while (!a)
{
    veh_id = random(MAX_VEHICLES + 1);
    GetVehiclePos(veh_id),XPos, YPos, ZPos);
    if(XPos != 0 && YPos != 0 && ZPos != 0) 
         {
         PutPlayerInVehicle(playerid,veh_id);
         a = true;
         }
    return 1;
}



Re: put player in random car - =WoR=Varth - 04.09.2011

Miss code there.

Your code will fail if there's no vehicle in server.

pawn Код:
new Float:Pos[3],a,b[MAX_VEHICLES];
for(new i;i<MAX_VEHICLES;i++)
{
    a = random(MAX_VEHICLES + 1);
    for(new o;o<MAX_VEHICLES;o++)
    {
        if(b[o] == a)
        {
            i--;
            continue;
        }
    }
    b[i] = a;
    GetVehiclePos(a,Pos[0],Pos[1],Pos[2]);
    if(Pos[0] == 0 && Pos[1] == 0 && Pos[2] == 0) continue;
    PutPlayerInVehicle(playerid,a);
    return 1;
}