Need a suggestion - Vehicles
#1

I was sure that I managed to make a vehicle system, but later I figured out that it won't work perfectly.
You see, I have no problem with createing vehicles, and loading them on GameModeInit, but I do have a problem with deleting some of them.
If I delete a created vehicle whitch I have already set it's information and vars, the vehicle gets deleted, and if I try to create a new vehicle it will create the new vehicle with the wrong stats, becasue it will apply on the vehicle ID whitch I just deleted.

I know that its hard to understand what I mean, therfore I will show some examples of my code to make sure you guys can understand me.

This is my LoadVehicles function whitch gets called on GameModeInit:
pawn Код:
LoadVehicles()
{
    totalVehicles = 1;
    mysql_query("SELECT * FROM `Vehicles`");
    mysql_store_result();
    if(mysql_num_rows() > 0)
    {
        while(mysql_fetch_row(Query))
        {
            sscanf(Query, "e<p<|>ddffffddddds[10]d>", Vehicles[totalVehicles]);
            SetVehicleNumberPlate(CreateVehicle(Vehicles[totalVehicles][Model],Vehicles[totalVehicles][PosX],Vehicles[totalVehicles][PosY],Vehicles[totalVehicles][PosZ],Vehicles[totalVehicles][Angle],Vehicles[totalVehicles][Color1],Vehicles[totalVehicles][Color2], -1), Vehicles[totalVehicles][Plate]);
            LinkVehicleToInterior(totalVehicles, 0);
            SetVehicleVirtualWorld(totalVehicles, 0);

            totalVehicles++;
        }
    }
    mysql_free_result();
    print("\n");
    printf("SERVER: Loaded %d MySQL vehicles successfully.", totalVehicles);
    return 1;
}
It loads each vehicle from my database and adds one to the 'totalVehicles' var.
This is my /createvehicle command, whitch creates a vehicle and automaticly loads it from my database:
pawn Код:
command(createvehicle, playerid, params[])
{
    #pragma unused params
    if(IsPlayerInAnyVehicle(playerid))
        return SendClientMessage(playerid, COLOR_BRIGHTRED, "You are insede of another vehicle, please get out first.");
    new ModelID, color1, color2, Float:PositionX, Float:PositionY, Float:PositionZ, Float:AngleZ, plate[10], faction, vehId;
    if (!sscanf(params, "iiii", ModelID, color1, color2, faction))
    {
        if(ModelID < 400 || ModelID > 611) return SendClientMessage(playerid, COLOR_BRIGHTRED, "[ERROR]: ModelID can not be smaller then 400 or bigger then 611!");
        if(color1 < 0 || color1 > 252 || color2 < 0 || color2 > 252 ) return SendClientMessage(playerid, COLOR_BRIGHTRED, "[ERROR]: Color can not be smaller then 0 or bigger then 252!");

        plate = CreateVehiclePlate();
        GetPlayerPos(playerid, PositionX, PositionY, PositionZ);
        GetPlayerFacingAngle(playerid, AngleZ);
        format(Query, sizeof(Query), "INSERT INTO `vehicles` (Model, PosX, PosY, PosZ, Angle, Color1, Color2, Plate) VALUES(%d, %f, %f, %f, %f, %d, %d, '%s')", ModelID, PositionX, PositionY, PositionZ, AngleZ, color1, color2, plate);
        mysql_query(Query);
        SendClientMessage(playerid, COLOR_BRIGHTRED, "You have added a new vehicle to the Database");

        format(Query, sizeof(Query), "SELECT * FROM vehicles WHERE Plate = '%s'", plate);
        mysql_query(Query);
        new a = mysql_store_result();
        printf("%s", a);
        if(mysql_num_rows() > 0)
        {
            if(mysql_fetch_row(Query))
            {
                sscanf(Query, "e<p<|>ddffffddddds[10]d>", Vehicles[totalVehicles]);
                SetVehicleNumberPlate(CreateVehicle(Vehicles[totalVehicles][Model],Vehicles[totalVehicles][PosX],Vehicles[totalVehicles][PosY],Vehicles[totalVehicles][PosZ],Vehicles[totalVehicles][Angle],Vehicles[totalVehicles][Color1],Vehicles[totalVehicles][Color2], -1), Vehicles[totalVehicles][Plate]);
                LinkVehicleToInterior(totalVehicles, 0);
                SetVehicleVirtualWorld(totalVehicles, 0);
                totalVehicles++;
            }
        }
        mysql_free_result();
       
        //LoadVehicle(plate);
       
        return 1;
    }
    return SendClientMessage(playerid, COLOR_GREY, "[USAGE]: /createvehicle [model] [color1] [color2] [faction]");

}
And the last command I would like you to see is my DeleteVehicle function - Delets the vehicle from the database and destroys the vehicle in server:
pawn Код:
DeleteVehicle(vehicleid)
{
    CheckMySQL();
    mysql_debug(1);
    format(Query, sizeof(Query), "DELETE FROM vehicles WHERE VehicleID='%d'", Vehicles[vehicleid][VehicleID]);
    mysql_query(Query);
    DestroyVehicle(vehicleid);
    return 1;
}
If I delete a vehicle and then creats a new one, in the script it will creats the vehicle for the last totalVehicles num, but in server it will creats the vehicle for the first empty vehicle id slot.

The only solution for this problem is relloading all the server vehicles - to make sure each vehicle will get the right vehicle ID (from the totalVehicles).

Are there any other ways to make the job done? I will be glad to get some suggestions.

EDIT: 'Plate' and 'VechicleID' fields will show up twice in the database with the same information. The 'VehicleID' filled is an auto-increase field whitch gives the vehicle's his privet ID each time a new vehicle is created, and the 'Plate' field is like the vehicle-code whitch like the first field - can not be the same with another Vehicle's 'Plate' field.
Reply
#2

Nice Idea tommy!
Reply
#3

Quote:
Originally Posted by nikivista
Посмотреть сообщение
Nice Idea tommy!
If you are talking about my solution to the problem - then its not a solution.
I'm asking to get a REAL EFFECTIVE solution, not reloading my vehicles each time somebody delets his vehicle.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)