AlliedModders

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

mrspeak 11-06-2020 08:17

index out of bounds
 
Hello there,

I'm getting "Index out of bounds" here and i don't get it why.. i think i'm blind...
Code:

if( g_szNumbers[ am_get_player_arena( id ) ] >= 1 && !is_user_bot( id ))

Code:

#pragma semicolon 1
#include <amxmodx>
#include <arena>

#define PLUGIN                " "
#define VERSION                " "
#define AUTHOR                " "

#if AMXX_VERSION_NUM < 183
        #define MAX_PLAYERS 32
#endif

new const g_szNumbers[ ] = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16" };
new gotname[ MAX_PLAYERS ];

public plugin_init()
{
        register_plugin(PLUGIN,VERSION,AUTHOR);
        set_task( 1.0, "arenaname", _, _, _, "b" );
}
public arenaname( id ) {
        if( g_szNumbers[ am_get_player_arena( id ) ] >= 1 && !is_user_bot( id ))
        {
                new sNewName[ MAX_NAME_LENGTH ];
                static szAName[ 32 ];
                if(gotname[ id ] == 0) {
                        //get_user_info(id, "name", szAName, charsmax( szAName ));
                        get_user_name( id, szAName, charsmax(szAName));
                        gotname[ id ] = 1;
                }
                formatex(sNewName, charsmax( sNewName ), "[Arena %s] %s", g_szNumbers[ am_get_player_arena( id ) ], szAName);
                set_user_info(id, "name", sNewName);
               
        }
}


error:
Code:

L 11/06/2020 - 14:16:22: [AMXX] Displaying debug trace (plugin "arenaname.amxx", version " ")
L 11/06/2020 - 14:16:22: [AMXX] Run time error 4: index out of bounds
L 11/06/2020 - 14:16:22: [AMXX]    [0] arenaname.sma::arenaname (line 22)


OciXCrom 11-06-2020 08:19

Re: index out of bounds
 
new gotname[ MAX_PLAYERS + 1 ];

mrspeak 11-06-2020 08:23

Re: index out of bounds
 
Quote:

Originally Posted by OciXCrom (Post 2723928)
new gotname[ MAX_PLAYERS + 1 ];

I already tried that, same error :/

EDIT:
here is the error
Code:

if( g_szNumbers[ am_get_player_arena( id ) ] >= 1 && !is_user_bot( id ))

By.King 11-06-2020 08:59

Re: index out of bounds
 
You should test the incoming number first for error.

public arenaname( id )
{
client_print_color(id, id, "%d", am_get_player_arena(id));
}

mrspeak 11-06-2020 09:40

Re: index out of bounds
 
Quote:

Originally Posted by By.King (Post 2723936)
You should test the incoming number first for error.

public arenaname( id )
{
client_print_color(id, id, "%d", am_get_player_arena(id));
}

Well you can't print am_get_player_arena with %d cuz it's a string.. it prints like "arena%s= arena1" for example..

That's also why i'm using it with g_szNumbers[ ].
I mean... if i print it w/ %s as a string it show the number of the arena.

The plugin it works without problems its just spamming that over and over.
Doesn't that work like that?

By.King 11-06-2020 14:45

Re: index out of bounds
 
You should can do str_to_num(am_get_player_arena(id))

Black Rose 11-07-2020 11:29

Re: index out of bounds
 
Your array is one dimension when it needs to be two (the second one is because your array contains strings, which are also arrays).
The reason you get index out of bounds is this:
Code:
public _am_get_player_arena( iPlugin, iParams ) {     if( iParams != 1 || ( g_iGame == Game_Waiting ) )         return -1;       return g_ePlayerData[ get_param( 1 ) ][ Player_Arena ]; }

Either way, this is (kind of) the right way to do it:
Quote:

Originally Posted by By.King (Post 2723936)
You should test the incoming number first for error.

public arenaname( id )
{
client_print_color(id, id, "%d", am_get_player_arena(id));
}

I would suggest this:
1. On player_putinserver(), using regex to strip away the arena number part (if such exists).
2. Hooking namechange and again using regex, stripping away arena number part (if exists), reset name with arena number.

Don't forget to hook the round start because that's where it is set in the arena plugin. Make sure your plugin is loaded after the arena plugin or add a delay on the register event. Otherwise you will always be 1 round behind.
Code:
register_logevent( "ev_RoundStart", 2, "1=Round_Start" );

Quote:

Originally Posted by mrspeak (Post 2723943)
Well you can't print am_get_player_arena with %d cuz it's a string.. it prints like "arena%s= arena1" for example..

am_get_player_arena() will return an integer between 0 and 15.

fysiks 11-07-2020 15:14

Re: index out of bounds
 
Quote:

Originally Posted by mrspeak (Post 2723943)
Well you can't print am_get_player_arena with %d cuz it's a string.. it prints like "arena%s= arena1" for example..

That's also why i'm using it with g_szNumbers[ ].
I mean... if i print it w/ %s as a string it show the number of the arena.

The plugin it works without problems its just spamming that over and over.
Doesn't that work like that?

Every reference of am_get_player_arena() that I can find shows that it return an integer so printing it with %d is correct. So, g_szNumbers[] is unnecessary.


All times are GMT -4. The time now is 00:32.

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