PDA

View Full Version : Array index out of bounds?!


Dokins
12/08/2015, 04:23 PM
new VehicleSQLID[MAX_VEHICLES];
new x;
cache_get_row(y, vvid, QueryString);
x = cache_get_row_int(y, vvid);


VehicleSQLID[vehicleid] = x;
printf("VehSQLID %d", x);


It's returning the correct ID in the printf.


[17:28:57] [debug] Run time error 4: "Array index out of bounds"
[17:28:57] [debug] AMX backtrace:
[17:28:57] [debug] #0 001259c8 in public LoadVehicles () at C:\Users\Grant\Dropbox\Los Santos City Roleplay\gamemodes\LSCRP_MYSQL.pwn:18672


I am getting that in the debug.

MarvinPWN
12/08/2015, 04:49 PM
Can you post the LoadVehicles function, please?

Dokins
12/08/2015, 04:50 PM
forward LoadVehicles();
public LoadVehicles()
{
new QueryString[256], count, vehicleid;
//mysql_query( "SELECT id, VehModel,VehOwner, VehColour1, VehColour2, VehPlate, VehSpawnX, VehSpawnY, VehSpawnZ, VehSpawnAngle, VehFuel, VehCash,VehWep1,VehWepA1,VehWep2,VehWepA2, VehArmour,VehFaction,VehFRank, VehOwned, VehFactioned, VehWeed, VehCoke, VehBizID, VehForSale,VehPrice FROM vehicles");
//mysql_store_result();
new x;
new rows, fields;
cache_get_data(rows, fields); // if the rows are more than the size of the variable, reset the rows to match (to avoid run time error 4)
for (new y; y != rows; ++y)
{
cache_get_row(y, vvid, QueryString);
x = cache_get_row_int(y, vvid);
printf("x %d", x);

cache_get_row(y, vVehModel, QueryString);
VehModel[x] = strval( QueryString);

cache_get_row(y, vVehColour1, QueryString);
VehColour1[x] = strval( QueryString);

cache_get_row(y, vVehColour2, QueryString);
VehColour2[x] = strval( QueryString);

cache_get_row(y,vVehSpawnX, QueryString);
VehSpawnX[x] = floatstr( QueryString);

cache_get_row(y,vVehSpawnY, QueryString);
VehSpawnY[x] = floatstr( QueryString);

cache_get_row(y,vVehSpawnZ, QueryString);
VehSpawnZ[x] = floatstr( QueryString);

cache_get_row(y,vVehSpawnAngle, QueryString);
VehSpawnAngle[x] = floatstr( QueryString);

cache_get_row(y,vVehSiren, QueryString);
VehSiren[x] = strval(QueryString);

vehicleid = CreateVehicle(VehModel[x], VehSpawnX[x], VehSpawnY[x],VehSpawnZ[x], VehSpawnAngle[x], VehColour1[x], VehColour2[x], -1, VehSiren[x]);
VehicleSQLID[vehicleid] = x;
VehSiren[vehicleid] = VehSiren[x];
printf("VehSQLID %d", x);

cache_get_row(y,vVehPlate, VehPlate[vehicleid]);
SetVehicleNumberPlate(vehicleid, VehPlate[vehicleid]);

cache_get_row(y,vVehOwner, QueryString);
VehOwner[vehicleid] = strval( QueryString);

cache_get_row(y,vVehJob, QueryString);
VehJob[vehicleid] = strval( QueryString);

cache_get_row(y,vVehFuel, QueryString);
VehFuel[vehicleid] = strval( QueryString);

cache_get_row(y,vVehCash, QueryString);
VehCash[vehicleid] = strval( QueryString);

cache_get_row(y,vVehWeed, QueryString);
VehWeed[vehicleid] = strval( QueryString);

cache_get_row(y,vVehCoke, QueryString);
VehCoke[vehicleid] = strval( QueryString);

cache_get_row(y,vVehLock, QueryString);
VehLock[vehicleid] = strval( QueryString);

cache_get_row(y,vVehAlarm, QueryString);
VehAlarm[vehicleid] = strval( QueryString);

cache_get_row(y,vVehFaction, QueryString);
VehicleFaction[vehicleid] = strval( QueryString);

cache_get_row(y,vVehFRank, QueryString);
VehicleRank[vehicleid] = strval( QueryString);

cache_get_row(y,vVehOwned, QueryString);
VehOwned[vehicleid] = strval( QueryString);

cache_get_row(y,vVehFactioned, QueryString);
VehFactioned[vehicleid] = strval( QueryString);

cache_get_row(y,vVehArmour, QueryString);
VehArmour[vehicleid] = floatstr( QueryString);

cache_get_row(y,vVehBizID, QueryString);
VehBizID[vehicleid] = strval( QueryString);

cache_get_row(y, vVehForSale, QueryString);
VehForSale[vehicleid] = strval( QueryString);

cache_get_row(y,vVehPrice, QueryString);
VehPrice[vehicleid] = strval( QueryString);

cache_get_row(y,vVehWepSlots, QueryString);
VehWepSlots[vehicleid] = strval( QueryString);

cache_get_row(y,vVehFType, QueryString);
VehFType[vehicleid] = strval(QueryString);

cache_get_row(y,vVehSpark, QueryString);
VehSpark[vehicleid] = strval(QueryString);

cache_get_row(y,vVehWater, QueryString);
VehWater[vehicleid] = strval(QueryString);

cache_get_row(y,vVehGear, QueryString);
VehGear[vehicleid] = strval(QueryString);

cache_get_row(y,vVehElec, QueryString);
VehElec[vehicleid] = strval(QueryString);

cache_get_row(y,vVehOil, QueryString);
VehOil[vehicleid] = strval(QueryString);

cache_get_row(y,vVehExhaust, QueryString);
VehExhaust[vehicleid] = strval(QueryString);

cache_get_row(y,vVehFLine, QueryString);
VehFLine[vehicleid] = strval(QueryString);

cache_get_row(y,vVehRadi, QueryString);
VehRadi[vehicleid] = strval(QueryString);

cache_get_row(y,vVehBattery, QueryString);
VehBattery[vehicleid] = strval(QueryString);

cache_get_row(y,vVehFSize, QueryString);
VehFSize[vehicleid] = strval(QueryString);

cache_get_row(y,vVehTyres, QueryString);
VehTyres[vehicleid] = strval(QueryString);

cache_get_row(y,vVehFUsed, QueryString);
VehFUsed[vehicleid] = strval(QueryString);

cache_get_row(y,vVehVMarker, QueryString);
VehVMarker[vehicleid] = strval(QueryString);

cache_get_row(y,vVehFMarker, QueryString);
VehFMarker[vehicleid] = strval(QueryString);

cache_get_row(y,vVehNMarker, QueryString);
VehNMarker[vehicleid] = strval(QueryString);

cache_get_row(y,vVehFSlots, QueryString);
VehFSlots[vehicleid] = strval(QueryString);

cache_get_row(y,vVehHealth, QueryString);
VehHealth[vehicleid] = floatstr(QueryString);

SetVehicleHealth(vehicleid, VehHealth[vehicleid]);
if(VehicleFaction[vehicleid] > 0)
{
new string[12];
format(string, sizeof(string), "%s%d", ShortFName[VehicleFaction[vehicleid]], x);

if(strcmp(string, VehPlate[vehicleid], false))
{
count++;
format(VehPlate[vehicleid], sizeof(VehPlate),"%s%d", ShortFName[VehicleFaction[vehicleid]], x);
MySQL_SetString(x, "VehPlate", VehPlate[vehicleid], "vehicles");
SetVehicleNumberPlate(vehicleid, VehPlate[vehicleid]);
printf("unmatched Vehicle Plates: %d", count);
}

}
if(VehicleFaction[vehicleid] == 0)
{
printf("PLATE %s - not faction", VehPlate[vehicleid]);
SetVehicleNumberPlate(vehicleid, VehPlate[vehicleid]);
}


SetVehicleParamsEx(vehicleid, 0, 0, 0, VehLock[vehicleid], 0, 0, 0);
SetVehicleParamsCarDoors(vehicleid, 0,0,0,0);
SetVehicleParamsCarWindows(vehicleid, 1,1, 1, 1);

if(VehForSale[vehicleid] == 1)
{
new string[128];
new name = VehModel[vehicleid] - 400;
format(string, sizeof(string), "%s, $%s, %s", VehicleNames[name], AddCommas(VehPrice[vehicleid]), VehPlate[vehicleid]);
VehicleLabel[vehicleid] = CreateDynamic3DTextLabel(string, COLOUR_ORANGE, VehSpawnX[vehicleid],VehSpawnY[vehicleid], VehSpawnZ[vehicleid], 100.0, INVALID_PLAYER_ID, vehicleid, 0, 0, -1, -1, 20.0);
Attach3DTextLabelToVehicle(VehicleLabel[vehicleid] ,vehicleid,0.0,0.0,0.0);
}
}
return 1;
}

SickAttack
12/08/2015, 04:54 PM
Why are you using "x" and then "vehicleid" as the slot? Use one only.

Dokins
12/08/2015, 04:57 PM
Because I'm loading the variables into the vehicle that's been created (vehicleid = Createvehicle) x = mysqlid

MarvinPWN
12/08/2015, 04:59 PM
delete

SickAttack
12/08/2015, 05:39 PM
Well, you are probably doing something wrong somewhere.
// [ DEVELOPMENT GAMEMODE ]

// INCLUDES:

#include <a_samp>
#include <a_mysql>

// DEFINES:

// FUNCTIONS:

#define function%0(%1) forward%0(%1); public%0(%1)

// DATABASE:

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

// VEHICLES DATABASE:

#define TABLE_VEHICLES "vehicles"

#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:

// DATABASE:

static vehicles_database;

// VEHICLES:

new vhModel[MAX_VEHICLES],
Float:vhPositionX[MAX_VEHICLES],
Float:vhPositionY[MAX_VEHICLES],
Float:vhPositionZ[MAX_VEHICLES],
Float:vhAngle[MAX_VEHICLES],
vhColor1[MAX_VEHICLES],
vhColor2[MAX_VEHICLES],
vhRespawnDelay[MAX_VEHICLES],
vhSiren[MAX_VEHICLES];

// MAIN:

main()
{
print("Development Mode: 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) != 0)
{
printf("[MySQL] Couldn't connect to %s.", MYSQL_DATABASE);
}
else
{
printf("[MySQL] Connected to %s.", MYSQL_DATABASE);

new query[800], string[144];
format(string, sizeof(string), "CREATE TABLE IF NOT EXISTS %s(", TABLE_VEHICLES);
strcat(query, string);

format(string, sizeof(string), "%s INT(20),", VEHICLES_MODEL);
strcat(query, string);

format(string, sizeof(string), "%s FLOAT(20),", VEHICLES_POSITION_X);
strcat(query, string);

format(string, sizeof(string), "%s FLOAT(20),", VEHICLES_POSITION_Y);
strcat(query, string);

format(string, sizeof(string), "%s FLOAT(20),", VEHICLES_POSITION_Z);
strcat(query, string);

format(string, sizeof(string), "%s FLOAT(20),", VEHICLES_ANGLE);
strcat(query, string);

format(string, sizeof(string), "%s INT(20),", VEHICLES_COLOR_1);
strcat(query, string);

format(string, sizeof(string), "%s INT(20),", VEHICLES_COLOR_2);
strcat(query, string);

format(string, sizeof(string), "%s INT(20),", VEHICLES_RESPAWN_DELAY);
strcat(query, string);

format(string, sizeof(string), "%s INT(20))", VEHICLES_SIREN);
strcat(query, string);

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

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

function 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);

vhModel[vehicleid] = model;
vhPositionX[vehicleid] = x;
vhPositionY[vehicleid] = y;
vhPositionZ[vehicleid] = z;
vhAngle[vehicleid] = angle;
vhColor1[vehicleid] = color_1;
vhColor2[vehicleid] = color_2;
vhRespawnDelay[vehicleid] = respawn_delay;
vhSiren[vehicleid] = siren;
}
return 1;
}

http://i.imgur.com/44n97Nb.png

http://i.imgur.com/TJaM1TS.png

Dokins
12/08/2015, 05:46 PM
Turns out it was to do with using -d2 instead of d3 due to the crash detect update.

SickAttack
12/08/2015, 06:09 PM
So you are all good? Great.

Dokins
12/08/2015, 06:36 PM
Thanks a lot. REP +