How to set a default value on a 3D/2D array?
#1

Title says it all, is this possible?

pawn Код:
new crap3[ 5 ] = { 0xFF, ... }; // 1D
new crap[ 5 ] [ 5 ] = ?;
new crap2[ 5 ] [ 5 ] [ 5 ] = ?;
meh, better to ask, I don't reckon it is >.>
Reply
#2

looping (OnGameModeInit)

pawn Код:
for(new i, j; i != sizeof array; ++i) {
        for(j = 0; j != sizeof array[]; ++j) {
            array[i][j] = 0;
        }
    }
Reply
#3

Quote:
Originally Posted by Kar
Посмотреть сообщение
looping (OnGameModeInit)

pawn Код:
for(new i, j; i != sizeof array; ++i) {
        for(j = 0; j != sizeof array[]; ++j) {
            array[i][j] = 0;
        }
    }
meh, yeah I knew that already just thought of opening a thread to see if theres a method like setting a value on a 1d array.

thanks anyway kar
Reply
#4

You can initialize multidimensional arrays by proving sub-arrays. Hope you dont mind, i changed the dimensions in the examples out of laziness.

pawn Код:
new crap3[3] = {0xFF, ... }; // 1D
new crap[3][5] = {{0xFF, ...}, {0xFF, ...}, {0xFF, ...}};
new crap2[3][3][5] = {{{0xFF, ...}, {0xFF, ...}, {0xFF, ...}}, {{0xFF, ...}, {0xFF, ...}, {0xFF, ...}}, {{0xFF, ...}, {0xFF, ...}, {0xFF, ...}}};
You can see that initializing arrays can get quite long when you keep adding dimensions. You can use the ellipsis on constant data (like 0xFF), but you cant use it to filling an array with a sub-arrays that also use the ellipsis operator. So the following would be an invalid initialization.

pawn Код:
//BAD
new crap4[3][5] = {{0xFF, ...}, ...};
As long as the data is constant though, you can use the ellipsis operator. An alternative to the approach we took with initializing crap would be filling out the second dimension (the 5 elements) as apposed to filling out the 3 elements and using the ellipsis.

pawn Код:
new crap[3][5] = {{0xFF,0xFF,0xFF,0xFF,0xFF}, ...};

//as apposed to

new crap[3][5] = {{0xFF, ...}, {0xFF, ...}, {0xFF, ...}};

Edit: "Constant" might not be the best term to use in order to explain this, but its kind of difficult to explain, so deal with it haha.
Reply
#5

Thanks!!!!!!!!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)