MySQL Variable/Enum is using cached data instead of updating
#1

I'm trying to create a vehicle park/retrieve system. Basically, I am trying to accomplish these few things:
  • The player can have only one of their vehicles spawned at a time.
  • When you /park the vehicle, it'll update it's position and such in the database and then despawn it.
  • The player can only spawn a vehicle that they own(it's saved in the database)
  • They spawn the vehicle based on it's ID in the database.
Now, I've got all of these working, but I've encountered a bug with either /park'ing the vehicle or /vget'ing the vehicle. Here's the scenario. You join the server and /vget carid(replace carid with one of the player's owned vehicles that's obtained with /listcars. which displays the ID of the vehicle in the database). When you do this command, it spawns the vehicle with that specified ID in it's proper location as long as it's owned by the player. This works. When you /park the vehicle while inside, it saves the position and all of it's modifications and then it destroys the vehicle. Now, once I spawn a different vehicle of mine it spawns in the position that I parked it originally. Yet, once I park the new vehicle, it says that I have parked the previous vehicle I parked. It also updates the position and such of the old vehicle instead of the new vehicle. It does destroy the vehicle I just tried to park though. I'm very confused as to why it's continuing to cache the first vehicle I park and updating it's stats instead of the vehicle I'm currently in. And finally, here's the code:

Here's the /vget command:
PHP код:
CMD:vget(playeridparams[])
{
    new 
carid;
    if (
sscanf(params"d"carid))
        return 
SendSyntaxMessage(playerid"/vget [vehicle id]");
    if ((
carid != -1))
    {
        for (new 
0MAX_DYNAMIC_CARS++)
        {
            if(
CarData[i][carID] == carid && CarData[i][carOwner] == PlayerData[playerid][pID] && CarData[i][carExists] == 1)
            {
                
Car_Spawn(i);
                
currentCarID[playerid] = carid;
                
SendServerMessage(playerid"You have successfully spawned your %s."ReturnVehicleModelName(CarData[i][carModel]));
                break;
            }
        }
    }
    return 
1;

Here's the /park command:
PHP код:
CMD:park(playeridparams[])
{
    new
        
carid GetPlayerVehicleID(playerid);
    if (!
carid)
        return 
SendErrorMessage(playerid"You must be inside your vehicle.");
    if (
IsVehicleImpounded(carid))
        return 
SendErrorMessage(playerid"This vehicle is impounded and you can't use it.");
    if ((
carid Car_GetID(carid)) != -&& Car_IsOwner(playeridcarid))
    {
        if (
GetPlayerState(playerid) != PLAYER_STATE_DRIVER)
            return 
SendErrorMessage(playerid"You must be the driver!");
        static
            
g_arrSeatData[10] = {INVALID_PLAYER_ID, ...},
            
g_arrDamage[4],
            
Float:health,
            
seatid;
        for (new 
014++) {
                
CarData[carid][carMods][i] = GetVehicleComponentInSlot(CarData[carid][carVehicle], i);
          }
        
GetVehicleDamageStatus(CarData[carid][carVehicle], g_arrDamage[0], g_arrDamage[1], g_arrDamage[2], g_arrDamage[3]);
        
GetVehicleHealth(CarData[carid][carVehicle], health);
        foreach (new 
Player) if (IsPlayerInVehicle(iCarData[carid][carVehicle])) {
            
seatid GetPlayerVehicleSeat(i);
            
g_arrSeatData[seatid] = i;
        }
        
GetVehiclePos(CarData[carid][carVehicle], CarData[carid][carPos][0], CarData[carid][carPos][1], CarData[carid][carPos][2]);
        
GetVehicleZAngle(CarData[carid][carVehicle], CarData[carid][carPos][3]);
        for (new 
0MAX_DYNAMIC_CARS++)
        {
            if(
CarData[i][carID] == currentCarID[playerid] && CarData[i][carOwner] == PlayerData[playerid][pID] && CarData[i][carExists] == 1)
            {
                
Car_Spawn(i);
                
Car_Save(i);
                
DestroyVehicle(CarData[i][carVehicle]);
                
currentCarID[playerid] = 0;
                
SendServerMessage(playerid"You have successfully parked your %s."ReturnVehicleModelName(CarData[i][carModel]));
                break;
            }
        }
    }
    else 
SendErrorMessage(playerid"You are not inside anything you can park.");
    return 
1;

I also believe that these stocks/functions could be worth noting:
PHP код:
stock Car_Spawn(carid)
{
    if (
carid != -&& CarData[carid][carExists])
    {
        if (
IsValidVehicle(CarData[carid][carVehicle]))
            
DestroyVehicle(CarData[carid][carVehicle]);
        if (
CarData[carid][carColor1] == -1)
            
CarData[carid][carColor1] = random(127);
        if (
CarData[carid][carColor2] == -1)
            
CarData[carid][carColor2] = random(127);
        
CarData[carid][carVehicle] = CreateVehicle(CarData[carid][carModel], CarData[carid][carPos][0], CarData[carid][carPos][1], CarData[carid][carPos][2], CarData[carid][carPos][3], CarData[carid][carColor1], CarData[carid][carColor2], (CarData[carid][carOwner] != 0) ? (-1) : (1200000));
        if (
CarData[carid][carVehicle] != INVALID_VEHICLE_ID)
        {
            if (
CarData[carid][carPaintjob] != -1)
            {
                
ChangeVehiclePaintjob(CarData[carid][carVehicle], CarData[carid][carPaintjob]);
            }
            if (
CarData[carid][carLocked])
            {
                new
                    
enginelightsalarmdoorsbonnetbootobjective;
                
GetVehicleParamsEx(CarData[carid][carVehicle], enginelightsalarmdoorsbonnetbootobjective);
                
SetVehicleParamsEx(CarData[carid][carVehicle], enginelightsalarm1bonnetbootobjective);
            }
            for (new 
014++)
            {
                if (
CarData[carid][carMods][i]) AddVehicleComponent(CarData[carid][carVehicle], CarData[carid][carMods][i]);
            }
               
ResetVehicle(CarData[carid][carVehicle]);
            return 
1;
        }
    }
    return 
0;

PHP код:
Car_Save(carid)
{
    static
        
query[900];
    if (
CarData[carid][carVehicle] != INVALID_VEHICLE_ID)
    {
        for (new 
014++) {
            
CarData[carid][carMods][i] = GetVehicleComponentInSlot(CarData[carid][carVehicle], i);
        }
    }
    
format(querysizeof(query), "UPDATE `cars` SET `carModel` = '%d', `carOwner` = '%d', `carPosX` = '%.4f', `carPosY` = '%.4f', `carPosZ` = '%.4f', `carPosR` = '%.4f', `carColor1` = '%d', `carColor2` = '%d', `carPaintjob` = '%d', `carLocked` = '%d'",
        
CarData[carid][carModel],
        
CarData[carid][carOwner],
        
CarData[carid][carPos][0],
        
CarData[carid][carPos][1],
        
CarData[carid][carPos][2],
        
CarData[carid][carPos][3],
        
CarData[carid][carColor1],
        
CarData[carid][carColor2],
        
CarData[carid][carPaintjob],
        
CarData[carid][carLocked]
    );
    
format(querysizeof(query), "%s, `carMod1` = '%d', `carMod2` = '%d', `carMod3` = '%d', `carMod4` = '%d', `carMod5` = '%d', `carMod6` = '%d', `carMod7` = '%d', `carMod8` = '%d', `carMod9` = '%d', `carMod10` = '%d', `carMod11` = '%d', `carMod12` = '%d', `carMod13` = '%d', `carMod14` = '%d'",
        
query,
        
CarData[carid][carMods][0],
        
CarData[carid][carMods][1],
        
CarData[carid][carMods][2],
        
CarData[carid][carMods][3],
        
CarData[carid][carMods][4],
        
CarData[carid][carMods][5],
        
CarData[carid][carMods][6],
        
CarData[carid][carMods][7],
        
CarData[carid][carMods][8],
        
CarData[carid][carMods][9],
        
CarData[carid][carMods][10],
        
CarData[carid][carMods][11],
        
CarData[carid][carMods][12],
        
CarData[carid][carMods][13]
    );
    
format(querysizeof(query), "%s, `carImpounded` = '%d', `carImpoundPrice` = '%d', `carFaction` = '%d', `carWeapon1` = '%d', `carWeapon2` = '%d', `carWeapon3` = '%d', `carWeapon4` = '%d', `carWeapon5` = '%d', `carAmmo1` = '%d', `carAmmo2` = '%d', `carAmmo3` = '%d', `carAmmo4` = '%d', `carAmmo5` = '%d' WHERE `carID` = '%d'",
        
query,
        
CarData[carid][carImpounded],
        
CarData[carid][carImpoundPrice],
        
CarData[carid][carFaction],
        
CarData[carid][carWeapons][0],
        
CarData[carid][carWeapons][1],
        
CarData[carid][carWeapons][2],
        
CarData[carid][carWeapons][3],
        
CarData[carid][carWeapons][4],
        
CarData[carid][carAmmo][0],
        
CarData[carid][carAmmo][1],
        
CarData[carid][carAmmo][2],
        
CarData[carid][carAmmo][3],
        
CarData[carid][carAmmo][4],
        
CarData[carid][carID]
    );
    return 
mysql_tquery(g_iHandlequery);

PHP код:
stock ResetVehicle(vehicleid)
{
    if (
<= vehicleid <= MAX_VEHICLES)
    {
        if (
CoreVehicles[vehicleid][vehSirenOn] && IsValidDynamicObject(CoreVehicles[vehicleid][vehSirenObject]))
            
DestroyDynamicObject(CoreVehicles[vehicleid][vehSirenObject]);
        
CoreVehicles[vehicleid][vehFuel] = 100;
        
CoreVehicles[vehicleid][vehWindowsDown] = false;
        
CoreVehicles[vehicleid][vehTemporary] = 0;
          
CoreVehicles[vehicleid][vehLoads] = 0;
        
CoreVehicles[vehicleid][vehLoadType] = 0;
        
CoreVehicles[vehicleid][vehCrate] = INVALID_OBJECT_ID;
        
CoreVehicles[vehicleid][vehTrash] = 0;
        
CoreVehicles[vehicleid][vehRepairing] = 0;
        
CoreVehicles[vehicleid][vehSirenOn] = 0;
        
CoreVehicles[vehicleid][vehRadio] = 0;
    }
    return 
1;

Here's the video showcasing my problem, just incase anyone wants a visual(just click the thumbnail):


I'm just genuinely lost, I've tried multiple different ways of retrieving the proper variables of the current vehicle that way it'd update the current vehicle instead of being stuck on the old one. I'm out of ideas.
Reply
#2

****** translate: do you have a problem with data storage?
Reply
#3

Quote:
Originally Posted by Florin48
Посмотреть сообщение
****** translate: do you have a problem with data storage?
I do not. It is saving to the database properly considering it updates the original vehicle's position. I just am unsure as to why it update's the original vehicle's position instead of the vehicle you are currently in but it does destroy the vehicle after it saves the position.
Reply
#4

Not sure what ResetVehicle function does, so it may be a mix up in there.

But I think the issue lies in your queries
pawn Код:
format(query,*sizeof(query),*"UPDATE*`cars`*SET*`carModel`*=*'%d',*`carOwner`*=*'%d',*`carPosX`*=*'%.4f',*`carPosY`*=*'%.4f',*`carPosZ`*=*'%.4f',*`carPosR`*=*'%.4f',*`carColor1`*=*'%d',*`carColor2`*=*'%d',*`carPaintjob`*=*'%d',*`carLocked`*=*'%d'",
********CarData[carid][carModel],
********CarData[carid][carOwner],
********CarData[carid][carPos][0],
********CarData[carid][carPos][1],
********CarData[carid][carPos][2],
********CarData[carid][carPos][3],
********CarData[carid][carColor1],
********CarData[carid][carColor2],
********CarData[carid][carPaintjob],
********CarData[carid][carLocked]
****);
You aren't declaring which row to update, so I would assume it would set all cars to this data (or maybe just the first entry). Try use the WHERE statement to specify the vehicle's unique ID in the database
pawn Код:
format(query, sizeof(query), "UPDATE `cars` SET ... WHERE `id` = %i" ...);
EDIT: I just posted the code into an editor and see you concatenate a huge query, so disregard. I did notice however you never update "CarData[carid][carID]" anywhere. Is that where ResetVehicle() comes into play?
Reply
#5

Quote:
Originally Posted by Grim_
Посмотреть сообщение
Not sure what ResetVehicle function does, so it may be a mix up in there.

But I think the issue lies in your queries
pawn Код:
format(query,*sizeof(query),*"UPDATE*`cars`*SET*`carModel`*=*'%d',*`carOwner`*=*'%d',*`carPosX`*=*'%.4f',*`carPosY`*=*'%.4f',*`carPosZ`*=*'%.4f',*`carPosR`*=*'%.4f',*`carColor1`*=*'%d',*`carColor2`*=*'%d',*`carPaintjob`*=*'%d',*`carLocked`*=*'%d'",
********CarData[carid][carModel],
********CarData[carid][carOwner],
********CarData[carid][carPos][0],
********CarData[carid][carPos][1],
********CarData[carid][carPos][2],
********CarData[carid][carPos][3],
********CarData[carid][carColor1],
********CarData[carid][carColor2],
********CarData[carid][carPaintjob],
********CarData[carid][carLocked]
****);
You aren't declaring which row to update, so I would assume it would set all cars to this data (or maybe just the first entry). Try use the WHERE statement to specify the vehicle's unique ID in the database
pawn Код:
format(query, sizeof(query), "UPDATE `cars` SET ... WHERE `id` = %i" ...);
EDIT: I just posted the code into an editor and see you concatenate a huge query, so disregard. I did notice however you never update "CarData[carid][carID]" anywhere. Is that where ResetVehicle() comes into play?
I am declaring which row to update already, it's on the third format line in the Car_Save function:
PHP код:
WHERE `carID` = '%d'" 
EDIT: Adding to above, we don't update [carID] because that retrieves the value of the vehicle in the database. That stays the same no matter what.

Also, here's the ResetVehicle stock, I've now added this to the main post as well:
PHP код:
stock ResetVehicle(vehicleid)
{
    if (
<= vehicleid <= MAX_VEHICLES)
    {
        if (
CoreVehicles[vehicleid][vehSirenOn] && IsValidDynamicObject(CoreVehicles[vehicleid][vehSirenObject]))
            
DestroyDynamicObject(CoreVehicles[vehicleid][vehSirenObject]);
        
CoreVehicles[vehicleid][vehFuel] = 100;
        
CoreVehicles[vehicleid][vehWindowsDown] = false;
        
CoreVehicles[vehicleid][vehTemporary] = 0;
          
CoreVehicles[vehicleid][vehLoads] = 0;
        
CoreVehicles[vehicleid][vehLoadType] = 0;
        
CoreVehicles[vehicleid][vehCrate] = INVALID_OBJECT_ID;
        
CoreVehicles[vehicleid][vehTrash] = 0;
        
CoreVehicles[vehicleid][vehRepairing] = 0;
        
CoreVehicles[vehicleid][vehSirenOn] = 0;
        
CoreVehicles[vehicleid][vehRadio] = 0;
    }
    return 
1;

Reply
#6

Okay. Just to clarify you are updating CarData[carid][carID] when the player loads a different vehicle from the database? Because your query for updating the data is dependent on that row. If it doesn't update between vehicles it will edit only the first one.
Reply
#7

Quote:
Originally Posted by Grim_
Посмотреть сообщение
Okay. Just to clarify you are updating CarData[carid][carID] when the player loads a different vehicle from the database? Because your query for updating the data is dependent on that row. If it doesn't update between vehicles it will edit only the first one.
I'm 99% sure I am, at least that's what I meant for my code to do. Each vehicle has it's own separate "carID" in the database. I make it set the variable: currentCarID[playerid], to the value the player enters on the /vget command. So let's say they do: /vget 375, this will spawn the vehicle with "carID" 375 in the database and set the value of: currentCarID[playerid], to 375. Then, when the player does /park while inside the vehicle, it checks every vehicle in the database and if the vehicle's [carID] matches with the value of: currentCarID[playerid], then it will save/update that car's data and then destroy the vehicle.
Reply
#8

I see how you have it working now. I've spent a few minutes looking over it.

Did you intend to call Car_Spawn() inside of the /park command before saving it? If that isn't the issue then I cannot seem to find it either. There is something I think I should point out though
pawn Код:
for*(new*i*=*0;*i*<*MAX_DYNAMIC_CARS;*i*++)
********{
************if(CarData[i][carID]*==*currentCarID[playerid]*&&*CarData[i][carOwner]*==*PlayerData[playerid][pID]*&&*CarData[i][carExists]*==*1)
************{
****************Car_Spawn(i);
****************Car_Save(i);
****************DestroyVehicle(CarData[i][carVehicle]);
****************currentCarID[playerid]*=*0;

****************SendServerMessage(playerid,*"You*have*successfully*parked*your*%s.",*ReturnVehicleModelName(CarData[i][carModel]));
****************break;
************}
********}
The loops seem redundant no? I assume Car_GetID(vehicle) retrieves the database unique ID for the vehicle? If that's the case in the /park command the local 'carid' is assigned that ID
pawn Код:
if*((carid*=*Car_GetID(carid))*!=*-1*&&*Car_IsOwner(playerid,*carid))
So instead of creating a loop to check over each vehicle, we could use the 'carid' variable to access the array directly since the array's first slot is the vehicle's unique ID in the database. Then call Car_Spawn/Save with 'carid' (since they also take the unique ID). Same goes for the loop in the /vget command (just do bounds check to prevent overflow). Or better yet just use currentCarID[playerid] as it holds the same value too.

Код:
CarInfo[carid][carID] = Unique database ID
CarInfo[100][carID] = 100
currentCarID[playerid] = 100
This, again, assumes I have it laid out correctly in my head...
Reply
#9

Quote:
Originally Posted by Grim_
Посмотреть сообщение
I see how you have it working now. I've spent a few minutes looking over it.

Did you intend to call Car_Spawn() inside of the /park command before saving it? If that isn't the issue then I cannot seem to find it either. There is something I think I should point out though
pawn Код:
for*(new*i*=*0;*i*<*MAX_DYNAMIC_CARS;*i*++)
********{
************if(CarData[i][carID]*==*currentCarID[playerid]*&&*CarData[i][carOwner]*==*PlayerData[playerid][pID]*&&*CarData[i][carExists]*==*1)
************{
****************Car_Spawn(i);
****************Car_Save(i);
****************DestroyVehicle(CarData[i][carVehicle]);
****************currentCarID[playerid]*=*0;

****************SendServerMessage(playerid,*"You*have*successfully*parked*your*%s.",*ReturnVehicleModelName(CarData[i][carModel]));
****************break;
************}
********}
The loops seem redundant no? I assume Car_GetID(vehicle) retrieves the database unique ID for the vehicle? If that's the case in the /park command the local 'carid' is assigned that ID
pawn Код:
if*((carid*=*Car_GetID(carid))*!=*-1*&&*Car_IsOwner(playerid,*carid))
So instead of creating a loop to check over each vehicle, we could use the 'carid' variable to access the array directly since the array's first slot is the vehicle's unique ID in the database. Then call Car_Spawn/Save with 'carid' (since they also take the unique ID). Same goes for the loop in the /vget command (just do bounds check to prevent overflow). Or better yet just use currentCarID[playerid] as it holds the same value too.

Код:
CarInfo[carid][carID] = Unique database ID
CarInfo[100][carID] = 100
currentCarID[playerid] = 100
This, again, assumes I have it laid out correctly in my head...
I've already tried the /park command without having Car_Spawn inside of it. My thought pattern was that it'd spawn and update any cached information of the previous vehicle but it doesn't. So, that doesn't do anything here. Besides that, your thought process is correct with Car_GetID(vehicleid); What it does is gets the database carID of the physical vehicleid in-game. I also have tried the /park command without the for-loop and instead just having the command like this:
PHP код:
CMD:park(playeridparams[])
{
    new
        
carid GetPlayerVehicleID(playerid);
    if (!
carid)
        return 
SendErrorMessage(playerid"You must be inside your vehicle.");
    if (
IsVehicleImpounded(carid))
        return 
SendErrorMessage(playerid"This vehicle is impounded and you can't use it.");
    if ((
carid Car_GetID(carid)) != -&& Car_IsOwner(playeridcarid))
    {
        if (
GetPlayerState(playerid) != PLAYER_STATE_DRIVER)
            return 
SendErrorMessage(playerid"You must be the driver!");
        static
            
g_arrSeatData[10] = {INVALID_PLAYER_ID, ...},
            
g_arrDamage[4],
            
Float:health,
            
seatid;
        for (new 
014++) {
                
CarData[carid][carMods][i] = GetVehicleComponentInSlot(CarData[carid][carVehicle], i);
          }
        
GetVehicleDamageStatus(CarData[carid][carVehicle], g_arrDamage[0], g_arrDamage[1], g_arrDamage[2], g_arrDamage[3]);
        
GetVehicleHealth(CarData[carid][carVehicle], health);
        foreach (new 
Player) if (IsPlayerInVehicle(iCarData[carid][carVehicle])) {
            
seatid GetPlayerVehicleSeat(i);
            
g_arrSeatData[seatid] = i;
        }
        
GetVehiclePos(CarData[carid][carVehicle], CarData[carid][carPos][0], CarData[carid][carPos][1], CarData[carid][carPos][2]);
        
GetVehicleZAngle(CarData[carid][carVehicle], CarData[carid][carPos][3]);
        
Car_Spawn(carid);
        
Car_Save(carid);
        
DestroyVehicle(CarData[carid][carVehicle]);
        
currentCarID[playerid] = 0;
        
SendServerMessage(playerid"You have successfully parked your %s."ReturnVehicleModelName(CarData[carid][carModel]));
    }
    else 
SendErrorMessage(playerid"You are not inside anything you can park.");
    return 
1;

Yet, I had the exact same problem as I am currently having.
Reply
#10

Can you post the SQL log of the queries? That should tell us something.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)