AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Run time error 4: index out of bounds (https://forums.alliedmods.net/showthread.php?t=293798)

Snitch 02-11-2017 04:19

Run time error 4: index out of bounds
 
Code:

Run time error 4: index out of bounds
menuitem_callbackc

Code:

public menuitem_callbackc( id, menu, item )
{
    if( !g_PlayerSkins[id][ Player[ id ][ PLAYER_SKIN ] ] )
    {
        if( Skin_Info[ item ][ SKIN_KNIFE ] != -1 && !g_PlayerKnives[id][Skin_Info[ item ][ SKIN_KNIFE ]] )
        {
            return ITEM_DISABLED;
        }
        else if(!CheckSkin(id, item))
        {
            return ITEM_DISABLED;
        }
    }
    return ITEM_IGNORE;
}

wat wrong? i did a check != -1

EFFx 02-11-2017 05:36

Re: Run time error 4: index out of bounds
 
We can't help without see what this Skin_Info and g_PlayerKnives means. This error happens because the array's size is less than that check. Show us how you defined they.

Snitch 02-11-2017 06:14

Re: Run time error 4: index out of bounds
 
Code:

new g_PlayerKnives[ MAX_PLAYERS + 1 ][ enumKnifes ];
Code:

enum _:enumKnifes
{
    KNIFE_1
}

Code:

new const Skin_Info[ enumSkins ][ enumSkinInfo ] =
{
    { 0, 0, 0, -1, 3 }
    /*{ 5, 2, 2, KNIFE_5, KNIFE_5 },
    { 10, 3, 3, KNIFE_9, KNIFE_9 },
    { 10, 3, 3, KNIFE_15, KNIFE_15 },*/
}

i enabled only skin 1, other no maybe error come from here?

Black Rose 02-11-2017 19:27

Re: Run time error 4: index out of bounds
 
The problem is not that "Skin_Info[ item ][ SKIN_KNIFE ]" is -1. It is that "item", id, the value of Player[] or Skin_Info[] is negative or larger than the array size.

How to debug your code in 5 minutes:
Print everything.
Code:
public menuitem_callbackc( id, menu, item ) { server_print("menuitem_callbackc(%d, %d, %d) called", id, menu, item);
If that doesn't reveal a problem, you add more messages.
Code:
server_print("Player[id][PLAYER_SKIN]: %d", Player[ id ][ PLAYER_SKIN ]) server_print("g_PlayerSkins[id][...]: %d", g_PlayerSkins[id][ Player[ id ][ PLAYER_SKIN ] ]) server_print("Skin_Info[item][SKIN_KNIFE]: %d", Skin_Info[ item ][ SKIN_KNIFE ]) server_print("g_PlayerKnives[id][...]: %d", g_PlayerKnives[id][Skin_Info[ item ][ SKIN_KNIFE ]])
One of these messages will show an obvious error which you will be able to correct.

If you are still unsure, post the result of those messages here.

Snitch 02-12-2017 08:07

Re: Run time error 4: index out of bounds
 
@Black Rose thank you. it helped

now i have new error:
Quote:

formatted incorrectly - parameter 8 (total 7)
Run time error 25: parameter error
Code:

stock ColorPrint( const index, const string[], any:... )
{
    new szMsg[ 191 ], Players[ 32 ], PNum = 1;
   
    static iLen; iLen = formatex( szMsg, charsmax( szMsg ), "^4[Joiues]^1 ");
   
    vformat( szMsg[ iLen ], charsmax( szMsg ) - iLen, string, 3 );
   
    if ( index )
        Players[ 0 ] = index;
   
    else
        get_players( Players, PNum, "ch" );
   
    for ( new i; i < PNum; i++ )
    {
        if( is_user_connected( Players[ i ] ) )
        {
            message_begin( MSG_ONE_UNRELIABLE, get_user_msgid( "SayText" ), _, Players[ i ] );
           
            write_byte( Players[ i ] );
           
            write_string( szMsg );
           
            message_end( );
        }
    }
   
    return 1;
}

and here same error:
Code:

GetPosition(Client)
{
    static Position, Size, SteamID[32]
    for(Position = 0, Size = ArraySize(g_SteamID); Position < Size; Position++)
    {
        ArrayGetString(g_SteamID, Position, SteamID, 31) // here
       
        if(equal(SteamID, g_Data[Client][DATA_STEAMID]))
        return Position + 1
    }
   
    return 0
}


Black Rose 02-12-2017 08:28

Re: Run time error 4: index out of bounds
 
Most likely you have a line with ColorPrint() where you don't have the same number of format placeholders (%d, %i, %f, %c, %s, %x, %a whatever) and arguments following.
Check out the line that the debug message is pointing at.

And just as a side note, use charsmax() instead of manually entering 31 when working with strings.

Snitch 02-12-2017 09:06

Re: Run time error 4: index out of bounds
 
Quote:

Originally Posted by Black Rose (Post 2494837)
Most likely you have a line with ColorPrint() where you don't have the same number of format placeholders (%d, %i, %f, %c, %s, %x, %a whatever) and arguments following.
Check out the line that the debug message is pointing at.

And just as a side note, use charsmax() instead of manually entering 31 when working with strings.

mhm.

ArrayGetString(g_SteamID, Position, charsmax (SteamID))?

- debug message is pointing at.
its the stock of colorprint

Black Rose 02-12-2017 10:52

Re: Run time error 4: index out of bounds
 
The debug usually points to several things.

Here's an example.

Code:
#include <amxmodx> public plugin_init() {     register_plugin("Test Plugin 1", "1.0", "[ --{-@ ]");         test("%d, %d", 1); } test(const fmt[], any:...) {     new text[128];     vformat(text, charsmax(text), fmt, 2); }

Code:

L 02/12/2017 - 16:49:22: String formatted incorrectly - parameter 3 (total 2)
L 02/12/2017 - 16:49:22: [AMXX] Displaying debug trace (plugin "test1.amxx")
L 02/12/2017 - 16:49:22: [AMXX] Run time error 25: parameter error
L 02/12/2017 - 16:49:22: [AMXX]    [0] test1.sma::test (line 11)
L 02/12/2017 - 16:49:22: [AMXX]    [1] test1.sma::plugin_init (line 6)

As you can see the error message is the same.
It points to line 11 and line 6. Line 11 is where the error occurs but the cause of the problem is at line 6 where I have not supplied enough arguments for the format.

Snitch 02-12-2017 11:39

Re: Run time error 4: index out of bounds
 
@Black_Rose
ok i think the error come from ColorPrint, i will try client_print_color

and can u example about charsmax how
Quote:

ArrayGetString(g_SteamID, Position, charsmax (SteamID))
? give me error

Black Rose 02-12-2017 15:52

Re: Run time error 4: index out of bounds
 
You're missing my point. The function is not the problem. It's how you used it. If you change to client_print_color() you will have the same problem.

You are right, but you just missed to enter SteamID before charsmax(SteamID).


All times are GMT -4. The time now is 20:44.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.