SA-MP Forums Archive
Run time error 4: "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: Run time error 4: "Array index out of bounds" (/showthread.php?tid=491865)



Run time error 4: "Array index out of bounds" - AntonioCroatia - 01.02.2014

I get this warning with crashdetect:

Quote:

[debug] Run time error 4: "Array index out of bounds"
[16:17:27] [debug] Accessing element at index 65535 past array upper bound 99
[16:17:27] [debug] AMX backtrace:
[16:17:27] [debug] #0 000fa53c in public OnPlayerDeath (0x00000002, 0x0000ffff, 0x000000ff) from PPCT.amx

pawn Код:
public OnPlayerDeath(playerid, killerid, reason)
{
    Delete3DTextLabel(Tekstovi[playerid]);
    if(APlayerData[killerid][PlayerLevel] < 1){
        if(GetPlayerWeapon(killerid) == 38) BanEx(killerid, "Weapon Hack"); //Ban if they have a minigun
        if(GetPlayerWeapon(killerid) == 35) BanEx(killerid, "Weapon Hack"); //Ban if they have a minigun
        if(GetPlayerWeapon(killerid) == 36) BanEx(killerid, "Weapon Hack"); //Ban if they have a minigun
        if(GetPlayerWeapon(killerid) == 37) BanEx(killerid, "Weapon Hack"); //Ban if they have a minigun
    }
    if(APlayerData[playerid][LoggedIn] == false) Kick(playerid);
   
    // Setup local variables
    new VictimName[24], KillerName[24], Msg[128];

    // Clear the missiontext
    TextDrawSetString(PlayerData[playerid][MissionText], " ");
    // Hide the missiontext for this player (when the player is choosing a class, it's not required to show any mission-text)
    TextDrawHideForPlayer(playerid, PlayerData[playerid][MissionText]);

    // Stop any job that may have started
    switch (APlayerData[playerid][PlayerClass])
    {
        case ClassTruckDriver: Trucker_EndJob(playerid);
        case ClassBusDriver: BusDriver_EndJob(playerid);
        case ClassPilot: Pilot_EndJob(playerid);
        case ClassMedic:
            {
                Medic_EndJob(playerid);
                Medic_HealEndJob(playerid);
            }
        case ClassMilitary: Military_EndJob(playerid);
        case ClassPolice: Police_EndJob(playerid);
        case ClassMafia: Mafia_EndJob(playerid);
        case ClassCourier: Courier_EndJob(playerid);
        case ClassAssistance: Assistance_EndJob(playerid);
        case ClassRoadWorker: Roadworker_EndJob(playerid);
        case ClassTashMaster: TashMaster_EndJob(playerid);
        case ClassTaxi: Taxi_EndJob(playerid);
        case ClassFarmer: Farmer_EndJob(playerid);
        case ClassRibar: Ribar_EndJob(playerid);
        case ClassIce: Ice_EndJob(playerid);
        case ClassDiler: Diler_EndJob(playerid);
        case ClassTrain: TrainDriver_EndJob(playerid);
        case ClassSumar: Sumar_EndJob(playerid);
        case ClassFire: Fire_EndJob(playerid);
        case ClassZastitar: Zastitar_EndJob(playerid);
    }

    // If the player is part of a convoy, kick him from it
    Convoy_Leave(playerid);
    GPS_OnPlayerDeath(playerid);
    Spawned[playerid] = false;
   
    // If another player kills you, he'll get an extra star of his wanted level
    if (killerid != INVALID_PLAYER_ID)
    {
        if(PlayerData[killerid][DMKills] == 3) {
            Kick(killerid);
        }
        else if(IsPlayerStreamedIn(playerid, killerid)) {
            PlayerData[killerid][DMKills] = PlayerData[killerid][DMKills] + 1;
            Spawned[playerid] = false;
            // Increase the wanted level of the killer by one star
            SetPlayerWantedLevel(killerid, GetPlayerWantedLevel(killerid) + 1);
            // Get the name of the killed player and the killer
            GetPlayerName(playerid, VictimName, sizeof(VictimName));
            GetPlayerName(killerid, KillerName, sizeof(KillerName));
            // Let the killed know the police are informed about the kill
            format(Msg, 128, "{FF0000}Ubio si igraca {FFFF00}%s{FF0000}. Policija traga za tobom.", VictimName);
            SendClientMessage(killerid, 0xFFFFFFFF, Msg);
            // Inform all police players about the kill
            format(Msg, 128, "{00FF00}Igrač {FFFF00}%s{00FF00} je ubio {FFFF00}%s{00FF00}. Pronadi ga i kazni ga.", KillerName, VictimName);
            Police_SendMessage(Msg);
            {
            SendDeathMessage(killerid, playerid, reason);
            return 1;
            }
        }
    }
    return 1;
}
What's wrong here?


Re: Run time error 4: "Array index out of bounds" - Konstantinos - 01.02.2014

killerid may or may not be a valid (connected) player.

Quote:
Important Note: You MUST check whether killerid is valid (NOT INVALID_PLAYER_ID) before using it in an array, as it will cause the OnPlayerDeath script to crash (not the entire script). This is because INVALID_PLAYER_ID is defined as 65535, and if an array only has 'MAX_PLAYERS' elements, e.g. 500, you're trying to access an index that is above 499, which is out of bounds.

Before using killerid in an array [], you need to check if it's not INVALID_PLAYER_ID (65535).


Re: Run time error 4: "Array index out of bounds" - AntonioCroatia - 01.02.2014

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
killerid may or may not be a valid (connected) player.



Before using killerid in an array [], you need to check if it's not INVALID_PLAYER_ID (65535).
I have to use this:
Код:
	// If another player kills you, he'll get an extra star of his wanted level
	if (killerid != MAX_PLAYERS)
	{
Or?


Re: Run time error 4: "Array index out of bounds" - Konstantinos - 01.02.2014

In your case, MAX_PLAYERS is 100 and INVALID_PLAYER_ID is 65535, so no. It will fail again. The code you had in the first post checks for the last part of it but you miss one:
pawn Код:
if(APlayerData[killerid][PlayerLevel] < 1){
        if(GetPlayerWeapon(killerid) == 38) BanEx(killerid, "Weapon Hack"); //Ban if they have a minigun
        if(GetPlayerWeapon(killerid) == 35) BanEx(killerid, "Weapon Hack"); //Ban if they have a minigun
        if(GetPlayerWeapon(killerid) == 36) BanEx(killerid, "Weapon Hack"); //Ban if they have a minigun
        if(GetPlayerWeapon(killerid) == 37) BanEx(killerid, "Weapon Hack"); //Ban if they have a minigun
    }
is out of that check; thus it gives the run time error. Add it inside that check:
pawn Код:
if (killerid != INVALID_PLAYER_ID)
{
    // here..
}



Re: Run time error 4: "Array index out of bounds" - AntonioCroatia - 01.02.2014

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
In your case, MAX_PLAYERS is 100 and INVALID_PLAYER_ID is 65535, so no. It will fail again. The code you had in the first post checks for the last part of it but you miss one:
pawn Код:
if(APlayerData[killerid][PlayerLevel] < 1){
        if(GetPlayerWeapon(killerid) == 38) BanEx(killerid, "Weapon Hack"); //Ban if they have a minigun
        if(GetPlayerWeapon(killerid) == 35) BanEx(killerid, "Weapon Hack"); //Ban if they have a minigun
        if(GetPlayerWeapon(killerid) == 36) BanEx(killerid, "Weapon Hack"); //Ban if they have a minigun
        if(GetPlayerWeapon(killerid) == 37) BanEx(killerid, "Weapon Hack"); //Ban if they have a minigun
    }
is out of that check; thus it gives the run time error. Add it inside that check:
pawn Код:
if (killerid != INVALID_PLAYER_ID)
{
    // here..
}
Ok, Thanks, Repped +