PDA

View Full Version : Respawn Vehicles?


Xx_OutLawZ_xX
26/11/2011, 08:54 AM
Hello,

Well I have been undergoing vehicles everywhere without respawning, So how do I make them all respawn?
I use CreateVehicleEx and AddStaticVehicleEx.

Thanks

Rob_Maate
26/11/2011, 09:07 AM
for(new i = 0; i < MAX_VEHICLES; i++)
{
if(i != INVALID_VEHICLE_ID)
{
SetVehicleToRespawn(i);
}
}


Just incorporate that into a simple command like /respawnallcars

Kostas'
26/11/2011, 09:15 AM
If you mean by respawning with a command, something like this

CMD:respawncars(playerid, params[])
{
if(IsPlayerConnected(playerid)) {
for(new x = 0; x < MAX_VEHICLES; x++) {
if(IsVehicleEmpty(x)) {
SetVehicleToRespawn(x);
}
}
SendClientMessage(playerid, COLOR_ORANGE, "Vehicles Respawned!");
}
return 1;
}


stock IsVehicleEmpty(vehicleid)
{
if(IsVehicleOccupied[vehicleid])return 0;
else return 1;
}

Rob_Maate
26/11/2011, 09:26 AM
I revised it a little - This will check through each vehicle and then check if any players are in it.
If it finds any, leaves the car alone
If it doesn't, respawns it


CMD:respawncars(playerid, params[])
{
for(new i = 0; i < MAX_VEHICLES; i++)
{
if(i != INVALID_VEHICLE_ID)
{
new IsVehicleEmpty = 0;
for(new j=0; j<MAX_PLAYERS; j++)
{
if(GetPlayerVehicleID(j) != i)
{
IsVehicleEmpty = 1;
}
else
{
IsVehicleEmpty = 0;
}
}
if(IsVehicleEmpty = 0)
{
SetVehicleToRespawn(i);
}
else
{
//If you need to perform an action on a used-car during the respawn, place it here
}
SendClientMessage(playerid, COLOR_ORANGE, "Vehicles Respawned!");
}
}
return 1;
}

Xx_OutLawZ_xX
26/11/2011, 09:39 AM
Ok thankyou but,

warning 219: local variable "IsVehicleEmpty" shadows a variable at a preceding level
warning 211: possibly unintended assignment
warning 204: symbol is assigned a value that is never used: "IsVehicleEmpty"

Rob_Maate
26/11/2011, 09:43 AM
Try that


CMD:respawncars(playerid, params[])
{
for(new i = 0; i < MAX_VEHICLES; i++)
{
if(i != INVALID_VEHICLE_ID)
{
new bool: IsVehicleEmpty;
for(new j=0; j<MAX_PLAYERS; j++)
{
if(GetPlayerVehicleID(j) != i)
{
IsVehicleEmpty = true;
}
else
{
IsVehicleEmpty = false;
}
}
if(IsVehicleEmpty = true)
{
SetVehicleToRespawn(i);
}
else
{
//If you need to perform an action on a used-car during the respawn, place it here
}
SendClientMessage(playerid, COLOR_ORANGE, "Vehicles Respawned!");
}
}
return 1;
}

Xx_OutLawZ_xX
26/11/2011, 09:50 AM
I still get them 3 warnings :S

Kostas'
26/11/2011, 09:55 AM
Did you try my way?

Rob_Maate
26/11/2011, 09:59 AM
@Kostas, you used
if(IsVehicleOccupied[vehicleid])return 0;

How does that variable get it's value?

@Outlawz - Try running it, see if it works... I'll have another look, I don't really see why it's giving warnings at all

Xx_OutLawZ_xX
26/11/2011, 10:01 AM
Through your way, I get errors:


error 028: invalid subscript (not an array or too many subscripts): "IsVehicleOccupied"
warning 215: expression has no effect
error 001: expected token: ";", but found "]"
error 029: invalid expression, assumed zero
fatal error 107: too many error messages on one line

Edit: I was going to say that.

Kostas'
26/11/2011, 10:04 AM
@Kostas, you used
if(IsVehicleOccupied[vehicleid])return 0;

How does that variable get it's value?


I have tested and it works. It respawns vehicles that are empty. If there is player inside nothing happens.

Rob_Maate
26/11/2011, 10:05 AM
I worked something out :D
Possibly, IsVehicleEmpty is already defined in one of your includes.
I changed the name to LocalIsVehicleEmpty
Give this a go - Maybe third time's the charm :c


CMD:respawncars(playerid, params[])
{
for(new i = 0; i < MAX_VEHICLES; i++)
{
if(i != INVALID_VEHICLE_ID)
{
new bool: LocalIsVehicleEmpty;
for(new j=0; j<MAX_PLAYERS; j++)
{
if(GetPlayerVehicleID(j) != i)
{
LocalIsVehicleEmpty = true;
}
else
{
LocalIsVehicleEmpty = false;
}
}
if(LocalIsVehicleEmpty = true)
{
SetVehicleToRespawn(i);
}
else
{
//If you need to perform an action on a used-car during the respawn, place it here
}

}

}
SendClientMessage(playerid, COLOR_ORANGE, "Vehicles Respawned!");
return 1;
}

Rob_Maate
26/11/2011, 10:07 AM
I have tested and it works. It respawns vehicles that are empty. If there is player inside nothing happens.

Head over to your script and search for that variable.
At some point you should come across the code that gives it a value.
It's not a function, it's simply a "storage unit" for a small piece of data. That data has to have come from somewhere.

Kostas'
26/11/2011, 10:09 AM
Don't double post. You can use edit button.
And thats why there is the stock at the script.

Rob_Maate
26/11/2011, 10:13 AM
Thankyou for your smartass reply, It's really assisted in solving this lad's question.
I was conveying two seperate ideas, so I used two different posts.

Your stock means nothing. Hate to be blunt, but your wrong.
You can't just call on a non-existent variable and expect it to return a value

Xx_OutLawZ_xX
26/11/2011, 10:16 AM
@rob
2 warnings left, This is the line if(LocalIsVehicleEmpty = true)

2954) : warning 211: possibly unintended assignment
2954) : warning 204: symbol is assigned a value that is never used: "LocalIsVehicleEmpty"

Kostas'
26/11/2011, 10:18 AM
My bad. :p I forgot the variable
new bool:IsVehicleOccupied[MAX_VEHICLES];


CMD:respawncars(playerid, params[])
{
if(IsPlayerConnected(playerid)) {
for(new x = 0; x < MAX_VEHICLES; x++) {
if(IsVehicleEmpty(x)) {
SetVehicleToRespawn(x);
}
}
SendClientMessage(playerid, COLOR_ORANGE, "Vehicles Respawned!");
}
return 1;
}


stock IsVehicleEmpty(vehicleid)
{
if(IsVehicleOccupied[vehicleid])return 0;
else return 1;
}

Rob_Maate
26/11/2011, 10:19 AM
OH LMAO IM SO STUPID.

Replace:
if(LocalIsVehicleEmpty = true)

With:
if(LocalIsVehicleEmpty == true)

The double equal signs LMAO


EDIT: so I don't get barked at again

@Kostas, you still forgot the part that actually gives your variable a value.
There needs to be a function that runs to set each item in that array with a boolean value

Xx_OutLawZ_xX
26/11/2011, 10:25 AM
Thank you so much! +rep

One more thing, If I didn't want to use a command for this, Should I just set a timer for the vehicles to respawn? or... What do you suggest?

Rob_Maate
26/11/2011, 10:28 AM
Well, There's a million ways of doing this... It really depends on what sort of gamemode your running.

If it's a DM gm, then yeah, have a timer along with a command.
If it's a RP gm, you'd wanna think carefully about having timers, often it wrecks a person's RP to have their vehicle dissapear ;D

Xx_OutLawZ_xX
26/11/2011, 10:30 AM
Ok thanks, And yeah I am running a freeroam server :P

Rob_Maate
26/11/2011, 10:32 AM
Ya nice, defs make a timer then.
Only thing you'd wanna think about is making a small warning system:

Say you were gonna run your respawn timer every 60 minutes, you would want another timer that also occurs every 60 minutes, but with a 2~3 minute offset, basically stating that a respawn is about to occur.