SA-MP Forums Archive
Array index out of bounds - 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: Array index out of bounds (/showthread.php?tid=624048)



Array index out of bounds - danielpalade - 10.12.2016

I have this command:

Код:
new SelVeh[MAX_PLAYERS][10];

CMD:myvehicles(playerid)
{
	new string[256], count = 0;
	if(CountUserVehicle(playerid) == 0) return SS(playerid, COOL_GREEN, "You don't own any vehicles.", "Nu detii nici o masina.");
	DeletePVar(playerid, "selected_vehicle");
	format(string, sizeof(string), "Name\tStatus\tCarplate\tPrice\n");
	for(new i = 0; i < sizeof(vehicleVariables); i++)
	{
		if(vehicleVariables[i][vVehicleOwnerID] == playerVariables[playerid][pInternalID])
		{
			SelVeh[playerid][count] = vehicleVariables[i][vVehicleID];
			if(vehicleVariables[i][vVehicleStatus] == 0) format(string, sizeof(string), "%s%s\t{%s}[ despawned ]\t%s\t$%s\n", string, VehicleNames[vehicleVariables[i][vVehicleModelID] - 400], selColor[175], vehicleVariables[i][vVehicleNumber], NumberFormat(vehPrice[vehicleVariables[i][vVehicleModelID]]));
			else format(string, sizeof(string), "%s%s\t{%s}[ spawned ]\t%s\t$%s\n", string, VehicleNames[vehicleVariables[i][vVehicleModelID] - 400], selColor[226], vehicleVariables[i][vVehicleNumber], NumberFormat(vehPrice[vehicleVariables[i][vVehicleModelID]]));
			count++;
		}
	}
	ShowPlayerDialog(playerid, DIALOG_DISPLAY_VEHICLES, DIALOG_STYLE_TABLIST_HEADERS, "SERVER: Your Vehicles", string, "Select", "Cancel");
	return 1;
}
Which displays your owned vehicles on the server.
Now whenever I use that command, the dialog works fine, but when I click on the vehicle, I get the error "Array index out of bounds".
This is the dialog response code:

Код:
	if(dialogid == DIALOG_DISPLAY_VEHICLES)
	{
		if(response)
		{
			new x = SelVeh[playerid][listitem];
			SpawnVehicleIfDespawned(x);
			SetPVarInt(playerid, "selected_vehicle", GrabVID(x));
			printf("GrabVID: %d", GrabVID(x));
			format(szMessage, sizeof(szMessage), "%s (%d)", VehicleNames[vehicleVariables[GrabVID(x)][vVehicleModelID] - 400], x);
			ShowPlayerDialog(playerid, DIALOG_VEHICLE_INFO, DIALOG_STYLE_LIST, "test", "Respawn Vehicle\nFind Vehicle\nSet Carplate\nPay Insurance\nSet Price\nUnstuck Vehicle\n{FFD738}Upgrade to VIP{FFFFFF}", "Select", "Cancel");
		}
	}
That is where I get that error.

This is the GrabVID stock:

Код:
stock GrabVID(x)
{
	for(new i = 0; i < MAX_VEHICLES; i++)
	{
		if(vehicleVariables[i][vVehicleID] == x) return i;
	}
	return INVALID_VEHICLE_ID;
}



Re: Array index out of bounds - AroseKhanNiazi - 10.12.2016

vehicleVariables[i][vVehicleID] can you show the place where you created this variable?


Re: Array index out of bounds - X337 - 10.12.2016

Try to replace MAX_VEHICLES in GrabVID functions, with sizeof(vehicleVariables).
Код:
stock GrabVID(x)
{
	for(new i = 0, j = sizeof(vehicleVariables); i < j; i++)
	{
		if(vehicleVariables[i][vVehicleID] == x) return i;
	}
	return INVALID_VEHICLE_ID;
}



Re: Array index out of bounds - danielpalade - 10.12.2016

Quote:
Originally Posted by AroseKhanNiazi
Посмотреть сообщение
vehicleVariables[i][vVehicleID] can you show the place where you created this variable?
No, thats the ID from the database


Re: Array index out of bounds - danielpalade - 10.12.2016

Quote:
Originally Posted by X337
Посмотреть сообщение
Try to replace MAX_VEHICLES in GrabVID functions, with sizeof(vehicleVariables).
Код:
stock GrabVID(x)
{
	for(new i = 0, j = sizeof(vehicleVariables); i < j; i++)
	{
		if(vehicleVariables[i][vVehicleID] == x) return i;
	}
	return INVALID_VEHICLE_ID;
}
But the thing is, i want to return the vehicleid, not the enum id.


Re: Array index out of bounds - danielpalade - 10.12.2016

I fixed it! It was a different stock that caused that problem.