AlliedModders

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

Croxeye. 12-01-2018 12:34

2d array index out of bounds
 
PHP Code:

enum _:playerTable {
    
nick[32],
    ...
}

new 
g_PlayerData[33][playerTable];

get_user_name(idg_PlayerData[id][nick], charsmax(g_PlayerData[][nick])); 

Code:

error 032: array index out of bounds (variable "g_PlayerData")

HamletEagle 12-01-2018 13:06

Re: 2d array index out of bounds
 
It compiles fine for me.

Bugsy 12-01-2018 14:14

Re: 2d array index out of bounds
 
What you posted is correct as HamletEagle says. But I see you have '...' after nick so there may be something you removed that is causing the error. You need to post full code if you want to get an answer.

Croxeye. 12-01-2018 17:56

Re: 2d array index out of bounds
 
PHP Code:

enum _:playerTable {
    
nick[32],
    
steamId[45],
    
password[45],
    
language[3],
    
boolisOnline false,
    
boolisVip false,
    
boolisAdmin false
}

new 
g_PlayerData[33][playerTable];

public 
client_connect(id) {
    new 
Query[512], Data[1];
    
    
Data[0] = id;
    
    
get_user_name(idg_PlayerData[id][nick], charsmax(g_PlayerData[][nick])); //error
    
    
formatex(Querycharsmax(Query), "SELECT * FROM `player` WHERE `player`.`nick` = '%s'"g_PlayerData[id][nick]);
    
SQL_ThreadQuery(MySQL_GlobalConnection"MySQL_LoadPlayer"QueryData1);
}

public 
MySQL_LoadPlayer(FailState,Handle:SQL_Query,Error[],Errcode,Data[],DataSize) {
    if(
FailState == TQUERY_CONNECT_FAILED) {
        
console_print(0"Load - Could not connect to SQL database.  [%d] %s"ErrcodeError);
    } else if(
FailState == TQUERY_QUERY_FAILED) {
        
console_print(0"Load Query failed. [%d] %s"ErrcodeError);
    }
    
    new 
playerId;
    
    
playerId Data[0];
    
    
console_print(0"playerId: %i"playerId); // prints: "playerId: 0"
    
    
    
if(SQL_NumResults(SQL_Query) != 0) {
        
SQL_ReadResult(SQL_Query1g_player[playerId][nick], charsmax(g_player[][nick])); //error
        
SQL_ReadResult(SQL_Query2g_player[playerId][steamId], charsmax(g_player[][steamId])); //error
        
SQL_ReadResult(SQL_Query3g_player[playerId][password], charsmax(g_player[][password])); //error
                
SQL_ReadResult(SQL_Query3g_player[playerId][language], charsmax(g_player[][language])); //error
        
g_player[playerId][bcoins] = SQL_ReadResult(SQL_Query6); //error
        
g_player[playerId][isOnline] = true;
    }

        
SQL_FreeHandle(SQL_Query);



Black Rose 12-01-2018 18:24

Re: 2d array index out of bounds
 
When I removed "= false" from the three booleans it worked.
Nothing actually wrong, so I'm gonna guess the compiler has a hard time interpreting in this case.
Enums are always a bit weird, keep them as simple as possible.
Booleans are always initialized as false.

Code:
enum playerTable {     nick[32],     steamId[45],     password[45],     language[3],     bool:isOnline,     bool:isVip,     bool:isAdmin,     bcoins }

Croxeye. 12-01-2018 18:37

Re: 2d array index out of bounds
 
Thank you so much, now it works...

Bugsy 12-01-2018 18:58

Re: 2d array index out of bounds
 
That was definitely the problem. The enumerator is really just a 'sizer' for an array so setting it to false does not make sense. The enum provides you offsets based on items preceding it, making it easy and fast to work with constants/variable-names to access items in an array. Setting to false for something would need to be done on the g_PlayerData[] array that was sized by playerTable.

The enum items in playerTable hold the below values. If the compiler allowed this, doing "isOnline = false" would change its value from 125 to 0 which essentially would over-write data where 'nick' is stored.

nick=0
steamID=32
password=77
language=122
isOnline=125
isVip=126
isAdmin=127
bcoins = 128

Croxeye. 12-01-2018 19:00

Re: 2d array index out of bounds
 
Just one more question: How to get number of not empty rows in array ?

PHP Code:

new achievements[50][achievementsTable]; //But I only have 5 achievements

for(new 0sizeof(achievements); i++) {
    new 
InfoStatus[198];
    
formatex(InfoStatuscharsmax(InfoStatus), "\yAchievements:");
    
    new 
menu menu_create(InfoStatus"achievements_menu_handler");
    
    for(new 
0sizeof(achievements); i++) {
        
formatex(InfoStatuscharsmax(InfoStatus),  "\w%s \r:: \r%i \y%s"achievements[i][name], achievements[i][rewardAmount], SYMBOL);

        
menu_additem(menuInfoStatus"0"0);
    }
    
    
menu_setprop(menuMPROP_EXITMEXIT_ALL);
    
menu_display(idmenu0);



Bugsy 12-01-2018 19:04

Re: 2d array index out of bounds
 
Change achievements[50][achievementsTable] to achievements[5][achievementsTable]? Or do you mean the specific player?

If it's the player who only has 5, whatever stores his achievements, do a if ( player variable ) { add to menu }

Croxeye. 12-01-2018 19:12

Re: 2d array index out of bounds
 
No, no.. I'm getting achievements from database and I should change [50] to [] because I dont know number of achiements and I always can add more..


All times are GMT -4. The time now is 07:35.

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