AlliedModders

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

Dores 01-02-2009 08:53

Very weird 'index out of bounds' error.
 
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.

Exolent[jNr] 01-02-2009 09:54

Re: Very weird 'index out of bounds' error.
 
Show full code.

danielkza 01-02-2009 17:40

Re: Very weird 'index out of bounds' error.
 
pressed can be '0', so you should check the value of 'map' before accessing the array.

Dores 01-03-2009 02:44

Re: Very weird 'index out of bounds' error.
 
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; }

Exolent[jNr] 01-03-2009 12:51

Re: Very weird 'index out of bounds' error.
 
When you added the items to the menu, you forgot to supply the info[] argument.

Dores 01-03-2009 13:05

Re: Very weird 'index out of bounds' error.
 
You told me once that that parameter is not needed, but whatever.
Thanks! :crab:

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; }

Exolent[jNr] 01-03-2009 13:10

Re: Very weird 'index out of bounds' error.
 
I probably said it wasn't needed for that situation.

Dores 01-03-2009 13:55

Re: Very weird 'index out of bounds' error.
 
I think you didn't had the chance to see the edit.
Take a look at my last post.

Exolent[jNr] 01-03-2009 14:31

Re: Very weird 'index out of bounds' error.
 
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.

Emp` 01-03-2009 15:27

Re: Very weird 'index out of bounds' error.
 
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.


All times are GMT -4. The time now is 09:17.

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