16.12.2014, 18:45
How can I detect an empty vehicle ? I would like to make a command to delete empty vehicles but I can't find a function, how does someone could help me ?.
CMD:cleanvehicles(playerid, params[])
{
for(new i = 0; i < MAX_PLAYERS; i++)
{
for(new a = 0; a < MAX_VEHICLES; a++)
{
if(!IsPlayerInVehicle(i, a) && i != INVALID_PLAYER_ID && a != INVALID_VEHICLE_ID)
{
SetVehicleToRespawn(a);
SendClientMessageToAll(-1, "Unused vehicles successfully respawned"); // debugging
}
}
}
return 1;
}
CMD:cleanvehicles(playerid, params[])
{
// Setup local variables
new bool:PlayerInsideVehicle = false;
// Loop through all vehicles (first vehicle-id is 1, not 0)
for(new vid = 1; vid < MAX_VEHICLES; vid++)
{
// If this vehicle doesn't have a model, it doesn't exist and the loop continues with the next vehicle, skipping the rest of the code
if (GetVehicleModel(vid) == 0) continue;
// Loop through all players
for(new i = 0; i < MAX_PLAYERS; i++)
{
// Check if this player is connected
if (IsPlayerConnected(i)
{
// If this player is connected, check if he's inside the current vehicle
if(IsPlayerInVehicle(i, vid))
{
// Current vehicle is occupied by at least one player
PlayerInsideVehicle = true;
}
}
}
// If no player is inside this vehicle, respawn it (if there is at least one player inside it, don't do anything)
if (PlayerInsideVehicle == false)
{
SetVehicleToRespawn(vid); // You may also put "DestroyVehicle(vid);" on this line to delete them instead of respawning
}
// Re-initialize the variable and proceed with the next vehicle
PlayerInsideVehicle = false;
}
// Send a message to all players to let them know unoccupied vehicles have been respawned
SendClientMessageToAll(-1, "Unused vehicles successfully respawned");
return 1;
}