[MySQL] Vehicle System
#1

Look Down..
Reply
#2

Bump. Been almost a day. Please, somebody?
Reply
#3

I'm not really sure if this would work, try changing your if statement at SpawnPlayerVehicle to;

pawn Code:
if(rows > 0)
Reply
#4

Quote:
Originally Posted by Zex Tan
View Post
I'm not really sure if this would work, try changing your if statement at SpawnPlayerVehicle to;

pawn Code:
if(rows > 0)
Thanks for helping.
That didn't work though, I think I know what the problem is. When I create a vehicle, it's fine. When I create another, it loads it again from the database along with 'OTHER CARS' created first. So, the previous ones duplicate. Creating third car duplicates the previous 2. Any help?
Reply
#5

It's because you're loading them all again instead of only the last one you added.

You add one vehicle to the database and you load it, fine until here.
You add the second car and reload all vehicles, loading both, so you end up with 3 vehicles instead of 2.

Why not just create the vehicle in the first place in your CreatePlayerVehicle stock?
Setup all data there, create the vehicle and spawn it, all in one go.
Then add it to your database with your insert query and you're done.

Next time your server is started and you login, you can use your "SELECT * FROM `Vehicles` WHERE `vOwner`='%e'" query to load them all at once during OnPlayerConnect.

There is no need to create the data (without creating the vehicle), store it in the database, reload it to create your vehicles based on the saved data and create them after they have been loaded.
Reply
#6

*REMOVED*
Thank you PowerPC603
Reply
#7

Can you show the entire code? Where you create the vehicles, load the vehicles and the code that displays the dialog?
It's hard to see the problem with only bits and pieces of it.
Reply
#8

*REMOVED*
Thank you PowerPC603.
Reply
#9

I tried debugging the parameters, until I knew where the bug was.
When I did this:
pawn Code:
printf("ID: %d, Plate: %s, Owner: %s",VehicleInfo[i][vActualID], VehicleInfo[i][vPlate], VehicleInfo[i][vOwner]);
Everything went fine.It loaded all the info, since I was using the info obtained from the tables.

When I did this:
pawn Code:
printf("ID: %d, Plate: %s, Owner: %s",VehicleInfo[i][vActualID], VehicleInfo[OwnedVehicle[playerid][i][vID]][vPlate],VehicleInfo[OwnedVehicle[playerid][i][vID]][vOwner]);
Things went the wrong way. The ID loaded since I again used the data, but when it came to 'OwnedVehicle' thingy, it bugged printing this. 'ID: 2, Plate: , Owner: ' i.e Plate and Owners were empty.
This makes it clear, that the problem is in OwnedVehicle.
What I want to make it work is, have '3 Vehicles' per Player. And I couldn't figure out any other way?
Reply
#10

You still have the same setup as before.
Your CreatePlayerVehicle still only saves the data into the database, which you load again afterwards using your timer and SpawnPlayerVehicle.

It makes it alot more confusing this way.

You also seem to have a major bug in there if more than one player has their own vehicles.
Since you loop through "i" from 0 to "rows", and you use that variable in your VehicleInfo array (I guess that's the one which would hold all data about all vehicles in your script, which usually is 2000 indices large).

In that case, the next player creates a new vehicle, where your data gets overwritten by his vehicles, since index 0 to 2 (seems like you allow 3 vehicles per player) are used all over again.



You should really create the vehicle first, so you can get an unused vehicleid to do all your stuff.
In the CreatePlayerVehicle function, your first line should be "new vehicleid = CreateVehicle(model, ...)".
Then set the data like the numberplate directly to the vehicle.

Then store the data in your VehicleInfo array, use the "vehicleid" variable to address the index in that array.
Since CreateVehicle doesn't allow 2 vehicles with the same id, you won't risk your data to be overwritten.

When that's done, use the INSERT query to save the vehicle in your database and you're done.
You won't need to reload the data you've just saved, as it's already in place.
You would also avoid loading multiple copies of the same vehicles over and over again when creating new vehicles.

And there is also a way to retrieve the newly inserted SQLid for your new vehicle in the database.


This code may not be complete, but it shows how it's usually done.
It's also easier to follow and you avoid bugs likes the ones you already experienced.
pawn Code:
stock CreatePlayerVehicle(playerid, Model, Color1, Color2, Price, Float:x, Float:y, Float:z, Float:a, Locked = 0, Plate[] = "")
{
    // Setup local variables
    new name[MAX_PLAYER_NAME], query[800], Slot;

    // Check if the player doesn't have 3 vehicles yet
    for (Slot = 0; Slot < 3; Slot++)
    {
        // Check if the current slot is free (no valid vehicleid, as those start with "1" for the first vehicle created)
        // When the slot is free, use "break" to exit the loop and hold the current Slot value
        if (OwnedVehicle[playerid][Slot] == 0)
            break;
    }
    // If the player already has 3 vehicles (Slot would point to "3"), exit the function with a message
    if (Slot == 3) return SendClientMessage(playerid, 0xFF0000FF, "You can only have 3 vehicles");

    // Immediately create a new vehicle on the server with the data provided
    new vehicleid = CreateVehicle(Model, x, y, z, a, Color1, Color2, 60);
    // Set numberplate directly to the vehicle
    SetVehicleNumberPlate(vehicleid, Plate);

    // Get the player's name
    GetPlayerName(playerid, name, sizeof(name));

    // Store all data about the new player-vehicle
    format(VehicleInfo[vehicleid][vOwner], MAX_PLAYER_NAME, "%s", name);
    format(VehicleInfo[vehicleid][vPlate], 12, "%s", Plate);
    VehicleInfo[vehicleid][vModel] = Model;
    VehicleInfo[vehicleid][vX] = x;
    VehicleInfo[vehicleid][vY] = y;
    VehicleInfo[vehicleid][vZ] = z;
    VehicleInfo[vehicleid][vA] = a;
    VehicleInfo[vehicleid][vColor1] = Color1;
    VehicleInfo[vehicleid][vColor2] = Color2;
    // Other data can also be stored in case you need it

    // Here, we store which vehicleid the player has
    OwnedVehicle[playerid][Slot] = vehicleid;


    // Now it's time to save the vehicledata to your database
    mysql_format(handle, query, sizeof(query), "INSERT INTO `vehicles` (vModel, vOwner, vOwnerID, vColor1, vColor2, vPrice, vX, vY, vZ, vA, vLocked, vPlate) \
    VALUES ('%i', '%e', '%i', '%i', '%i', '%i', '%f', '%f', '%f', '%f', '%i', '%s')"
, Model, name, Character[playerid][CurrentCharacter[playerid]][ID], Color1, Color2, Price, x, y, z, a, Locked, Plate);
    // Execute the query and call a callback when the vehicle has been inserted
    mysql_tquery(handle, query, "GetVehicleSQLID", "i", vehicleid);
}


// Since you only need the vehicleid for each vehicle, you don't need an enum here
new OwnedVehicle[MAX_PLAYERS][3];



forward GetVehicleSQLID(vehicleid);
public GetVehicleSQLID(vehicleid)
{
    // Get the new index in MySQL where the vehicle got inserted (this must be the auto-increment column and it must also be set as PRIMARY KEY)
    VehicleInfo[vehicleid][vID] = cache_insert_id();

    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)