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;
}
[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) |
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);
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);
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?
*/
}
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 Код:
pawn Код:
|
Vehicle_Save(i);
Vehicle_Refresh(i);
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);
}
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;
}
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;
}
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 Код:
|
Thank you both.
@Calisthenics, this is what Vehicle_Save and Vehicle_Refresh do: pawn Код:
pawn Код:
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;
mysql_tquery(mysql, query, "OnVehicleCreate", "dddddffffdd", i, vehid, price, color1, color2, x, y, z, angle, player_interior, player_virtual_world);
break;
/*
create the vehicle here.
what do `Vehicle_Save` and `Vehicle_Refresh` do?
*/
Vehicle_Refresh(veh_index);
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;
}
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;
}
VehicleData[vid][vehicleCreate] = CreateVehicle(VehicleData[vid][vehicleModel], ...);