PDA

View Full Version : Array index out of bounds


kesarthakur
17/01/2015, 12:32 PM
[19:56:39] [debug] Run time error 4: "Array index out of bounds"
[19:56:39] [debug] Accessing element at negative index -400
[19:56:39] [debug] AMX backtrace:
[19:56:39] [debug] #0 000544f4 in public cmd_v (10, 18260712) from usf.amx
[19:56:39] [debug] #1 native CallLocalFunction () from samp03svr
[19:56:39] [debug] #2 00006330 in public OnPlayerCommandText (10, 18260680) from usf.amx

Getting this errors in my console

CMD:v(playerid, params[])
{
if (PlayerInfo[playerid][inDM] == 1) return SendClientMessage(playerid, -1, ""RED"ERROR: "GREY"You cannot spawn a vehicle here, please /leave to proceed.");
if( Minigamer_{ playerid } != false && inProgress >= 1) return SendClientMessage( playerid, COLOR_RED, ""RED"ERROR: "GREY"You can't use this command while in Don't Get Wet minigame." );
if(GetPlayerVirtualWorld(playerid) == 1520 || GetPlayerVirtualWorld(playerid) == 1517) return SendClientMessage(playerid, COLOR_GREY, "You can't use that cmd in this zone!");
new Vehicle[32], VehicleID, ColorOne, ColorTwo;
if(sscanf(params, "s[32]D(1)D(1)", Vehicle, ColorOne, ColorTwo))
{
SendClientMessage(playerid, 0x6FFF00FF, "{F07F1D}USAGE: {BBFF00}/v <Vehicle Name>");
return 1;
}

if(GetPlayerVirtualWorld(playerid) != 1518)
{
if(strcmp(Vehicle, "520")==0 || strcmp(Vehicle, "425")==0 || strcmp(Vehicle, "432")==0 || strcmp(Vehicle, "464")==0 ||
strcmp(Vehicle, "476")==0 && GetPlayerVirtualWorld(playerid) == 0) return SendClientMessage(playerid, -1, ""RED"ERROR: "GREY"The vehicle you have specified is globally disabled.");
VehicleID = GetVehicleModelIDFromName(Vehicle);
if(VehicleID != 425 && VehicleID != 432 && VehicleID != 447 &&
VehicleID != 430 && VehicleID != 449 && VehicleID != 476 &&
VehicleID != 520 && VehicleID != 537 && VehicleID != 538 &&
VehicleID != 569 && VehicleID != 570 && VehicleID != 577 &&
VehicleID != 590 && VehicleID != 592 && VehicleID != 610 && VehicleID != 539) {
if(VehicleID == -1 )
{
VehicleID = strval(Vehicle);

if(VehicleID < 400 || VehicleID > 611 )
{
return SendClientMessage(playerid, -1, ""RED"ERROR: "GREY"You have specified an invalid vehicle name!");
}
}

GetPlayerPos(playerid, pX, pY, pZ);
GetPlayerFacingAngle(playerid, pAngle);

DestroyVehicle(PlayerInfo[playerid][pSpawnVehicle]);
PlayerInfo[playerid][pSpawnVehicle] = CreateVehicle(VehicleID, pX, pY, pZ+2.0, pAngle, random(128), random(128), -1);
LinkVehicleToInterior(PlayerInfo[playerid][pSpawnVehicle], GetPlayerInterior(playerid));
SetVehicleVirtualWorld(PlayerInfo[playerid][pSpawnVehicle], GetPlayerVirtualWorld(playerid));
PutPlayerInVehicle(playerid, PlayerInfo[playerid][pSpawnVehicle], 0);
new msg[60];
format(msg, sizeof(msg), "~g~>> ~r~You have spawned ~g~%s", VehicleNames[GetVehicleModelIdFromName(params) - 400]);
TD_MSG( playerid, 3000, msg );
} else {
if(PlayerInfo[playerid][Admin] == 0) return SendClientMessage(playerid, -1, ""RED"ERROR: "GREY"Unauthorized Vehicle.");
GetPlayerPos(playerid, pX, pY, pZ);
GetPlayerFacingAngle(playerid, pAngle);

DestroyVehicle(PlayerInfo[playerid][pSpawnVehicle]);
PlayerInfo[playerid][pSpawnVehicle] = CreateVehicle(VehicleID, pX, pY, pZ+2.0, pAngle, random(128), random(128), -1);
LinkVehicleToInterior(PlayerInfo[playerid][pSpawnVehicle], GetPlayerInterior(playerid));
SetVehicleVirtualWorld(PlayerInfo[playerid][pSpawnVehicle], GetPlayerVirtualWorld(playerid));
PutPlayerInVehicle(playerid, PlayerInfo[playerid][pSpawnVehicle], 0);
new msg[60];
format(msg, sizeof(msg), "~g~>> ~r~You have spawned ~g~%s", VehicleNames[GetVehicleModelIdFromName(params) - 400]);
TD_MSG( playerid, 3000, msg );
}
} else {
SendClientMessage(playerid, -1, ""RED"ERROR: "GREY"You cannot spawn a vehicle in this area.");
}
if(GetPlayerVirtualWorld(playerid) == 1518)
{
VehicleID = GetVehicleModelIDFromName(Vehicle);
if(VehicleID == -1 )
{
VehicleID = strval(Vehicle);

if(VehicleID < 400 || VehicleID > 611 )
{
return SendClientMessage(playerid, -1, ""RED"ERROR: "GREY"You have specified an invalid vehicle name!");
}
}

GetPlayerPos(playerid, pX, pY, pZ);
GetPlayerFacingAngle(playerid, pAngle);

DestroyVehicle(PlayerInfo[playerid][pSpawnVehicle]);
PlayerInfo[playerid][pSpawnVehicle] = CreateVehicle(VehicleID, pX, pY, pZ+2.0, pAngle, ColorOne, ColorTwo, -1);
LinkVehicleToInterior(PlayerInfo[playerid][pSpawnVehicle], GetPlayerInterior(playerid));
SetVehicleVirtualWorld(PlayerInfo[playerid][pSpawnVehicle], GetPlayerVirtualWorld(playerid));
PutPlayerInVehicle(playerid, PlayerInfo[playerid][pSpawnVehicle], 0);
new msg[60];
format(msg, sizeof(msg), "~g~>> ~r~You have spawned ~g~%s", VehicleNames[GetVehicleModelIdFromName(params) - 400]);
TD_MSG( playerid, 3000, msg );
}
return 1;
}


Please Help me to fix this...
Thanks in advance

BroZeus
17/01/2015, 12:55 PM
I am pretty sure the line off error is this -
format(msg, sizeof(msg), "~g~>> ~r~You have spawned ~g~%s", VehicleNames[GetVehicleModelIdFromName(params) - 400]);
As you have used optional specifier in sscanf that means possible inputs could be like these :
/v sultan [params = "sultan"]
/v sultan 154 [params = "sultan 154"]
/v sultan 154 31 [params = "sultan 154 31"]
Now you see first condition is ok but if user enters other two then that will cause a major problem
If u have vehicle model already then why use GetVehicleModelIdFromName(params)..
So use this line -

format(msg, sizeof(msg), "~g~>> ~r~You have spawned ~g~%s", VehicleNames[VehicleID - 400]);
Note : you have used this line at two places so replace the line with line i gave in both two places