SA-MP Forums Archive
Vehicle Positions not saving - 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: Vehicle Positions not saving (/showthread.php?tid=428951)



Vehicle Positions not saving - Deal-or-die - 07.04.2013

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.


Re: Vehicle Positions not saving - Pottus - 07.04.2013

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


Re: Vehicle Positions not saving - HurtLocker - 07.04.2013

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.


Re: Vehicle Positions not saving - ReneG - 07.04.2013

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.


Re: Vehicle Positions not saving - Deal-or-die - 08.04.2013

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.


Re: Vehicle Positions not saving - Scenario - 08.04.2013

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.