SA-MP Forums Archive
Vehicle command - 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 command (/showthread.php?tid=663856)



Vehicle command - SymonClash - 13.02.2019

I'm scripting a vehicle system and made this /createvehicle command, which insert created vehicle in the "vehicles" table:

pawn Код:
CMD:createvehicle(playerid, params[])
{
    if(Vehicle_Nearest(playerid) != -1) return SCM(playerid, COLOR_ERROR, "* You can't create a vehicle near another one.");
   
    new modelid[30], vehid, color1, color2, price;
   
    if(sscanf(params, "s[30]iid", modelid, color1, color2, price)) return SCM(playerid, COLOR_BELGREEN, "* [USAGE]: /createvehicle <ModelID/Vehicle Name> <Color1> <Color2> <Price>");

    if(IsNumeric(modelid)) vehid = strval(modelid);
    else vehid = ReturnVehicleModelID(modelid);

    if(vehid < 400 || vehid > 611) return SCM(playerid, COLOR_ERROR, "* Invalid vehicle model.");

    if(IsPlayerInAnyVehicle(playerid)) return SCM(playerid, COLOR_ERROR, "* You must be on foot to create a vehicle.");

    static Float:x, Float:y, Float:z, Float:angle;
   
    GetPlayerPos(playerid, x, y, z);
    GetPlayerFacingAngle(playerid, angle);

    for (new i = 0; i != MAX_VEH; i ++) if (!VehicleData[i][vehicleExists])
    {
        VehicleData[i][vehicleExists] = true;
       
        VehicleData[i][vehicleDBID] = cache_insert_id();
       
        SetVehicleNumberPlate(VehicleData[i][vehiclePlate], "SSRP");
       
        VehicleData[i][vehicleModel] = vehid;
       
        format(VehicleData[i][vehicleName], MAX_PLAYER_NAME, GetVehicleName(vehid));
        format(VehicleData[i][vehicleOwner], MAX_PLAYER_NAME, "-");
        format(VehicleData[i][vehiclePlate], 16, "SSRP");

        VehicleData[i][vehiclePrice] = price;
       
        VehicleData[i][vehicleLock] = 0;
       
        VehicleData[i][vehicleColorOne] = color1;
        VehicleData[i][vehicleColorTwo] = color2;

        VehicleData[i][vehiclePos][0] = x;
        VehicleData[i][vehiclePos][1] = y;
        VehicleData[i][vehiclePos][2] = z;
        VehicleData[i][vehiclePos][3] = angle;

        VehicleData[i][vehicleInterior] = GetPlayerInterior(playerid);
        VehicleData[i][vehicleWorld] = GetPlayerVirtualWorld(playerid);
       
        VehicleData[i][vehicleOwned] = 0;
       
        new query[1000];
       
        format(query,sizeof(query),"INSERT INTO `vehicles` SET vehicleDBID = %d, vehicleName = '%e', vehicleOwner = '%e', vehiclePlate = '%e'",
        VehicleData[i][vehicleDBID], VehicleData[i][vehicleName], VehicleData[i][vehicleOwner], VehicleData[i][vehiclePlate]);
        mysql_query(mysql, query);
       
        format(query,sizeof(query),"INSERT INTO `vehicles` SET vehicleDBID = %d, vehicleModel = %d, vehiclePrice = %d, vehicleColorOne = %d, vehicleColorTwo = %d",
        VehicleData[i][vehicleDBID], VehicleData[i][vehicleModel], VehicleData[i][vehiclePrice], VehicleData[i][vehicleColorOne], VehicleData[i][vehicleColorTwo]);
        mysql_query(mysql, query);
       
        format(query,sizeof(query),"INSERT INTO `vehicles` SET vehicleDBID = %d, vehicleX = %f, vehicleY = %f, vehicleZ = %f, vehicleA = %f",
        VehicleData[i][vehicleDBID], VehicleData[i][vehiclePos][0], VehicleData[i][vehiclePos][1], VehicleData[i][vehiclePos][2], VehicleData[i][vehiclePos][3]);
        mysql_query(mysql, query);

        format(query,sizeof(query),"INSERT INTO `vehicles` SET vehicleDBID = %d, vehicleInterior = %d, vehicleWorld = %d, vehicleOwned = %d",
        VehicleData[i][vehicleDBID], VehicleData[i][vehicleInterior], VehicleData[i][vehicleWorld], VehicleData[i][vehicleOwned]);
        mysql_query(mysql, query);
       
        Vehicle_Save(i);

        Vehicle_Refresh(i);

        if(!VehicleData[i][vehicleExists]) return 0;
    }
    return 1;
}
But it doesn't work. VehicleDBID is set as auto increment but everytime i create a vehicle, its DBID is -1. Also i'm getting "duplicate entry key for DBID -1" in error.log.

And in "vehicles" table all the vehicle data (model, price, color etc) is blank, but it shows the correct executing query in mysql log.

Now i know the problem is in that sql INSERT query, how to fix it?

Error log:

Quote:

[11:30:34] [plugins/mysql] cache_insert_id: no active cache (C:\test\v.pwn:376)
[11:30:34] [plugins/mysql] error #1062 while executing query "INSERT INTO `vehicles` SET vehicleDBID = -1, vehicleName = 'e', vehicleOwner = 'e', vehiclePlate = 'e'": Duplicate entry '-1' for key 'vehicleDBID' (C:\test\v.pwn:407)
[11:30:34] [plugins/mysql] error #1062 while executing query "INSERT INTO `vehicles` SET vehicleDBID = -1, vehicleModel = 411, vehiclePrice = 5000, vehicleColorOne = 200, vehicleColorTwo = 50": Duplicate entry '-1' for key 'vehicleDBID' (C:\test\v.pwn:411)
[11:30:34] [plugins/mysql] error #1062 while executing query "INSERT INTO `vehicles` SET vehicleDBID = -1, vehicleX = 1299.185913, vehicleY = -801.424926, vehicleZ = 84.140625, vehicleA = 269.527404": Duplicate entry '-1' for key 'vehicleDBID' (C:\test\v.pwn:415)
[11:30:34] [plugins/mysql] error #1062 while executing query "INSERT INTO `vehicles` SET vehicleDBID = -1, vehicleInterior = 0, vehicleWorld = 0, vehicleOwned = 0": Duplicate entry '-1' for key 'vehicleDBID' (C:\test\v.pwn:419)
[11:30:34] [plugins/mysql] cache_insert_id: no active cache (C:\test\v.pwn:376)

Also i don't know why i'm getting no active cache since i'm connected to the database.


Re: Vehicle command - d3Pedro - 13.02.2019

You're doing it wrong!
pawn Код:
format(query,sizeof(query),"INSERT INTO `vehicles` (vehicleDBID, vehicleName, vehicleOwner, vehiclePlate) VALUES(%d,'%e','%e','%e')",
VehicleData[i][vehicleDBID], VehicleData[i][vehicleName], VehicleData[i][vehicleOwner], VehicleData[i][vehiclePlate]);
mysql_query(mysql, query);
Do this for the rest of your queries in that command! Also read: https://www.tutorialspoint.com/mysql...sert-query.htm


Re: Vehicle command - Calisthenics - 13.02.2019

The first mistake is that `cache_insert_id` returns -1 on error for R40 version and above. You call a cache function before even executing the INSERT query.

The second mistake is that `format` function does not support %e specifier. Use `mysql_format` instead.

The third mistake is the number of INSERT queries you are trying to execute. It must be 1 query otherwise it will create 4 rows with different dbid but duplicate data.

Move the `IsPlayerInAnyVehicle` check at the very top.

You know the modelid, no need to store the vehicle name as it can be easily retrieved.

Vehicle owner should be an integer, sql unique id of the player which can be linked and can also be easily compared with. If a vehicle is not owned, owner should be NULL and makes `owned` column redundant.

Set default value for plate so you do not have to set it in the query. You call SetVehicleNumberPlate but the vehicle was not created yet.

Pass everything as arguments and when the query succeeded, assign them to the variables.

While INSERT INTO .. SET syntax is valid, I'll show an example with this mysql extension and not the standard sql syntax. If you change the owner datatype, remove the redundant vehicle name and set the default value for vehicle plate, you can remove them from the query:
pawn Код:
new player_interior = GetPlayerInterior(playerid),
    player_virtual_world = GetPlayerVirtualWorld(playerid);
   
mysql_format(mysql, query, sizeof(query),
    "INSERT INTO vehicles \
        SET vehicleName='%e', \
            vehicleOwner='-', \
            vehiclePlate='SSRP', \
            vehicleModel=%d, \
            vehiclePrice=%d, \
            vehicleColorOne=%d, \
            vehicleColorTwo=%d, \
            vehicleX=%f, \
            vehicleY=%f, \
            vehicleZ=%f, \
            vehicleA=%f, \
            vehicleInterior=%d, \
            vehicleWorld=%d, \
            vehicleOwned=0"
,

    GetVehicleName(vehid),
    vehid,
    price,
    color1,
    color2,
    x,
    y,
    z,
    angle,
    player_interior,
    player_virtual_world);

mysql_tquery(mysql, query, "OnVehicleCreate", "dddddffffdd", veh_index, vehid, price, color1, color2, x, y, z, angle, player_interior, player_virtual_world);
pawn Код:
forward OnVehicleCreate(veh_index, vehid, price, color1, color2, Float: x, Float: y, Float: z, Float: angle, vehicle_interior, vehicle_virtual_world);
public OnVehicleCreate(veh_index, vehid, price, color1, color2, Float: x, Float: y, Float: z, Float: angle, vehicle_interior, vehicle_virtual_world)
{
    VehicleData[veh_index][vehicleDBID] = cache_insert_id();
    VehicleData[veh_index][vehicleExists] = true;

    VehicleData[veh_index][vehicleModel] = vehid;
 
    strcpy(VehicleData[veh_index][vehicleName], GetVehicleName(vehid), MAX_PLAYER_NAME);
    strcpy(VehicleData[veh_index][vehicleOwner], "-", MAX_PLAYER_NAME);
    strcpy(VehicleData[veh_index][vehiclePlate], "SSRP", 16);

    VehicleData[veh_index][vehiclePrice] = price;

    VehicleData[veh_index][vehicleLock] = 0;

    VehicleData[veh_index][vehicleColorOne] = color1;
    VehicleData[veh_index][vehicleColorTwo] = color2;

    VehicleData[veh_index][vehiclePos][0] = x;
    VehicleData[veh_index][vehiclePos][1] = y;
    VehicleData[veh_index][vehiclePos][2] = z;
    VehicleData[veh_index][vehiclePos][3] = angle;

    VehicleData[veh_index][vehicleInterior] = vehicle_interior;
    VehicleData[veh_index][vehicleWorld] = vehicle_virtual_world;

    VehicleData[veh_index][vehicleOwned] = 0;
 
    /*
        create the vehicle here.
        what do `Vehicle_Save` and `Vehicle_Refresh` do?
    */

}



Re: Vehicle command - d3Pedro - 13.02.2019

Quote:
Originally Posted by Calisthenics
Посмотреть сообщение
The first mistake is that `cache_insert_id` returns -1 on error for R40 version and above. You call a cache function before even executing the INSERT query.

The second mistake is that `format` function does not support %e specifier. Use `mysql_format` instead.

The third mistake is the number of INSERT queries you are trying to execute. It must be 1 query otherwise it will create 4 rows with different dbid but duplicate data.

Move the `IsPlayerInAnyVehicle` check at the very top.

You know the modelid, no need to store the vehicle name as it can be easily retrieved.

Vehicle owner should be an integer, sql unique id of the player which can be linked and can also be easily compared with. If a vehicle is not owned, owner should be NULL and makes `owned` column redundant.

Set default value for plate so you do not have to set it in the query. You call SetVehicleNumberPlate but the vehicle was not created yet.

Pass everything as arguments and when the query succeeded, assign them to the variables.

While INSERT INTO .. SET syntax is valid, I'll show an example with this mysql extension and not the standard sql syntax. If you change the owner datatype and remove the redundant vehicle name, you can remove them from the query:
pawn Код:
new player_interior = GetPlayerInterior(playerid),
    player_virtual_world = GetPlayerVirtualWorld(playerid);
   
mysql_format(mysql, query, sizeof(query),
    "INSERT INTO vehicles \
        SET vehicleName='%e', \
            vehicleOwner='-', \
            vehiclePlate='SSRP', \
            vehicleModel=%d, \
            vehiclePrice=%d, \
            vehicleColorOne=%d, \
            vehicleColorTwo=%d, \
            vehicleX=%f, \
            vehicleY=%f, \
            vehicleZ=%f, \
            vehicleA=%f, \
            vehicleInterior=%d, \
            vehicleWorld=%d, \
            vehicleOwned=0"
,

    GetVehicleName(vehid),
    vehid,
    price,
    color1,
    color2,
    x,
    y,
    z,
    angle,
    player_interior,
    player_virtual_world);

mysql_tquery(mysql, query, "OnVehicleCreate", "dddddffffdd", veh_index, vehid, price, color1, color2, x, y, z, angle, player_interior, player_virtual_world);
pawn Код:
forward OnVehicleCreate(veh_index, vehid, price, color1, color2, Float: x, Float: y, Float: z, Float: angle, vehicle_interior, vehicle_virtual_world);
public OnVehicleCreate(veh_index, vehid, price, color1, color2, Float: x, Float: y, Float: z, Float: angle, vehicle_interior, vehicle_virtual_world)
{
    VehicleData[veh_index][vehicleDBID] = cache_insert_id();
    VehicleData[veh_index][vehicleExists] = true;

    VehicleData[veh_index][vehicleModel] = vehid;
 
    strcpy(VehicleData[veh_index][vehicleName], GetVehicleName(vehid), MAX_PLAYER_NAME);
    strcpy(VehicleData[veh_index][vehicleOwner], "-", MAX_PLAYER_NAME);
    strcpy(VehicleData[veh_index][vehiclePlate], "SSRP", 16);

    VehicleData[veh_index][vehiclePrice] = price;

    VehicleData[veh_index][vehicleLock] = 0;

    VehicleData[veh_index][vehicleColorOne] = color1;
    VehicleData[veh_index][vehicleColorTwo] = color2;

    VehicleData[veh_index][vehiclePos][0] = x;
    VehicleData[veh_index][vehiclePos][1] = y;
    VehicleData[veh_index][vehiclePos][2] = z;
    VehicleData[veh_index][vehiclePos][3] = angle;

    VehicleData[veh_index][vehicleInterior] = vehicle_interior;
    VehicleData[veh_index][vehicleWorld] = vehicle_virtual_world;

    VehicleData[veh_index][vehicleOwned] = 0;
 
    /*
        create the vehicle here.
        what do `Vehicle_Save` and `Vehicle_Refresh` do?
    */

}
I didn't read the whole code tho! This is a good way of doing it, but if you look at his code he actually got
pawn Код:
Vehicle_Save(i);

        Vehicle_Refresh(i);
So in my way, I'd go with inserting the cache then just saving it and refreshing which is probably reloading vehicle using these two functions.


Re: Vehicle command - SymonClash - 13.02.2019

Thank you both.

@Calisthenics, this is what Vehicle_Save and Vehicle_Refresh do:

pawn Код:
stock Vehicle_Save(vid)
{
    static query[256];

    format(query, sizeof(query), "UPDATE `vehicles` SET `vehicleOwner` = '%e', `vehicleX` = '%.4f', `vehicleY` = '%.4f', `vehicleZ` = '%.4f', `vehicleA` = '%.4f', `vehicleInterior` = '%d', `vehicleWorld` = '%d' WHERE `vehicleDBID` = '%d'",
        VehicleData[vid][vehicleOwner],
        VehicleData[vid][vehiclePos][0],
        VehicleData[vid][vehiclePos][1],
        VehicleData[vid][vehiclePos][2],
        VehicleData[vid][vehiclePos][3],
        VehicleData[vid][vehicleInterior],
        VehicleData[vid][vehicleWorld],
        VehicleData[vid][vehicleDBID]
    );
    return mysql_tquery(mysql, query);
}
pawn Код:
stock Vehicle_Refresh(vid)
{
    if(vid != -1 && VehicleData[vid][vehicleExists] && VehicleData[vid][vehicleOwned] == 0)
    {
        static string[128];

        if(IsValidDynamic3DTextLabel(VehicleData[vid][vehicleLabel])) DestroyDynamic3DTextLabel(VehicleData[vid][vehicleLabel]);

        VehicleData[vid][vehicleDBID] = CreateVehicle(vid, VehicleData[vid][vehiclePos][0], VehicleData[vid][vehiclePos][1], VehicleData[vid][vehiclePos][2], VehicleData[vid][vehiclePos][3], VehicleData[vid][vehicleColorOne], VehicleData[vid][vehicleColorOne], 10);

        format(string, sizeof(string), "%s - For Sale (%s)", VehicleData[vid][vehicleName], VehicleData[vid][vehiclePrice]);
        VehicleData[vid][vehicleLabel] = CreateDynamic3DTextLabel(string, -1, VehicleData[vid][vehiclePos][0], VehicleData[vid][vehiclePos][1], VehicleData[vid][vehiclePos][2], 15.0, INVALID_VEHICLE_ID, INVALID_PLAYER_ID, 0, VehicleData[vid][vehicleWorld], VehicleData[vid][vehicleInterior]);

        UpdateDynamic3DTextLabelText(VehicleData[vid][vehicleLabel], -1, string);
    }
    return 1;
}
Edit: Now the command is this:

pawn Код:
CMD:createvehicle(playerid, params[])
{
    if(IsPlayerInAnyVehicle(playerid)) return SCM(playerid, COLOR_ERROR, "* You must be on foot to create a vehicle.");
   
    if(Vehicle_Nearest(playerid) != -1) return SCM(playerid, COLOR_ERROR, "* You can't create a vehicle near another one.");
   
    new modelid[30], vehid, color1, color2, price;
   
    if(sscanf(params, "s[30]iid", modelid, color1, color2, price)) return SCM(playerid, COLOR_BELGREEN, "* [USAGE]: /createvehicle <ModelID/Vehicle Name> <Color1> <Color2> <Price>");

    if(IsNumeric(modelid)) vehid = strval(modelid);
    else vehid = ReturnVehicleModelID(modelid);

    if(vehid < 400 || vehid > 611) return SCM(playerid, COLOR_ERROR, "* Invalid vehicle model.");

    static Float:x, Float:y, Float:z, Float:angle;
   
    GetPlayerPos(playerid, x, y, z);
    GetPlayerFacingAngle(playerid, angle);

    for (new i = 0; i != MAX_VEH; i ++) if(!VehicleData[i][vehicleExists])
    {
        new query[1000];
       
        new player_interior = GetPlayerInterior(playerid),
        player_virtual_world = GetPlayerVirtualWorld(playerid);

    mysql_format(mysql, query, sizeof(query),
        "INSERT INTO vehicles \
            SET vehicleName='%e', \
                vehicleOwner='-', \
                vehiclePlate='SSRP', \
                vehicleModel=%d, \
                vehiclePrice=%d, \
                vehicleColorOne=%d, \
                vehicleColorTwo=%d, \
                vehicleX=%f, \
                vehicleY=%f, \
                vehicleZ=%f, \
                vehicleA=%f, \
                vehicleInterior=%d, \
                vehicleWorld=%d, \
                vehicleOwned=0"
,

        GetVehicleName(vehid),
        vehid,
        price,
        color1,
        color2,
        x,
        y,
        z,
        angle,
        player_interior,
        player_virtual_world);

    mysql_tquery(mysql, query, "OnVehicleCreate", "dddddffffdd", veh_index, vehid, price, color1, color2, x, y, z, angle, player_interior, player_virtual_world);

        if(!VehicleData[i][vehicleExists]) return 0;
    }
    return 1;
}
But getting undefined symbol "veh_index" at the tquery line.


Re: Vehicle command - Calisthenics - 13.02.2019

Quote:
Originally Posted by ConnorW
Посмотреть сообщение
I didn't read the whole code tho! This is a good way of doing it, but if you look at his code he actually got
pawn Код:
Vehicle_Save(i);

        Vehicle_Refresh(i);
So in my way, I'd go with inserting the cache then just saving it and refreshing which is probably reloading vehicle using these two functions.
I mentioned it in comments that he should create the vehicle at the end and asked what those two functions do. If you just insert a new record, what would be the point to save it again right away? Nothing has changed!

Quote:
Originally Posted by SymonClash
Посмотреть сообщение
Thank you both.

@Calisthenics, this is what Vehicle_Save and Vehicle_Refresh do:

pawn Код:
stock Vehicle_Save(vid)
{
    static query[256];

    format(query, sizeof(query), "UPDATE `vehicles` SET `vehicleOwner` = '%e', `vehicleX` = '%.4f', `vehicleY` = '%.4f', `vehicleZ` = '%.4f', `vehicleA` = '%.4f', `vehicleInterior` = '%d', `vehicleWorld` = '%d' WHERE `vehicleDBID` = '%d'",
        VehicleData[vid][vehicleOwner],
        VehicleData[vid][vehiclePos][0],
        VehicleData[vid][vehiclePos][1],
        VehicleData[vid][vehiclePos][2],
        VehicleData[vid][vehiclePos][3],
        VehicleData[vid][vehicleInterior],
        VehicleData[vid][vehicleWorld],
        VehicleData[vid][vehicleDBID]
    );
    return mysql_tquery(mysql, query);
}
pawn Код:
stock Vehicle_Refresh(vid)
{
    if(vid != -1 && VehicleData[vid][vehicleExists] && VehicleData[vid][vehicleOwned] == 0)
    {
        static string[128];

        if(IsValidDynamic3DTextLabel(VehicleData[vid][vehicleLabel])) DestroyDynamic3DTextLabel(VehicleData[vid][vehicleLabel]);

        VehicleData[vid][vehicleDBID] = CreateVehicle(vid, VehicleData[vid][vehiclePos][0], VehicleData[vid][vehiclePos][1], VehicleData[vid][vehiclePos][2], VehicleData[vid][vehiclePos][3], VehicleData[vid][vehicleColorOne], VehicleData[vid][vehicleColorOne], 10);

        format(string, sizeof(string), "%s - For Sale (%s)", VehicleData[vid][vehicleName], VehicleData[vid][vehiclePrice]);
        VehicleData[vid][vehicleLabel] = CreateDynamic3DTextLabel(string, -1, VehicleData[vid][vehiclePos][0], VehicleData[vid][vehiclePos][1], VehicleData[vid][vehiclePos][2], 15.0, INVALID_VEHICLE_ID, INVALID_PLAYER_ID, 0, VehicleData[vid][vehicleWorld], VehicleData[vid][vehicleInterior]);

        UpdateDynamic3DTextLabelText(VehicleData[vid][vehicleLabel], -1, string);
    }
    return 1;
}
Edit: Now the command is this:

pawn Код:
CMD:createvehicle(playerid, params[])
{
    if(IsPlayerInAnyVehicle(playerid)) return SCM(playerid, COLOR_ERROR, "* You must be on foot to create a vehicle.");
   
    if(Vehicle_Nearest(playerid) != -1) return SCM(playerid, COLOR_ERROR, "* You can't create a vehicle near another one.");
   
    new modelid[30], vehid, color1, color2, price;
   
    if(sscanf(params, "s[30]iid", modelid, color1, color2, price)) return SCM(playerid, COLOR_BELGREEN, "* [USAGE]: /createvehicle <ModelID/Vehicle Name> <Color1> <Color2> <Price>");

    if(IsNumeric(modelid)) vehid = strval(modelid);
    else vehid = ReturnVehicleModelID(modelid);

    if(vehid < 400 || vehid > 611) return SCM(playerid, COLOR_ERROR, "* Invalid vehicle model.");

    static Float:x, Float:y, Float:z, Float:angle;
   
    GetPlayerPos(playerid, x, y, z);
    GetPlayerFacingAngle(playerid, angle);

    for (new i = 0; i != MAX_VEH; i ++) if(!VehicleData[i][vehicleExists])
    {
        new query[1000];
       
        new player_interior = GetPlayerInterior(playerid),
        player_virtual_world = GetPlayerVirtualWorld(playerid);

    mysql_format(mysql, query, sizeof(query),
        "INSERT INTO vehicles \
            SET vehicleName='%e', \
                vehicleOwner='-', \
                vehiclePlate='SSRP', \
                vehicleModel=%d, \
                vehiclePrice=%d, \
                vehicleColorOne=%d, \
                vehicleColorTwo=%d, \
                vehicleX=%f, \
                vehicleY=%f, \
                vehicleZ=%f, \
                vehicleA=%f, \
                vehicleInterior=%d, \
                vehicleWorld=%d, \
                vehicleOwned=0"
,

        GetVehicleName(vehid),
        vehid,
        price,
        color1,
        color2,
        x,
        y,
        z,
        angle,
        player_interior,
        player_virtual_world);

    mysql_tquery(mysql, query, "OnVehicleCreate", "dddddffffdd", veh_index, vehid, price, color1, color2, x, y, z, angle, player_interior, player_virtual_world);

        if(!VehicleData[i][vehicleExists]) return 0;
    }
    return 1;
}
But getting undefined symbol "veh_index" at the tquery line.
Replace:
pawn Код:
mysql_tquery(mysql, query, "OnVehicleCreate", "dddddffffdd", veh_index, vehid, price, color1, color2, x, y, z, angle, player_interior, player_virtual_world);

if(!VehicleData[i][vehicleExists]) return 0;
with:
pawn Код:
mysql_tquery(mysql, query, "OnVehicleCreate", "dddddffffdd", i, vehid, price, color1, color2, x, y, z, angle, player_interior, player_virtual_world);

break;
We need to stop the loop upon sending the threaded query. Also replace:
pawn Код:
/*
    create the vehicle here.
    what do `Vehicle_Save` and `Vehicle_Refresh` do?
*/
with:
pawn Код:
Vehicle_Refresh(veh_index);



Re: Vehicle command - SymonClash - 13.02.2019

Ok compiled without error. Give me a tip. I saw you just added Vehicle_Refresh and not too Vehicle_Save, since i use that function to save vehicle informations, can you suggest me a way to optimize it? Basically i'm using it in this way:

pawn Код:
public OnVehicleMod(playerid, vehicleid, componentid)
{
        if(IsPlayerInVehicle(playerid, VehicleData[vehicleid][vehicleDBID]))
        {
            if(strcmp(VehicleData[vehicleid][vehicleOwner], ReturnName(playerid, 1)))
            {
                for(new x; x < 14; x++)
                {
                    if(GetVehicleComponentType(componentid) == x) VehicleData[vehicleid][vehicleMod][x] = componentid;
                }
                Vehicle_Save(vehicleid);
            }
        }
        return 1;
}
So when a player tunes their vehicle, its component will be saved.

Am i doing it right? I need somehow to optimize Vehicle_Save.


Re: Vehicle command - Calisthenics - 13.02.2019

Yes, I replied to ConnorW that there is no purpose to save when you just insert a new record. No changes.

You need to save what changes only and not everything. If the owner changes the vehicle plate, why would you update 15 columns when only 1 was changed?

About the components, have them in a separate table.


Re: Vehicle command - SymonClash - 13.02.2019

Ok tested the command. The vehicle gets inserted in DB but it doesn't create IG.

It creates just the label that shows "VehicleName - For Sale (Price)".

pawn Код:
stock Vehicle_Refresh(vid)
{
    if(vid != -1 && VehicleData[vid][vehicleExists] && VehicleData[vid][vehicleOwned] == 0)
    {
        static string[128];

        if(IsValidDynamic3DTextLabel(VehicleData[vid][vehicleLabel])) DestroyDynamic3DTextLabel(VehicleData[vid][vehicleLabel]);

        VehicleData[vid][vehicleCreate] = CreateVehicle(vid, VehicleData[vid][vehiclePos][0], VehicleData[vid][vehiclePos][1], VehicleData[vid][vehiclePos][2], VehicleData[vid][vehiclePos][3], VehicleData[vid][vehicleColorOne], VehicleData[vid][vehicleColorOne], 10);

        format(string, sizeof(string), "%s - For Sale (%d)", VehicleData[vid][vehicleName], VehicleData[vid][vehiclePrice]);
        VehicleData[vid][vehicleLabel] = CreateDynamic3DTextLabel(string, -1, VehicleData[vid][vehiclePos][0], VehicleData[vid][vehiclePos][1], VehicleData[vid][vehiclePos][2], 15.0, INVALID_VEHICLE_ID, INVALID_PLAYER_ID, 0, VehicleData[vid][vehicleWorld], VehicleData[vid][vehicleInterior]);

        UpdateDynamic3DTextLabelText(VehicleData[vid][vehicleLabel], -1, string);
    }
    return 1;
}
What's wrong?


Re: Vehicle command - Calisthenics - 13.02.2019

Invalid model id so it returns INVALID_VEHICLE_ID.

`vid` is the index for `VehicleData` variable. You need to pass the modelid in `CreateVehicle` function.
pawn Код:
VehicleData[vid][vehicleCreate] = CreateVehicle(VehicleData[vid][vehicleModel], ...);