Raised This Month: $51 Target: $400
 12% 

index out of bounds


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
mrspeak
Junior Member
Join Date: Apr 2020
Old 11-06-2020 , 08:17   index out of bounds
Reply With Quote #1

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)

Last edited by mrspeak; 11-06-2020 at 08:24.
mrspeak is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 11-06-2020 , 08:19   Re: index out of bounds
Reply With Quote #2

new gotname[ MAX_PLAYERS + 1 ];
__________________

Last edited by OciXCrom; 11-06-2020 at 08:19.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
mrspeak
Junior Member
Join Date: Apr 2020
Old 11-06-2020 , 08:23   Re: index out of bounds
Reply With Quote #3

Quote:
Originally Posted by OciXCrom View Post
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 ))

Last edited by mrspeak; 11-06-2020 at 08:26.
mrspeak is offline
By.King
Member
Join Date: Jan 2019
Old 11-06-2020 , 08:59   Re: index out of bounds
Reply With Quote #4

You should test the incoming number first for error.

public arenaname( id )
{
client_print_color(id, id, "%d", am_get_player_arena(id));
}
By.King is offline
mrspeak
Junior Member
Join Date: Apr 2020
Old 11-06-2020 , 09:40   Re: index out of bounds
Reply With Quote #5

Quote:
Originally Posted by By.King View Post
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?
mrspeak is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-07-2020 , 15:14   Re: index out of bounds
Reply With Quote #6

Quote:
Originally Posted by mrspeak View Post
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.
__________________
fysiks is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 11-07-2020 , 11:29   Re: index out of bounds
Reply With Quote #7

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 View Post
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 View Post
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.
__________________

Last edited by Black Rose; 11-08-2020 at 06:28.
Black Rose is offline
By.King
Member
Join Date: Jan 2019
Old 11-06-2020 , 14:45   Re: index out of bounds
Reply With Quote #8

You should can do str_to_num(am_get_player_arena(id))
By.King is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 23:56.


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