PDA

View Full Version : Help foreach Player


bigtigerbeee
03/12/2013, 11:35 PM
[22:36:08] [debug] Run time error 4: "Array index out of bounds"
[22:36:08] [debug] Accessing element at index 500 past array upper bound 499

I use

foreach(new i: Player)


can I increase MAX_PLAYERS ??

eblood1
03/12/2013, 11:40 PM
Put this at the top of your script
#define MAX_PLAYERS 500

bigtigerbeee
03/12/2013, 11:44 PM
It on a_samp include

If I #define MAX_PLAYERS 501 It wrong

[22:36:08] [debug] Run time error 4: "Array index out of bounds"
[22:36:08] [debug] Accessing element at index 501 past array upper bound 500

eblood1
03/12/2013, 11:52 PM
Show the code where you are getting the error.

bigtigerbeee
04/12/2013, 12:11 AM
public OnVehicleSpawn(vehicleid) {
#if defined DEBUG
printf("[debug] OnVehicleSpawn(%d)", vehicleid);
#endif

switch(GetVehicleModel(vehicleid)) {
case 427, 428, 432, 601, 528: SetVehicleHealth(vehicleid, 5000.0); // Enforcer, Securicar, Rhino, SWAT Tank, FBI truck - this is the armour plating dream come true.
}

if(vehicleid != 509 && vehicleid != 481 && vehicleid != 510)
{
SetVehicleParamsEx(vehicleid, 0, 0, 0, 0, 0, 0, 0);
if(IsPackerCar(vehicleid))
{
if(IsWoodVeh[vehicleid])
{
IsWoodVeh[vehicleid] = 0;
if(IsValidDynamicObject(WoodOnVeh[vehicleid]))
{
DestroyDynamicObject(WoodOnVeh[vehicleid]);
}
}
}
if(IsGasCar(vehicleid))
{
if(IsGasOnVeh[vehicleid])
{
IsGasOnVeh[vehicleid] = 0;
if(IsValidDynamicObject(GasOnVeh[vehicleid]))
{
DestroyDynamicObject(GasOnVeh[vehicleid]);
}
}
}
foreach(new i: Player)
{
if(vehicleid == PlayerInfo[i][pVeh])
{
if(PlayerInfo[i][vLocked]) SetVehicleParamsEx(vehicleid, 0, 0, 0, 1, 0, 0, 0);
ChangeVehiclePaintjob(PlayerInfo[i][pVeh], PlayerInfo[i][vPJ]);
SetVehicleVirtualWorld(PlayerInfo[i][pVeh], PlayerInfo[i][pVehVW]);
for(new x=0; x<14; i++)
{
AddVehicleComponent(PlayerInfo[i][pVeh], PlayerInfo[i][pVehMod][x]);
}
}
if(vehicleid == PlayerInfo[i][pVVeh])
{
if(PlayerInfo[i][vvLocked]) SetVehicleParamsEx(vehicleid, 0, 0, 0, 1, 0, 0, 0);
ChangeVehiclePaintjob(PlayerInfo[i][pVVeh], PlayerInfo[i][vvPJ]);
SetVehicleVirtualWorld(PlayerInfo[i][pVVeh], PlayerInfo[i][pVVehVW]);
for(new x=0; x<14; i++)
{
AddVehicleComponent(PlayerInfo[i][pVVeh], PlayerInfo[i][pVVehMod][x]);
}
}
if(vehicleid == RentCarKey[i])
{
if(RentCarLock[i] == 1) SetVehicleParamsEx(vehicleid, 0, 0, 0, 1, 0, 0, 0);
}
}

}
return 1;
}

eblood1
04/12/2013, 12:28 AM
Are you sure that the problem is foreach?
Use this inside foreach, at the top:

printf("playerid: %i", i);


Make sure you're not using 'MAX_PLAYERS' when declaring these variables:

new IsGasOnVeh[MAX_VEHICLES],
IsWoodVeh[MAX_VEHICLES];

Check this function too, and make sure they're declared with 'MAX_VEHICLES':

IsPackerCar(vehicleid)

Jefff
04/12/2013, 02:12 AM
It looks like

for(new i=0; i <= MAX_PLAYERS; i++)

but must be

for(new i=0; i < MAX_PLAYERS; i++)

eblood1
04/12/2013, 02:16 AM
It looks like

for(new i=0; i <= MAX_PLAYERS; i++)

but must be

for(new i=0; i < MAX_PLAYERS; i++)

He's using foreach, not a standard for-loop for players

Tayab
04/12/2013, 03:23 AM
To increase the size of MAX_PLAYERS you can do like this.


#undef MAX_PLAYERS;
#define MAX_PLAYERS 501

Richie©
04/12/2013, 03:53 AM
for(new x=0; x<14; i++) i++?