SA-MP Forums Archive
Respawn all unoccupied vehicles - 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: Respawn all unoccupied vehicles (/showthread.php?tid=613231)



Respawn all unoccupied vehicles - yvoms - 25.07.2016

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;


Re: Respawn all unoccupied vehicles - VVWVV - 25.07.2016

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



Re: Respawn all unoccupied vehicles - Abagail - 26.07.2016

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.


Re: Respawn all unoccupied vehicles - Gammix - 26.07.2016

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.


Re: Respawn all unoccupied vehicles - Threshold - 26.07.2016

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