Vehicle Positions not saving
#1

Hey there!
Does anyone have any idea at all as to why my vehicles positions aren't saving? Basically they save to where they were spawned so if the server crashes or restarts the vehicles no matter how far driven away they always spawn where they were originally spawned. Not where they were left.

pawn Code:
public OnGameModeInit()
{
    LoadVehicles();
pawn Code:
public OnGameModeExit()
{
    SaveVehicles(0);
pawn Code:
stock SaveVehicles(pos)
{
    foreach(Vehicles, i)
    {
        SaveVehicle(i,pos);
    }
}
pawn Code:
stock SaveVehicle(i,pos)
{
    new id = GetCar(i,1);
    if(pos)
    {
        GetVehiclePos(i, Vehicle[id][vPositionX], Vehicle[id][vPositionY], Vehicle[id][vPositionZ]);
        GetVehicleZAngle(i, Vehicle[id][vAngleZ]);
    }
    new model = GetVehicleModel(i);
    format(query, sizeof(query), "UPDATE `Vehicles` SET `VehicleID` = '%d', `ModelID` = '%d', `PositionX` = '%f', `PositionY` = '%f', `PositionZ` = '%f', `AngleZ` = '%f', `Color1` = '%d', `Color2` = '%d' WHERE `ID` = '%d'",i,model,Vehicle[id][vPositionX],Vehicle[id][vPositionY],Vehicle[id][vPositionZ],Vehicle[id][vAngleZ],Vehicle[id][vColor1],Vehicle[id][vColor2], id);
    mysql_query(query);
    format(query,sizeof(query), "UPDATE `Vehicles` SET `Faction` = '%d', `SubFaction` = '%d', `Plate` = '%s' WHERE `ID` = '%d'",Vehicle[id][vFaction],Vehicle[id][vSubFaction],Vehicle[id][vPlate],id);
    mysql_query(query);
    return 1;
}

All help is greatly appreciated.
Cheers.
Reply
#2

I'm guessing your using AddStaticVehicle() is LoadVehicles(); after or before you add vehicles ?
Reply
#3

Try replacing "foreach" with "for". I had some issues with foreach and when i replaced it with "for" all were fine. Dont forget that foreach actually replaces (for+IsPlayerConnected) and you want to make a loop on vehicles, not players.
Reply
#4

Pass a 1, not a 0 in your SaveVehicles function. Look at the code, you could have figured that out yourself. Also, adding to the post above, foreach doesn't come with a vehicle iterator.
Reply
#5

Quote:
Originally Posted by [uL]Pottus
View Post
I'm guessing your using AddStaticVehicle() is LoadVehicles(); after or before you add vehicles ?
Quote:
Originally Posted by HurtLocker
View Post
Try replacing "foreach" with "for". I had some issues with foreach and when i replaced it with "for" all were fine. Dont forget that foreach actually replaces (for+IsPlayerConnected) and you want to make a loop on vehicles, not players.
Quote:
Originally Posted by VincentDunn
View Post
Pass a 1, not a 0 in your SaveVehicles function. Look at the code, you could have figured that out yourself. Also, adding to the post above, foreach doesn't come with a vehicle iterator.
Thank you all very much for your help. But thanks to you Vincent, the problem was solved. Not sure why I didn't pick up on that myself.

(With 'foreach' are you suggesting that I should just change that to 'for'?)


Thank you once again.
Reply
#6

No, you should use foreach. However, you'll need to make your own foreach iterator for vehicles. A good way to do this is to hook CreateVehicle and DestroyVehicle.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)