Array, vehicle foreach
#1

Getting this error if a player enters a vehicle that isn't a dmv car, can't figure out the error.

Cheers

Код:
[20:08:24] [debug] Run time error 4: "Array index out of bounds"
[20:08:24] [debug] AMX backtrace:
[20:08:24] [debug] #0 00161a38 in IsDMVCar (vehicleid=131) at gm.pwn:21583
[20:08:24] [debug] #1 00119ae0 in public OnPlayerExitVehicle (playerid=1, vehicleid=131) at gm.pwn:16057

++

[20:05:51] [debug] Run time error 4: "Array index out of bounds"
[20:05:51] [debug] AMX backtrace:
[20:05:51] [debug] #0 00161a38 in IsDMVCar (vehicleid=131) at gm.pwn:21583
[20:05:51] [debug] #1 001178e0 in public OnPlayerEnterVehicle (playerid=1, vehicleid=131, ispassenger=0) at gm.pwn:15872
Код:
stock IsDMVCar(vehicleid)
{
    for(new i=0; i<MAX_VEHICLES; i++)
    {
        if(vehicleid == DMVCar[i]) 
        	return 1;
    }
    return 0;
}
Код:
new DMVCar[6];
Код:
if(IsDMVCar(vehicleid))
    {
    	SendClientMessage(playerid, COLOR_GENERIC_WARNING, "DMV: Type /drivinglicense to take a driving test. [$500]");
    	SendClientMessage(playerid, COLOR_GENERIC_WARNING, "DMV: If you exit the vehicle or fail, you will pay a fee of [$200]");
    }
Код:
DMVCar[0] = CreateVehicle(550, 1270.1439, -1557.7300, 13.3526, 0.0000, 1, 1, -1);
	DMVCar[1] = CreateVehicle(550, 1273.4481, -1557.8203, 13.3526, 0.0000, 1, 1, -1);
	DMVCar[2] = CreateVehicle(550, 1282.9244, -1557.7059, 13.3526, 0.0000, 1, 1, -1);
	DMVCar[3] = CreateVehicle(550, 1286.0720, -1557.7350, 13.3526, 0.0000, 1, 1, -1);
Reply
#2

Your DMVCar array size is 6
and in this
PHP код:
stock IsDMVCar(vehicleid)
{
    for(new 
i=0i<MAX_VEHICLESi++)
    {
        if(
vehicleid == DMVCar[i]) //DMVCar can reach lot more than 6
            
return 1;
    }
    return 
0;

Change it to
PHP код:
stock IsDMVCar(vehicleid)
{
    for(new 
i=0i<6i++)
    {
        if(
vehicleid == DMVCar[i])
            return 
1;
    }
    return 
0;

Reply
#3

Learn to use the sizeof operator. Then it will always be right, even if the size gets changed in the future.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)