Run Time Error 4
#1

Hi all as the title says I have run time error 4 in my script
Код:
Run time error 4: "Array index out of bounds"
[23/05/2015 14:42:32] [debug]  Accessing element at negative index -400
[23/05/2015 14:42:32] [debug] AMX backtrace:
[23/05/2015 14:42:32] [debug] #0 00007970 in ?? (0) from BulgEdition.amx
[23/05/2015 14:42:32] [debug] #1 0000b4f8 in public OnPlayerThings (0) from BulgEdition.amx
Код:
public OnPlayerThings(playerid) {
	new string[500], vehicleid = GetPlayerVehicleID(playerid), Float:vX, Float:vY, Float: vZ;
	GetVehiclePos(vehicleid, vX, vY, vZ);
	format(string, sizeof(string), "Speed: %s~n~Health: %d~n~Fuel: 100/100 l", GetVehicleSpeed(vehicleid, KMPH), GetVehicleHP(vehicleid));
	if(IsBoat(vehicleid) || IsPlane(vehicleid) || IsChopper(vehicleid)) {
		format(string, sizeof(string), "Speed: %s~n~Health: %d~n~Fuel: 100/100 l", GetVehicleSpeed(vehicleid, KNOTS), GetVehicleHP(vehicleid));
	}
	if(IsPlane(vehicleid) || IsChopper(vehicleid)) {
		format(string, sizeof(string), "Speed: %s~n~Health: %d~n~Fuel: 100/100 l~n~Height: %.0f", GetVehicleSpeed(vehicleid, KNOTS), GetVehicleHP(vehicleid), vZ);
	}
	TextDrawSetString(Draw[playerid][2], string);
	format(string, sizeof(string), "%s", GetVehicleName(vehicleid));
	TextDrawSetString(Draw[playerid][1], string);
	return 1;
}
where is the red color there is the problem. IDK what is
there is my stock for the vehicle names:
Код:
stock GetVehicleName(vehicleid) {
	new string[123];
	format(string, sizeof(string), "%s", VehicleNames[(GetVehicleModel(vehicleid) - 400)]);
	return string;
}
Please help. rep+
Reply
#2

What if the player is not in any vehicle so GetPlayerVehicleID returns 0? It will subtract 400 so the index will be -400 (invalid as the valid indexes of the array are 0 to 211).

PHP код:
GetVehicleName(vehicleid)
{
    new 
string[32] = "N/A"modelid GetVehicleModel(vehicleid);
    if (
modelidstrcat((string[0] = EOSstring), VehicleNames[modelid 400], sizeof (string));
    return 
string;

Reply
#3

Thanks. Now I have problem with one cmd. When I use /veh spawn 411(only with id is the problem) In the cosole:
Код:
[23/05/2015 16:36:03] [debug] Run time error 4: "Array index out of bounds"
[23/05/2015 16:36:03] [debug]  Accessing element at index 212 past array upper bound 211
[23/05/2015 16:36:03] [debug] AMX backtrace:
[23/05/2015 16:36:03] [debug] #0 00007a9c in ?? (1104940) from BulgEdition.amx
[23/05/2015 16:36:03] [debug] #1 0000cf24 in public cmd_veh (0, 1093620) from BulgEdition.amx
[23/05/2015 16:36:03] [debug] #2 native CallLocalFunction () from samp-server.exe
[23/05/2015 16:36:03] [debug] #3 00006164 in public OnPlayerCommandText (0, 1093560) from BulgEdition.amx
In-game chat - Unkown cmd. When I use only this format /veh spawn [VehicleID].

the cmd:
Код:
CMD:veh(playerid, params[]) {
	new Option[25], SecOption[128], string[1024];
	if(!IsPlayerConnected(playerid)) return SendClientMessage(playerid, -1, "{E03636}[ERROR]: {FFFFFF}You are not connected to the server. :D Please connect. xD");
	format(string, sizeof(string), "{36E0B9}[USAGE]: {FFFFFF}/veh [option] - Options: Help, Spawn, ID, Name, Color, Engine, Lights, Lock, Trunk,");
	format(string[128], sizeof(string), "Windows, Doors, Speedo, Find, Flip, Fix, Repair, Tune");
	if(sscanf(params, "s[25]S(NULL)[128]", Option, SecOption)) return SendClientMessage(playerid, -1, string), SendClientMessage(playerid, -1, string[128]);
	if(!strcmp(Option, "help", true)) {
		format(string, sizeof(string), "There will be help for the vehicles.");
		ShowPlayerDialog(playerid, DIALOG_VEHICLE_HELP, DIALOG_STYLE_MSGBOX, "Vehicle help center.", string, "Ok", "Close");
	} else if(!strcmp(Option, "spawn", true)) {
		new Vehicle[25], VehicleID, Color[2], RespawnTime, Siren[5], Float:X, Float:Y, Float:Z, Float:A, Float:vX, Float:vY, Float:vZ;
		if(IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, -1, "[WARNING]: You are now in vehicle. Please exit and then spawn new one!");
		format(string, sizeof(string), "{36E0B9}[USAGE]: {FFFFFF}/veh %s [VehicleName/VehicleID] [Color 1 (optional)] [Color 2 (optional)]", Option);
		format(string[128], sizeof(string), "[Respawn time in minutes (optional)] [Siren Yes/No (optional)]");
		if(sscanf(params, "{s[10]}s[25]D(0)D(0)D(0)S(No)[5]", Vehicle, Color[0], Color[1], RespawnTime, Siren)) return SendClientMessage(playerid, -1, string), SendClientMessage(playerid, -1, string[128]);
		VehicleID = GetVehicleIDFromNameEx(Vehicle);
		if('0' <= Vehicle[0] && Vehicle[1] && Vehicle[2] <= '9') {
			VehicleID = strval(Vehicle);
			format(string, sizeof(string), "{E0AE36}[WARNING]: {FFFFFF}Invalid vehicle ID! To find vehicle's ID use \"/veh id\".");
			if(!(400 <= VehicleID <= 611)) return SendClientMessage(playerid, -1, string);
		} else { 
			format(string, sizeof(string), "{E0AE36}[WARNING]: {FFFFFF}Invalid vehicle name! To find vehicle's name use \"/veh name\".");
			if(VehicleID == -1) return SendClientMessage(playerid, -1, string);
		}
		if(!strcmp(Siren, "yes", true)) {
			Siren[4] = 1;
		} else if(!strcmp(Siren, "no", true)) {
			Siren[4] = 0;
		} else return SendClientMessage(playerid, -1, "{36A9E0}[INFO]: {FFFFFF}Add siren: Yes/No.");
		if(IsPlayerInAnyVehicle(playerid)) {
			GetVehicleVelocity(SpawnedVehicle[playerid], vX, vY, vZ);
			GetVehiclePos(SpawnedVehicle[playerid], X, Y, Z);
			GetVehicleZAngle(SpawnedVehicle[playerid], A);
		} else {
			GetPlayerPos(playerid, X, Y, Z);
			GetPlayerFacingAngle(playerid, A);
		}
		DestroyVehicle(SpawnedVehicle[playerid]);
		SpawnedVehicle[playerid] = CreateVehicle(VehicleID, X, Y, Z + 1, A, Color[0], Color[1], RespawnTime * 60, Siren[4]);
		LinkVehicleToInterior(SpawnedVehicle[playerid], GetPlayerInterior(playerid));
		SetVehicleVirtualWorld(SpawnedVehicle[playerid], GetPlayerVirtualWorld(playerid));
		PutPlayerInVehicle(playerid, SpawnedVehicle[playerid], 0);
		SetVehicleVelocity(SpawnedVehicle[playerid], vX, vY, vZ);
		format(string, sizeof(string), "{36A9E0}[INFO]: {FFFFFF}You successfully spawned \"%s\" (ID: %d). Enjoy it!", GetVehicleName(SpawnedVehicle[playerid]), GetVehicleModel(SpawnedVehicle[playerid]));
		SendClientMessage(playerid, -1, string);
	} else return SendClientMessage(playerid, -1, string), SendClientMessage(playerid, -1, string[128]);
	return 1;
}
and I think the problem is from this stock but IDK
Код:
stock GetVehicleIDFromNameEx(vehiclename[]) {
	foreach(i, MAX_PLAYERS) {
		if(strfind(VehicleNamesEx[i], vehiclename, false) != -1)
		return i + 400;
	}
	return -1;
}
Help again, please. Thanks in advice
Reply
#4

Why did you use foreach? It should be a for loop and use the size of VehicleNamesEx array.

pawn Код:
GetVehicleIDFromNameEx(vehiclename[])
{
    for (new i; i != sizeof (VehicleNamesEx); ++i)
    {
        if(strfind(VehicleNamesEx[i], vehiclename, false) != -1) return i + 400;
    }
    return -1;
}
By the way, why do you use VehicleNames and VehicleNamesEx arrays? One would be fine unless you have different vehicle names.
Reply
#5

Konstantinos I have two different functions beacuse of the command and of the speedometer.
Some vehicles has spaces in their names and for the cmd I am using VehicleNamesEx where I have changet the spaces with dots

EDIT: this foreach is not from ******. I made a define for it:
Код:
#define foreach(%1,%2) for(new %1 = 0; %1 < %2; %1++)
Sorry for my bad English
Reply
#6

Still, you loop until MAX_PLAYERS-1 and MAX_PLAYERS is defined as 1000 by default in 0.3.7 when the max valid index for VehicleNameEx array is 211.

pawn Код:
foreach(i, sizeof (VehicleNamesEx))
As there is a reason for the two arrays, okay. Although, I'd still suggest having 1 array and extracting the name to a temporary variable, then replace spaces with dots.
Reply
#7

Can you give me an example, please?
Reply
#8

https://sampforum.blast.hk/showthread.php?tid=362764

pawn Код:
new string[17];
strcat(string, VehicleNames[497-400], sizeof (string));
strreplace(string, " ", ".");
// strreplace(string, character_to_search, character_for_replacement);
print(string);
497 model ID is "Police Maverick" and after the replacement is "Police.Maverick".
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)