Problem with 3D Dimensional array.
#1

Here's the problem, I am trying to loop through a 50 slot 3D Dimensional array, check which slot has no text [empty string] & save a string in it, them loop once again and save a 2nd string, etc.

The first string works fine, however when I try to save a second string into the array, it sends me an error that after looping through the 50 slot, they're all used even tho I only used one.

Here's the code

Код:
new TheArray[MAX_PLAYERS][50][128];


			    for(new i = 0; i < 50; i++)
			    {
					if(strlen(TheArray[giveplayerid][i]) < 1)
					{
					 
						strmid(TheArray[giveplayerid][i], inputtext, 0, 128);
						return 1;
			                }
			                else
			               {
						SCM(playerid, COLOR_GREY, "ERROR: ERROR MESSAGE");
						return 1;
					}
			    }
			    return 1;
It works fine first time, but for the 2nd time, I get the error.
Reply
#2

Of course it will not work. It is always going to check the first index and if the first index is occupied, it will display the error. Hence why it only works once.

You'll have to reconfigure it to fit in your dialog as I've used it in a command:
PHP код:
#undef MAX_PLAYERS
#define MAX_PLAYERS    30
#define MAX_INDEXES    50
new
    
TheArray[MAX_PLAYERS][MAX_INDEXES][128];
CMD:functest(playeridparams[]) {
    if(
isnull(params)) {
        return 
SendClientMessage(playerid, -1"You have to enter something!");
    }
    new
        
emptyIndex 0;
    for(new 
0MAX_INDEXES++) {
        if(!
isnull(TheArray[playerid][i])) {
            
emptyIndex ++;
            continue;
        }
        else {
            
printf("TheArray[playerid][%d] (before strmid): %s"iTheArray[playerid][i]);
            
strmid(TheArray[playerid][i], params0strlen(params));
            
printf("TheArray[playerid][%d]: %s"iTheArray[playerid][i]);
            
emptyIndex 0;
            break;
        }
    }
    if(
emptyIndex != 0) {
        print(
"All indexes are occupied.");
    }
    return 
true;

Also not redefining MAX_PLAYERS extended the compilation time by 6 seconds (from 0.3 seconds in a blank template)! Because: An array of 500 indexes with another 50 cells each and then another 128 cells each. That's 12,800,000 bytes or 12.8 MB.

EDIT: I forgot about the recent increase of MAX_PLAYERS from 500 to 1000, so instead of 12.8MB it's 25MB!
EDIT2: I defined a constant for the maximum cells to make it easier to modify if needed later on.
Reply
#3

Quote:
Originally Posted by AndySedeyn
Посмотреть сообщение
Of course it will not work. It is always going to check the first index and if the first index is occupied, it will display the error. Hence why it only works once.

You'll have to reconfigure it to fit in your dialog as I've used it in a command:
PHP код:
#undef MAX_PLAYERS
#define MAX_PLAYERS    30
#define MAX_INDEXES    50
new
    
TheArray[MAX_PLAYERS][MAX_INDEXES][128];
CMD:functest(playeridparams[]) {
    if(
isnull(params)) {
        return 
SendClientMessage(playerid, -1"You have to enter something!");
    }
    new
        
emptyIndex 0;
    for(new 
0MAX_INDEXES++) {
        if(!
isnull(TheArray[playerid][i])) {
            
emptyIndex ++;
            continue;
        }
        else {
            
printf("TheArray[playerid][%d] (before strmid): %s"iTheArray[playerid][i]);
            
strmid(TheArray[playerid][i], params0strlen(params));
            
printf("TheArray[playerid][%d]: %s"iTheArray[playerid][i]);
            
emptyIndex 0;
            break;
        }
    }
    if(
emptyIndex != 0) {
        print(
"All indexes are occupied.");
    }
    return 
true;

Also not redefining MAX_PLAYERS extended the compilation time by 6 seconds (from 0.3 seconds in a blank template)! Because: An array of 500 indexes with another 50 cells each and then another 128 cells each. That's 12,800,000 bytes or 12.8 MB.

EDIT: I forgot about the recent increase of MAX_PLAYERS from 500 to 1000, so instead of 12.8MB it's 25MB!
EDIT2: I defined a constant for the maximum cells to make it easier to modify if needed later on.
I see, could you please explain what's happening here

Код:
   new 
        emptyIndex = 0; 

    for(new i = 0; i < MAX_INDEXES; i ++) { 

        if(!isnull(TheArray[playerid][i])) { 

            emptyIndex ++; 
            continue; 
        } 
        else { 

            printf("TheArray[playerid][%d] (before strmid): %s", i, TheArray[playerid][i]); 
            strmid(TheArray[playerid][i], params, 0, strlen(params)); 
            printf("TheArray[playerid][%d]: %s", i, TheArray[playerid][i]); 
            emptyIndex = 0; 
            break; 
        }
as this part is kinda confusing me.

Thanks!

EDIT: Nevermind I get it, thanks again.
Reply
#4

The prints were for debug purposes. You can remove them.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)