Run time error 4: "Array index out of bounds"
#1

Hi all, I have already tried to fix it, but I have no idea what to do anymore so post this

I have a speedometer on my script, that messes everything up. I have an array where I store vehicle names to later use them on a textdraw, and a timer to update speed and damage called every 200 ms aprox.

Quote:

[18:01:07] [debug] Run time error 4: "Array index out of bounds"
[18:01:07] [debug] Attempted to read/write array element at negative index -400
[18:01:07] [debug] AMX backtrace:
[18:01:07] [debug] #0 0003b08c in public Speedometer () from pito.amx

This is what I get spammed on the log.

PHP код:
//ongamemodeinit
SetTimer("Speedometer", 1000, true);
//-------------------------------------------
public Speedometer(playerid)
{
    foreach(
Player, i)
    {
        if(
pInfo[i][Logged] && IsPlayerInAnyVehicle(i))
        {
            new 
string[32], string2[32], string3[10], Float:vhealth;
            
format(string, sizeof(string), "~g~%s", 
                        
VehicleNames[GetVehicleModel(GetPlayerVehicleID(i))-400]); //here is the problem.
            
TextDrawSetString(VehicleName[i], string);
            
format(string2, sizeof(string2), "%d", GetPlayerSpeed(i, 0));
            
TextDrawSetString(VehicleSpeed[i], string2);
            
GetVehicleHealth(GetPlayerVehicleID(i), vhealth);
            
format(string3, sizeof(string3), "~%s~%.0f.0", DamageColor(vhealth), vhealth);
            
TextDrawSetString(VehicleHealth[i], string3);
        }
    }
    return 
1;
} 
And the cars array is at the top,

PHP код:
new VehicleNames[212][] =
{
    
"Landstalker", "Bravura", "Buffalo", "Linerunner", "Perrenial", "Sentinel", "Dumper", "Firetruck",
    
"BF Injection", "Hunter", "Premier", "Enforcer", "Securicar", "Banshee", "Predator", "Bus",
    
"Rhino", "Barracks", "Hotknife", "Article Trailer", "Previon", "Coach", "Cabbie", "Stallion",
    
"Rumpo", "RC Bandit", "Romero", "Packer", "Monster", "Admiral", "Squallo", "Seasparrow",
     
etc............ till all cars are done.
}; 
Any ideas??
Reply
#2

Make your timer be called every 1 second (1000 ms).
Update your foreach to the latest version; The latest one uses different syntax of foreach(new i : Player).
No need to make several arrays (or strings), you can use one array and re-use it.
I made vehicleid variable and stored the ID, so I don't need to use the GetPlayerVehicleID function over and over again.

PHP код:
public Speedometer(playerid)
{
    new 
string[32], vehicleid;
    foreach(new 
i : Player)
    {
        if(!
pInfo[i][Logged]) continue;
        
vehicleid = GetPlayerVehicleID(playerid);
        if(
vehicleid)
        {
            
format(string, sizeof string, "~g~%s", VehicleNames[GetVehicleModel(vehicleid) - 400]);
            
TextDrawSetString(VehicleName[i], string);
            
format(string, sizeof string, "%d", GetPlayerSpeed(i, 0)); 
            
TextDrawSetString(VehicleSpeed[i], string);
            
GetVehicleHealth(vehicleid, vhealth);
            
format(string, sizeof string, "~%s~%.0f.0", DamageColor(vhealth), vhealth);
            
TextDrawSetString(VehicleHealth[i], string);
        }
    }
    return 
1; 
} 
Reply
#3

Yeah that was it basically, thanks man.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)