PDA

View Full Version : InValidVehicleID.


norton2
05/01/2016, 12:40 PM
I did that when a player enters the server, personal vehicles to create. But you can not create more than 5 vehicles! I do not know why.

OnPlayerLogin:
mysql_format(MySQLCon, QuerY, sizeof(QuerY), "SELECT * FROM `vehicles` WHERE `Owner` = %d", pInfo[playerid][pID]);
mysql_pquery(MySQLCon, QuerY, "OnVehiclesLoad", "");

OnVehiclesLoad:
forward OnVehiclesLoad(playerid);
public OnVehiclesLoad(playerid)
{
for(new r = 0; r < cache_num_rows(); ++r, UlIDV++)
{
printf("RR: %d", UlIDV);
new ORM:ormid = Veh[UlIDV][ORM_ID] = orm_create("vehicles");
orm_addvar_int(ormid, Veh[UlIDV][ID], "ID"); //this is the key
orm_setkey(ormid, "ID"); //here we declare it as the key
orm_addvar_int(ormid, Veh[UlIDV][Owner], "Owner");
orm_addvar_int(ormid, Veh[UlIDV][Model], "Model");
orm_addvar_float(ormid, Veh[UlIDV][PosX], "PosX");
orm_addvar_float(ormid, Veh[UlIDV][PosY], "PosY");
orm_addvar_float(ormid, Veh[UlIDV][PosZ], "PosZ");
orm_addvar_float(ormid, Veh[UlIDV][VAngle], "VAngle");
orm_addvar_int(ormid, Veh[UlIDV][Color1], "Color1");
orm_addvar_int(ormid, Veh[UlIDV][Color2], "Color2");
orm_addvar_int(ormid, Veh[UlIDV][Paintjob], "Paintjob");
orm_addvar_int(ormid, Veh[UlIDV][Value], "Value");
orm_addvar_int(ormid, Veh[UlIDV][Lock], "Lock");
orm_addvar_string(ormid, Veh[UlIDV][Plate], 16, "Plate");
orm_addvar_float(ormid, Veh[UlIDV][KM], "KM");
orm_addvar_int(ormid, Veh[UlIDV][Mod0], "Mod0");
orm_addvar_int(ormid, Veh[UlIDV][Mod1], "Mod1");
orm_addvar_int(ormid, Veh[UlIDV][Mod2], "Mod2");
orm_addvar_int(ormid, Veh[UlIDV][Mod3], "Mod3");
orm_addvar_int(ormid, Veh[UlIDV][Mod4], "Mod4");
orm_addvar_int(ormid, Veh[UlIDV][Mod5], "Mod5");
orm_addvar_int(ormid, Veh[UlIDV][Mod6], "Mod6");
orm_addvar_int(ormid, Veh[UlIDV][Mod7], "Mod7");
orm_addvar_int(ormid, Veh[UlIDV][Mod8], "Mod8");
orm_addvar_int(ormid, Veh[UlIDV][Mod9], "Mod9");
orm_addvar_int(ormid, Veh[UlIDV][Mod10], "Mod10");
orm_addvar_int(ormid, Veh[UlIDV][Mod11], "Mod11");
orm_addvar_int(ormid, Veh[UlIDV][Mod12], "Mod12");
orm_addvar_int(ormid, Veh[UlIDV][Mod13], "Mod13");
orm_addvar_int(ormid, Veh[UlIDV][Mod14], "Mod14");
orm_addvar_int(ormid, Veh[UlIDV][Mod15], "Mod15");
orm_addvar_int(ormid, Veh[UlIDV][Mod16], "Mod16");
orm_addvar_string(ormid, Veh[UlIDV][Class], 13, "Class");
orm_apply_cache(ormid, UlIDV);
Veh[UlIDV][ServerID] = CreateVehicle(Veh[UlIDV][Model], Veh[UlIDV][PosX], Veh[UlIDV][PosY], Veh[UlIDV][PosZ], Veh[UlIDV][VAngle], Veh[UlIDV][Color1], Veh[UlIDV][Color2], -1);
}
return 1;
}
UlIDV = The last set ID of a vehicle from the database.

[13:10:36] [debug] Run time error 4: "Array index out of bounds"
[13:10:36] [debug] Accessing element at index 65535 past array upper bound 1999
[13:10:36] [debug] AMX backtrace:
[13:10:36] [debug] #0 0004f104 in public OnVehiclesLoad (1) from GM.amx
Line Error: Veh[UlIDV][ServerID] = CreateVehicle(Veh[UlIDV][Model], Veh[UlIDV][PosX], Veh[UlIDV][PosY], Veh[UlIDV][PosZ], Veh[UlIDV][VAngle], Veh[UlIDV][Color1], Veh[UlIDV][Color2], -1);

enum e_Veh
{
//.....
ServerID,
//.....
};
new Veh[MAX_VEHICLES][e_Veh];

norton2
05/01/2016, 01:05 PM
I did some testing and found that is not extracted properly "Model".
I have 3 personal vehicles in database.
In OnVehiclesLoad:
// code......
printf("RRd: %d", Veh[UlIDV][Model]);
Veh[UlIDV][ServerID] = CreateVehicle(Veh[UlIDV][Model], Veh[UlIDV][PosX], Veh[UlIDV][PosY], Veh[UlIDV][PosZ], Veh[UlIDV][VAngle], Veh[UlIDV][Color1], Veh[UlIDV][Color2], -1);
// code......

.LOG:
RRd: 451
RRd: 495
RRd: 0

norton2
05/01/2016, 02:21 PM
Bump.

kadaradam
05/01/2016, 03:45 PM
the "Veh" array's size is only 1999 which is the defined value of MAX_VEHICLE, and you are trying to access the 65535th element.

by the way 65535 means INVALID_PLAYER_ID or INVALID_VEHICLE_ID. are u sure u are not using the array like this? Veh[INVALID_VEHICLE_ID][...];

norton2
05/01/2016, 04:08 PM
Yes, not use.

kadaradam
05/01/2016, 04:59 PM
Then I would print out the index whenever you want to access the "Veh" veriable to make sure it won't exceed the 1999 value.

Like this:
printf("Veh index: %i", UlIDV);
Veh[UlIDV][ServerID] = CreateVehicle(Veh[UlIDV][Model], Veh[UlIDV][PosX], Veh[UlIDV][PosY], Veh[UlIDV][PosZ], Veh[UlIDV][VAngle], Veh[UlIDV][Color1], Veh[UlIDV][Color2], -1);

norton2
05/01/2016, 05:06 PM
Then I would print out the index whenever you want to access the "Veh" veriable to make sure it won't exceed the 1999 value.

Like this:
printf("Veh index: %i", UlIDV);
Veh[UlIDV][ServerID] = CreateVehicle(Veh[UlIDV][Model], Veh[UlIDV][PosX], Veh[UlIDV][PosY], Veh[UlIDV][PosZ], Veh[UlIDV][VAngle], Veh[UlIDV][Color1], Veh[UlIDV][Color2], -1);

Player 1 has have 6 vehicles, Player 1 Connect:
[17:37:57] Model: 468
[17:37:57] Veh index: 0
[17:37:57] Model: 602
[17:37:57] Veh index: 1
[17:37:57] Model: 411
[17:37:57] Veh index: 2
[17:37:57] Model: 496
[17:37:57] Veh index: 3
[17:37:57] Model: 462
[17:37:57] Veh index: 4
[17:37:57] Model: 463
[17:37:57] Veh index: 5

Player 2 has have 1 vehicles, Player 2 Connect:
[17:38:38] Model: 0
[17:38:38] Veh index: 6
[17:38:38] [debug] Run time error 4: "Array index out of bounds"
[17:38:38] [debug] Accessing element at index 65535 past array upper bound 1999
[17:38:38] [debug] AMX backtrace:
[17:38:38] [debug] #0 0004ed6c in public OnVehiclesLoad (1) from GM.amx

SickAttack
05/01/2016, 05:21 PM
// ** INCLUDES

#include <a_samp>
#include <a_mysql>

// ** DEFINES

// *** DATABASE

// **** GENERAL

#define MYSQL_HOST "localhost"
#define MYSQL_USER "root"
#define MYSQL_PASSWORD ""
#define MYSQL_DATABASE "mysql_vehicles"

// **** TABLES

// ***** VEHICLES

// ****** GENERAL

#define TABLE_VEHICLES "vehicles"

// ****** FIELDS

#define VEHICLES_MODEL "model"
#define VEHICLES_POSITION_X "position_x"
#define VEHICLES_POSITION_Y "position_y"
#define VEHICLES_POSITION_Z "position_z"
#define VEHICLES_ANGLE "angle"
#define VEHICLES_COLOR_1 "color_1"
#define VEHICLES_COLOR_2 "color_2"
#define VEHICLES_RESPAWN_DELAY "respawn_delay"
#define VEHICLES_SIREN "siren"

// ** ARRAYS AND ENUMERATORS

enum eCreatedVehiclesInfo
{
created_vehicle_info_model,
Float:created_vehicle_info_x,
Float:created_vehicle_info_y,
Float:created_vehicle_info_z,
Float:created_vehicle_info_angle,
created_vehicle_info_color_1,
created_vehicle_info_color_2,
created_vehicle_info_respawn,
created_vehicle_info_siren
};

new aCreatedVehiclesInfo[MAX_VEHICLES][eCreatedVehiclesInfo];

// ** VARIABLES

// *** GLOBAL VARIABLES

// **** DATABASE

static vehicles_database;

// ** MAIN

main()
{
print("Loaded \"mysql_vehicles.amx\".");
}

// ** CALLBACKS

public OnGameModeInit()
{
LoadDatabase();
LoadVehicles();
return 1;
}

public OnGameModeExit()
{
return 1;
}

// ** FUNCTIONS

stock LoadDatabase()
{
mysql_log(LOG_ERROR | LOG_WARNING | LOG_DEBUG);
vehicles_database = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DATABASE, MYSQL_PASSWORD);

if(mysql_errno(vehicles_database))
{
print("[MySQL] Couldn't connect to "#MYSQL_DATABASE".");
}
else
{
new query[800];
print("[MySQL] Connected to "#MYSQL_DATABASE".");

strcat(query, "CREATE TABLE IF NOT EXISTS "#TABLE_VEHICLES"(");
strcat(query, ""#VEHICLES_MODEL" INT(20),");
strcat(query, ""#VEHICLES_POSITION_X" FLOAT(20),");
strcat(query, ""#VEHICLES_POSITION_Y" FLOAT(20),");
strcat(query, ""#VEHICLES_POSITION_Z" FLOAT(20),");
strcat(query, ""#VEHICLES_ANGLE" FLOAT(20),");
strcat(query, ""#VEHICLES_COLOR_1" INT(20),");
strcat(query, ""#VEHICLES_COLOR_2" INT(20),");
strcat(query, ""#VEHICLES_RESPAWN_DELAY" INT(20),");
strcat(query, ""#VEHICLES_SIREN" INT(20))");

mysql_query(vehicles_database, query);
}
return 1;
}

stock LoadVehicles()
{
new query[500];
strcat(query, "SELECT `"#VEHICLES_MODEL"`, `"#VEHICLES_POSITION_X"`, `"#VEHICLES_POSITION_Y"`, `"#VEHICLES_POSITION_Z"`, `"#VEHICLES_ANGLE"`, `"#VEHICLES_COLOR_1"`, `"#VEHICLES_COLOR_2"`, `"#VEHICLES_RESPAWN_DELAY"`, `"#VEHICLES_SIREN"` FROM `"#TABLE_VEHICLES"`");
mysql_function_query(vehicles_database, query, true, "OnVehiclesLoaded", "");
return 1;
}

forward OnVehiclesLoaded();
public OnVehiclesLoaded()
{
new vehicleid, model, Float:x, Float:y, Float:z, Float:angle, color_1, color_2, respawn_delay, siren;
for(new i = 0, j = cache_get_row_count(vehicles_database); i < j; i ++)
{
model = cache_get_row_int(i, 0, vehicles_database);
x = cache_get_row_float(i, 1, vehicles_database);
y = cache_get_row_float(i, 2, vehicles_database);
z = cache_get_row_float(i, 3, vehicles_database);
angle = cache_get_row_float(i, 4, vehicles_database);
color_1 = cache_get_row_int(i, 5, vehicles_database);
color_2 = cache_get_row_int(i, 6, vehicles_database);
respawn_delay = cache_get_row_int(i, 7, vehicles_database);
siren = cache_get_row_int(i, 8, vehicles_database);

vehicleid = CreateVehicle(model, x, y, z, angle, color_1, color_2, respawn_delay, siren);

aCreatedVehiclesInfo[vehicleid][created_vehicle_info_model] = model;
aCreatedVehiclesInfo[vehicleid][created_vehicle_info_x] = x;
aCreatedVehiclesInfo[vehicleid][created_vehicle_info_y] = y;
aCreatedVehiclesInfo[vehicleid][created_vehicle_info_z] = z;
aCreatedVehiclesInfo[vehicleid][created_vehicle_info_angle] = angle;
aCreatedVehiclesInfo[vehicleid][created_vehicle_info_color_1] = color_1;
aCreatedVehiclesInfo[vehicleid][created_vehicle_info_color_2] = color_2;
aCreatedVehiclesInfo[vehicleid][created_vehicle_info_respawn] = respawn_delay;
aCreatedVehiclesInfo[vehicleid][created_vehicle_info_siren] = siren;
}
return 1;
}

norton2
05/01/2016, 05:24 PM
// ** INCLUDES

#include <a_samp>
#include <a_mysql>

// ** DEFINES

// *** DATABASE

// **** GENERAL

#define MYSQL_HOST "localhost"
#define MYSQL_USER "root"
#define MYSQL_PASSWORD ""
#define MYSQL_DATABASE "mysql_vehicles"

// **** TABLES

// ***** VEHICLES

// ****** GENERAL

#define TABLE_VEHICLES "vehicles"

// ****** FIELDS

#define VEHICLES_MODEL "model"
#define VEHICLES_POSITION_X "position_x"
#define VEHICLES_POSITION_Y "position_y"
#define VEHICLES_POSITION_Z "position_z"
#define VEHICLES_ANGLE "angle"
#define VEHICLES_COLOR_1 "color_1"
#define VEHICLES_COLOR_2 "color_2"
#define VEHICLES_RESPAWN_DELAY "respawn_delay"
#define VEHICLES_SIREN "siren"

// ** VARIABLES

// *** GLOBAL VARIABLES

// **** DATABASE

static vehicles_database;

// **** VEHICLES

new gvhModel[MAX_VEHICLES],
Float:gvhPositionX[MAX_VEHICLES],
Float:gvhPositionY[MAX_VEHICLES],
Float:gvhPositionZ[MAX_VEHICLES],
Float:gvhAngle[MAX_VEHICLES],
gvhColor1[MAX_VEHICLES],
gvhColor2[MAX_VEHICLES],
gvhRespawnDelay[MAX_VEHICLES],
gvhSiren[MAX_VEHICLES];

// ** MAIN

main()
{
print("Loaded \"mysql_vehicles.amx\".");
}

// ** CALLBACKS

public OnGameModeInit()
{
LoadDatabase();
LoadVehicles();
return 1;
}

public OnGameModeExit()
{
return 1;
}

// ** FUNCTIONS

stock LoadDatabase()
{
mysql_log(LOG_ERROR | LOG_WARNING | LOG_DEBUG);
vehicles_database = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DATABASE, MYSQL_PASSWORD);

if(mysql_errno(vehicles_database))
{
print("[MySQL] Couldn't connect to "#MYSQL_DATABASE".");
}
else
{
new query[800];
print("[MySQL] Connected to "#MYSQL_DATABASE".");

strcat(query, "CREATE TABLE IF NOT EXISTS "#TABLE_VEHICLES"(");
strcat(query, ""#VEHICLES_MODEL" INT(20),");
strcat(query, ""#VEHICLES_POSITION_X" FLOAT(20),");
strcat(query, ""#VEHICLES_POSITION_Y" FLOAT(20),");
strcat(query, ""#VEHICLES_POSITION_Z" FLOAT(20),");
strcat(query, ""#VEHICLES_ANGLE" FLOAT(20),");
strcat(query, ""#VEHICLES_COLOR_1" INT(20),");
strcat(query, ""#VEHICLES_COLOR_2" INT(20),");
strcat(query, ""#VEHICLES_RESPAWN_DELAY" INT(20),");
strcat(query, ""#VEHICLES_SIREN" INT(20))");

mysql_query(vehicles_database, query);
}
return 1;
}

stock LoadVehicles()
{
new query[500];
strcat(query, "SELECT `"#VEHICLES_MODEL"`, `"#VEHICLES_POSITION_X"`, `"#VEHICLES_POSITION_Y"`, `"#VEHICLES_POSITION_Z"`, `"#VEHICLES_ANGLE"`, `"#VEHICLES_COLOR_1"`, `"#VEHICLES_COLOR_2"`, `"#VEHICLES_RESPAWN_DELAY"`, `"#VEHICLES_SIREN"` FROM `"#TABLE_VEHICLES"`");
mysql_function_query(vehicles_database, query, true, "OnVehiclesLoaded", "");
return 1;
}

forward OnVehiclesLoaded();
public OnVehiclesLoaded()
{
new vehicleid, model, Float:x, Float:y, Float:z, Float:angle, color_1, color_2, respawn_delay, siren;
for(new i = 0, j = cache_get_row_count(vehicles_database); i < j; i ++)
{
model = cache_get_row_int(i, 0, vehicles_database);
x = cache_get_row_float(i, 1, vehicles_database);
y = cache_get_row_float(i, 2, vehicles_database);
z = cache_get_row_float(i, 3, vehicles_database);
angle = cache_get_row_float(i, 4, vehicles_database);
color_1 = cache_get_row_int(i, 5, vehicles_database);
color_2 = cache_get_row_int(i, 6, vehicles_database);
respawn_delay = cache_get_row_int(i, 7, vehicles_database);
siren = cache_get_row_int(i, 8, vehicles_database);

vehicleid = CreateVehicle(model, x, y, z, angle, color_1, color_2, respawn_delay, siren);

gvhModel[vehicleid] = model;
gvhPositionX[vehicleid] = x;
gvhPositionY[vehicleid] = y;
gvhPositionZ[vehicleid] = z;
gvhAngle[vehicleid] = angle;
gvhColor1[vehicleid] = color_1;
gvhColor2[vehicleid] = color_2;
gvhRespawnDelay[vehicleid] = respawn_delay;
gvhSiren[vehicleid] = siren;
}
return 1;
}

No, you did not understood.

SickAttack
05/01/2016, 05:29 PM
No, you did not understood.

But you can not create more than 5 vehicles!

What I understood is that you are having problems with loading more than 5 vehicles, use what I posted as a reference.

norton2
05/01/2016, 05:32 PM
What I understood is that you are having problems with loading more than 5 vehicles, use what I posted as a reference.

That's how it is. I do not want to give me another system! I want to fix mine.

SickAttack
05/01/2016, 05:37 PM
That's how it is. I do not want to give me another system! I want to fix mine.

And how is that working out for you? :p

Yeah, but seriously, update your code and use the latest MySQL plugin by BlueG (https://github.com/pBlueG/SA-MP-MySQL/releases) if it does not already use it, then use my code as a reference (modify the functions you use, etc.).

P.S. Scrap out that "orm" deal, use an array instead.

Array index out of bounds cause: http://forum.sa-mp.com/showpost.php?p=3626585&postcount=4, http://forum.sa-mp.com/showpost.php?p=3629843&postcount=5

kadaradam
05/01/2016, 07:02 PM
using the normal mysql functions would be better, yeah.

anyway u have to pass the playerid parameter to your function when you start the threading.

mysql_format(MySQLCon, QuerY, sizeof(QuerY), "SELECT * FROM `vehicles` WHERE `Owner` = %d", pInfo[playerid][pID]);
mysql_pquery(MySQLCon, QuerY, "OnVehiclesLoad", "i", playerid);

i never used orm, but dont u have to close the orm after u reterived the data to avoid memory leak?http://wiki.sa-mp.com/wiki/MySQL/R33#orm_destroy