Raised This Month: $ Target: $400
 0% 

Very weird 'index out of bounds' error.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 01-02-2009 , 08:53   Very weird 'index out of bounds' error.
Reply With Quote #1

Code:
new pressed[ 5 ], map = 0;     menu_item_getinfo( menu, item, _access, pressed, charsmax( pressed ), name, charsmax( name ), callback );     map = str_to_num( pressed ); // First map starts at Maps[0] and not 1, so we need to substract 1 from the pressed key.     g_iVotes[ map - 1 ]++; // This is where I get the index out of bounds error. g_iVotes have 50 cells.

Thanks in advance, +karma to helpers.
__________________
O o
/¯________________________
| IMMA FIRIN' MAH LAZOR!!!
\_¯¯¯

Last edited by Dores; 01-02-2009 at 09:14.
Dores is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 01-02-2009 , 09:54   Re: Very weird 'index out of bounds' error.
Reply With Quote #2

Show full code.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 01-02-2009 , 17:40   Re: Very weird 'index out of bounds' error.
Reply With Quote #3

pressed can be '0', so you should check the value of 'map' before accessing the array.
__________________

Community / No support through PM
danielkza is offline
Old 01-02-2009, 18:39
Bad_Bud
This message has been deleted by Bad_Bud. Reason: Not sure if I'm right.
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 01-03-2009 , 02:44   Re: Very weird 'index out of bounds' error.
Reply With Quote #4

Code:
#include <amxmodx> #include <amxmisc> #define VERSION "1.0" new const Teams[ 2 ][] = {     "Terrorists",     "Counter-Terrorists" } new p_Deaths, p_Team, p_HUD, p_MaxMaps; new mapsFile[ 62 ], Maps[ 50 ][ 25 ]; new g_iDeaths, g_iVotes[ 50 ], g_iMaxPlayers, g_iMapCount = 0; new bool:g_bVoted[ 33 ]; public plugin_precache() {     get_configsdir( mapsFile, charsmax( mapsFile ) );     format( mapsFile, charsmax( mapsFile ), "%s/maps.ini", mapsFile );     /* Taken from Map Menu */     if( !file_exists( mapsFile ) )         get_cvar_string( "mapcyclefile", mapsFile, charsmax( mapsFile ) );         if( !file_exists( mapsFile ) )         format( mapsFile, charsmax( mapsFile ), "mapcycle.txt" ); } public plugin_init() {     register_plugin( "Map Vote After X Kills", VERSION, "Dores" );         p_Deaths = register_cvar( "mvak_deaths", "20" );     p_HUD = register_cvar( "mvak_hud", "1" );     p_MaxMaps = register_cvar( "mvak_max_maps", "5" );     p_Team = register_cvar( "mvak_team", "1" ); // 1 = Terrorist, 2 = CT, 0 = plugin off.         g_iMaxPlayers = get_maxplayers();     register_event( "DeathMsg", "ev_Death", "a" );         if( !loadMaps() )     {         server_print( "[AMXX]: Can't retrieve maps from file! Turning ^"Map Vote After X Kills^" plugin OFF!" );         return;     } } public ev_Death() {     static iVic ; iVic = read_data( 2 );     static team ; team = get_user_team( iVic );     if( team != get_pcvar_num( p_Team ) )         return PLUGIN_CONTINUE;         new pcvar = get_pcvar_num( p_Deaths );     if( pcvar - ++g_iDeaths <= 0 )     {         g_iDeaths = 0;         startMapVote();         return PLUGIN_CONTINUE;     }         if( get_pcvar_num( p_HUD ) && team == get_pcvar_num( p_Team ) )     {         set_hudmessage( 255, 0, 0, 0.8, 0.28, 0, 6.0, 12.0 );         show_hudmessage( 0, "%s deaths left before map voting: %d", Teams[ team - 1 ], pcvar - g_iDeaths );     }         return PLUGIN_CONTINUE; } public startMapVote() {     new menu = menu_create( "Map Vote", "mapVoteMenu_Handler" );         new i;     for( i = 0 ; i < g_iMapCount ; i++ )     {         menu_additem( menu, Maps[ i ] );     }         menu_setprop( menu, MPROP_EXIT, MEXIT_ALL );         for( i = 1 ; i <= g_iMaxPlayers ; i++ )     {         if( is_user_connected( i ) )         {             menu_display( i, menu );         }     }         return 1; } public mapVoteMenu_Handler( menu, id, item ) {     if( item == MENU_EXIT )     {         g_bVoted[ id ] = true;         menu_destroy( menu );         return PLUGIN_HANDLED;     }         new _access, name[ 32 ], callback;     new pressed[ 5 ], map = 0;     menu_item_getinfo( menu, item, _access, pressed, charsmax( pressed ), name, charsmax( name ), callback );     map = str_to_num( pressed ); // First map starts at Maps[0] and not 1, so we need to substract 1 from the pressed key.     g_iVotes[ map - 1 ]++;     g_bVoted[ id ] = true;     new bool:bAllVoted = true;     for( new i = 1 ; i <= g_iMaxPlayers ; i++ )     {         if( !is_user_connected( i ) )         {             g_bVoted[ i ] = true;             continue;         }                 if( !g_bVoted[ i ] )             bAllVoted = false;     }         if( bAllVoted )     {         new best;         for( new i = 0 ; i < g_iMapCount ; i++ )         {             if( g_iVotes[ i ] > g_iVotes[ best ] )             {                 best = i;             }         }                 client_print( 0, print_chat, "[AMXX]: Map Voting has ended!" );         client_print( 0, print_chat, "Map chosen: %s    Votes for it: %d", Maps[ best ], g_iVotes[ best ] );         client_print( 0, print_chat, "Changing map!" );         set_task( 2.0, "mapChange", best );     }         menu_destroy( menu );     return PLUGIN_HANDLED; } public mapChange( map ) {     server_cmd( "changelevel %s", Maps[ map ] ); } public server_changelevel( map[] ) {     for( new i = 1 ; i <= g_iMaxPlayers ; i++ )     {         if( g_bVoted[ i ] )         {             g_bVoted[ i ] = false;         }     }         for( new i = 0 ; i < g_iMapCount ; i++ )     {         if( g_iVotes[ i ] )         {             g_iVotes[ i ] = 0;         }     } } loadMaps() {     new Map[ 25 ], line = 0, txtlen, size;     size = file_size( mapsFile, 1 );     new bool:bUsedLine[ 50 ], bool:bUsedAll = true;         while( line < size )     {         for( new i = 0 ; i < size ; i++ )         {             if( !bUsedLine[ i ] )                 bUsedAll = false;         }                 if( bUsedAll )             break;                 do         {             line = random( size );         }         while( bUsedLine[ line ] );                 bUsedLine[ line ] = true;                 read_file( mapsFile, line, Map, charsmax( Map ), txtlen );         if( !txtlen || Map[ 0 ] == ';' )             continue;                 format( Maps[ g_iMapCount++ ], charsmax( Maps[] ), "%s", Map );         if( g_iMapCount == get_pcvar_num( p_MaxMaps ) )             break;     }         if( !g_iMapCount )         return 0;         return 1; }
__________________
O o
/¯________________________
| IMMA FIRIN' MAH LAZOR!!!
\_¯¯¯
Dores is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 01-03-2009 , 12:51   Re: Very weird 'index out of bounds' error.
Reply With Quote #5

When you added the items to the menu, you forgot to supply the info[] argument.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 01-03-2009 , 13:05   Re: Very weird 'index out of bounds' error.
Reply With Quote #6

You told me once that that parameter is not needed, but whatever.
Thanks!

EDIT: Doesn't work:
Code:
#include <amxmodx> #include <amxmisc> #define VERSION "1.0" new const Teams[ 2 ][] = {     "Terrorists",     "Counter-Terrorists" } new p_Deaths, p_Team, p_HUD, p_MaxMaps; new mapsFile[ 62 ], Maps[ 50 ][ 25 ]; new g_iDeaths, g_iVotes[ 50 ], g_iMaxPlayers, g_iMapCount = 0; new bool:g_bVoted[ 33 ]; public plugin_precache() {     get_configsdir( mapsFile, charsmax( mapsFile ) );     format( mapsFile, charsmax( mapsFile ), "%s/maps.ini", mapsFile );     /* Taken from Map Menu */     if( !file_exists( mapsFile ) )         get_cvar_string( "mapcyclefile", mapsFile, charsmax( mapsFile ) );         if( !file_exists( mapsFile ) )         format( mapsFile, charsmax( mapsFile ), "mapcycle.txt" ); } public plugin_init() {     register_plugin( "Map Vote After X Kills", VERSION, "Dores" );         p_Deaths = register_cvar( "mvak_deaths", "20" );     p_HUD = register_cvar( "mvak_hud", "1" );     p_MaxMaps = register_cvar( "mvak_max_maps", "5" );     p_Team = register_cvar( "mvak_team", "1" ); // 1 = Terrorist, 2 = CT, 0 = plugin off.         g_iMaxPlayers = get_maxplayers();     register_event( "DeathMsg", "ev_Death", "a" );         if( !loadMaps() )     {         server_print( "[AMXX]: Can't retrieve maps from file! Turning ^"Map Vote After X Kills^" plugin OFF!" );         return;     } } public ev_Death() {     static iVic ; iVic = read_data( 2 );     static team ; team = get_user_team( iVic );     if( team != get_pcvar_num( p_Team ) )         return PLUGIN_CONTINUE;         new pcvar = get_pcvar_num( p_Deaths );     if( pcvar - ++g_iDeaths <= 0 )     {         g_iDeaths = 0;         startMapVote();         return PLUGIN_CONTINUE;     }         if( get_pcvar_num( p_HUD ) && team == get_pcvar_num( p_Team ) )     {         set_hudmessage( 255, 0, 0, 0.8, 0.28, 0, 6.0, 12.0 );         show_hudmessage( 0, "%s deaths left before map voting: %d", Teams[ team - 1 ], pcvar - g_iDeaths );     }         return PLUGIN_CONTINUE; } public startMapVote() {     new menu = menu_create( "Map Vote", "mapVoteMenu_Handler" );         new i, iValue[ 3 ];     for( i = 0 ; i < g_iMapCount ; i++ )     {         format( iValue, charsmax( iValue ), "%d", ( i + 1 ) );         // Also tried: num_to_str( ( i + 1 ), iValue, charsmax( iValue ) ).         menu_additem( menu, Maps[ i ], iValue );     }         menu_setprop( menu, MPROP_EXIT, MEXIT_ALL );         for( i = 1 ; i <= g_iMaxPlayers ; i++ )     {         if( is_user_connected( i ) )         {             menu_display( i, menu );         }     }         return 1; } public mapVoteMenu_Handler( menu, id, item ) {     if( item == MENU_EXIT )     {         g_bVoted[ id ] = true;         menu_destroy( menu );         return PLUGIN_HANDLED;     }         new _access, name[ 32 ], callback;     new pressed[ 5 ], map = 0;     menu_item_getinfo( menu, item, _access, pressed, charsmax( pressed ), name, charsmax( name ), callback );     map = str_to_num( pressed ); // First map starts at Maps[0] and not 1, so we need to substract 1 from the pressed key.     g_iVotes[ map - 1 ]++;     g_bVoted[ id ] = true;     new bool:bAllVoted = true;     for( new i = 1 ; i <= g_iMaxPlayers ; i++ )     {         if( !is_user_connected( i ) )         {             g_bVoted[ i ] = true;             continue;         }                 if( !g_bVoted[ i ] )             bAllVoted = false;     }         if( bAllVoted )     {         new best;         for( new i = 0 ; i < g_iMapCount ; i++ )         {             if( g_iVotes[ i ] > g_iVotes[ best ] )             {                 best = i;             }         }                 client_print( 0, print_chat, "[AMXX]: Map Voting has ended!" );         client_print( 0, print_chat, "Map chosen: %s    Votes for it: %d", Maps[ best ], g_iVotes[ best ] );         client_print( 0, print_chat, "Changing map!" );         set_task( 2.0, "mapChange", best );     }         menu_destroy( menu );     return PLUGIN_HANDLED; } public mapChange( map ) {     server_cmd( "changelevel %s", Maps[ map ] ); } public server_changelevel( map[] ) {     for( new i = 1 ; i <= g_iMaxPlayers ; i++ )     {         if( g_bVoted[ i ] )         {             g_bVoted[ i ] = false;         }     }         for( new i = 0 ; i < g_iMapCount ; i++ )     {         if( g_iVotes[ i ] )         {             g_iVotes[ i ] = 0;         }     } } loadMaps() {     new Map[ 25 ], line = 0, txtlen, size;     size = file_size( mapsFile, 1 );     new bool:bUsedLine[ 50 ], bool:bUsedAll = true;         while( line < size )     {         for( new i = 0 ; i < size ; i++ )         {             if( !bUsedLine[ i ] )                 bUsedAll = false;         }                 if( bUsedAll )             break;                 do         {             line = random( size );         }         while( bUsedLine[ line ] );                 bUsedLine[ line ] = true;                 read_file( mapsFile, line, Map, charsmax( Map ), txtlen );         if( !txtlen || Map[ 0 ] == ';' )             continue;                 format( Maps[ g_iMapCount++ ], charsmax( Maps[] ), "%s", Map );         if( g_iMapCount == get_pcvar_num( p_MaxMaps ) )             break;     }         if( !g_iMapCount )         return 0;         return 1; }
__________________
O o
/¯________________________
| IMMA FIRIN' MAH LAZOR!!!
\_¯¯¯

Last edited by Dores; 01-03-2009 at 13:12.
Dores is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 01-03-2009 , 13:10   Re: Very weird 'index out of bounds' error.
Reply With Quote #7

I probably said it wasn't needed for that situation.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 01-03-2009 , 13:55   Re: Very weird 'index out of bounds' error.
Reply With Quote #8

I think you didn't had the chance to see the edit.
Take a look at my last post.
__________________
O o
/¯________________________
| IMMA FIRIN' MAH LAZOR!!!
\_¯¯¯
Dores is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 01-03-2009 , 14:31   Re: Very weird 'index out of bounds' error.
Reply With Quote #9

Why are you doing (i + 1) when you are just going to do (i - 1) when you retrieve it?
Just set the info to (i) and retrieve it as the same value.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Emp`
AMX Mod X Plugin Approver
Join Date: Aug 2005
Location: Decapod 10
Old 01-03-2009 , 15:27   Re: Very weird 'index out of bounds' error.
Reply With Quote #10

Btw: reason for the current error is that you are passing the map string as the info. You then do str_to_num to the map name, which returns 0. You then try to access an array at -1.
Emp` is offline
Send a message via AIM to Emp` Send a message via MSN to Emp` Send a message via Yahoo to Emp` Send a message via Skype™ to Emp`
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 09:17.


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