PDA

View Full Version : Respawn only unused vehicles


iWaYz
24/09/2013, 07:01 PM
Hello everyone, can someone tell me how to respawn only unused vehicles?

Here's my function:

if(IsPlayerConnected(playerid))
{
if(PlayerInfo[playerid][pRank] == 6 && PlayerInfo[playerid][pMember] == TEAM_GROVE)
{
for(new cars=0; cars<19; cars++)
{
if(!VehicleOccupied(cars))
{
SetVehicleToRespawn(grovecars[cars]);
}
}
format(string, sizeof(string), "All vehicles has been respawned by %s(%d)", PlayerName(playerid),playerid);
SendFactionMessage(TEAM_GROVE, COLOR_LIGHTBLUE, string);
}
}

TonyII
24/09/2013, 08:55 PM
Hello everyone, can someone tell me how to respawn only unused vehicles?

Here's my function:

if(IsPlayerConnected(playerid))
{
if(PlayerInfo[playerid][pRank] == 6 && PlayerInfo[playerid][pMember] == TEAM_GROVE)
{
for(new cars=0; cars<19; cars++)
{
if(!VehicleOccupied(cars))
{
SetVehicleToRespawn(grovecars[cars]);
}
}
format(string, sizeof(string), "All vehicles has been respawned by %s(%d)", PlayerName(playerid),playerid);
SendFactionMessage(TEAM_GROVE, COLOR_LIGHTBLUE, string);
}
}





new bool:vehicleused[MAX_VEHICLES];
for(new i=0; i < MAX_PLAYERS; i++)
{
if(IsPlayerConnected(i) && IsPlayerInAnyVehicle(i))
{
vehicleused[GetPlayerVehicleID(i)] = true;
}
}
for(new i=1; i < MAX_VEHICLES; i++)
{
if(!vehicleused[i])
{
SetVehicleToRespawn(i);
}
}

iWaYz
24/09/2013, 09:03 PM
Sorry for my impertinence but can you make further explain. Because I can't understand it. ;(

TonyII
24/09/2013, 09:07 PM
Sure

for(new i=0; i < MAX_PLAYERS; i++)//Here it is looping through all the players
{
if(IsPlayerConnected(i) && IsPlayerInAnyVehicle(i))//It checks if any of the players is in a vehicle, so the bool is set to true for the players those are in any vehicle.
{
vehicleused[GetPlayerVehicleID(i)] = true;
}
}
for(new i=1; i < MAX_VEHICLES; i++)//Loop through the vehicles
{
if(!vehicleused[i])//If the bool is false it will respawn the vehicle, else it stays.
{
SetVehicleToRespawn(i);
}
}

Patrick
24/09/2013, 09:10 PM
I'll use TonyII's code and explain it to you

new bool:vehicleused[MAX_VEHICLES]; //bool variable

for(new i=0; i < MAX_PLAYERS; i++) // loop of MAX_PLAYERS which is equals to 500
{//open bracket
if(IsPlayerConnected(i) && IsPlayerInAnyVehicle(i)) // Checking if there is someone inside the vehicle and if the player is connected
{//open bracket
vehicleused[GetPlayerVehicleID(i)] = true; //making the variable true statement, means someone is inside the vehicle
}//close bracket
}//close bracket

for(new i=1; i < MAX_VEHICLES; i++) // loop of MAX_VEHICLES which is equals to 2000 (I guess)
{//open bracket
if(!vehicleused[i]) // ! = return 0; means if the vehicle isn't occupied by any players
{
SetVehicleToRespawn(i); //Loop of all the vehicles and respawn it | Function: http://wiki.sa-mp.com/wiki/Function:SetVehicleToRespawn
}//close bracket
}//open bracket


Edit: Too Late

TonyII
24/09/2013, 09:18 PM
Your explanation is much better then mine tho, ;)