SA-MP Forums Archive
Code doesn't work. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Code doesn't work. (/showthread.php?tid=657605)



Code doesn't work. - Michatex - 11.08.2018

Hello.

I have a strange problem, namely the code I put below doesn't work.

Код:
new string[100], vehicleid[2], VehicleText[30];
vehicleid[0] = dini_Int(PlayerAccountPath(playerid), "PrivVeh1");
vehicleid[1] = dini_Int(PlayerAccountPath(playerid), "PrivVeh2");
PlayerData[PlayerData[playerid][SellID]][SellAmount] = strval(inputtext);
PlayerData[PlayerData[playerid][SellID]][SellID] = playerid;
if(PlayerData[playerid][VehicleSell] == 1)
{
	strcat(VehicleText, VehicleNames[vehicleid[0] -400]);
	PlayerData[PlayerData[playerid][SellID]][VehicleSell] = 1;
}
else
{
	strcat(VehicleText, VehicleNames[vehicleid[1] -400]);
	PlayerData[PlayerData[playerid][SellID]][VehicleSell] = 2;
}
CrashDetect detects that:

Код:
[debug] Run time error 4: "Array index out of bounds"
[18:42:59] [debug]  Attempted to read/write array element at negative index -400
[18:42:59] [debug] AMX backtrace:
[18:42:59] [debug] #0 000158f4 in public OnDialogResponse (0, 94, 1, -1, 2249876) from carriage.amx
I am asking you for help.


Re: Code doesn't work. - NaS - 11.08.2018

Looks like vehicleid[0] or vehicleid[1] have the value 0, since you subtract 400 from it and it accesses index -400 which is invalid:

Код:
strcat(VehicleText, VehicleNames[vehicleid[0] -400]);
You should check if the values were successfully loaded before accessing an array with them.

Also, is it a Vehicle ID or a Model ID? Since Models start at 400, and Vehicle IDs at 1. So either way, the ID is invalid.


Re: Code doesn't work. - NeXTGoD - 11.08.2018

I had this error in my gamemode too and solved by this
Код:
VehicleNames[GetVehicleModel(vehicleid[0])-400]);
VehicleNames[GetVehicleModel(vehicleid[1])-400]);
you are trying to get the vehicle name from a model id


Re: Code doesn't work. - Michatex - 11.08.2018

Thanks. It helped.