Respawn all unoccupied vehicles
#1

Hey guys, i've been trying to create a few admin commands to learn understanding pawn better,
but i keep running into issues, now i have another one hehehe i wanted to create 2 commands,
One that reloads all the vehicles in the game, and one that reloads all the unoccupied vehicles in the game, however how will i do this, can i create a loop that searches all the vehicle id's and respawns them, and a loop that checks if there occupied or not?

I came up with the following code to respawn all unoccupied vehicles however its giving me an error
Код:
CMD:respawnalluv(playerid,params[])
{
    if(pData[playerid][Admin] < 5) return SendClientMessage(playerid, COLOR_RED, "[Error]:{ffffff} You are not authorized to use this command."); else
    {
		for(new i = 1; i < MAX_VEHICLES; i++) if(!IsVehicleOccupied(i)) SetVehicleToRespawn(i);
	}
	return 1;
}
the stock
Код:
stock IsVehicleOccupied(iVehicleID, iSeatID = 0) {
	foreach(Player, i) if(GetPlayerVehicleID(i) == iVehicleID && GetPlayerVehicleSeat(i)== iSeatID) return 1;
	return 0;
}
local variable "using_deprecated_foreach_syntax" shadows a variable at a preceding level
At the line ;
foreach(Player, i) if(GetPlayerVehicleID(i) == iVehicleID && GetPlayerVehicleSeat(i)== iSeatID) return 1;
Reply
#2

Use this:
pawn Код:
foreach(new i : Player) if(GetPlayerVehicleID(i) == iVehicleID && GetPlayerVehicleSeat(i)== iSeatID) return 1;
Reply
#3

That method is actually rather ineffecient if you're looping through vehicles AND players, you should consider using this method to check if a vehicle is occupied instead:

http://forum.sa-mp.com/showpost.php?...5&postcount=13

The example I post only supports drivers however it can easily be expanded for all passengers as well.
Reply
#4

Vehicle occupancy function:
pawn Код:
IsVehicleOccupied(vehicleid)
{
    foreach (new i : Player)
    {
        if (GetPlayerVehicleID(i) == vehicleid)
            return 1;
    }
    return 0;
}
Respawn all unoccupied vehicles:
pawn Код:
for(new i = 1, j = GetVehiclePoolSize(); i <= j; i++)
{
    if (IsValidVehicle(i))
    {
        if (!IsVehicleOccupied(i))
            SetVehicleToRespawn(i);
    }
}
EDIT: Fixed returns.
Reply
#5

You have your returns in IsVehicleOccupied around the wrong way Gammix.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)