Multi-Dimensional Array Question

Previous topic - Next topic

Charlie

is it possible to declare multi-dimensional arrays in glbasic like in other languages such as:

array[4][3] = { 1,2,3,4,
              1,2,3,4,
              1,2,3,4 }

Kitty Hello

No. Use a loop and DATA segments.
Or: use 1D arrays and multiply the Y component yourself (that's what I do with SPRITE2MEM)

msx

I'm very clumsy, could you give an example, Gernot?

backslider

Hi, you could fill the arrays also with a loop:

Code (glbasic) Select

LOCAL array[]
DIM array[4][3]

FOR i=0 to 2
    FOR j=0 to 3
        array[i][j] = j
    NEXT
NEXT


But I don´t know if this works for you. :)

cheers

msx

That may help Charlie but not me. What I need is a quick way to fill the array with known values​​.

Charlie

my reason for using arrays this way was for a "visual" for using tile maps. That way you can see a visual of your map using image numbers.
Most languages can do this with arrays.

Example:
image 1 = grass 2 = dirt

array[5][5] = {
1,2,1,1,1
1,2,1,1,1
1,2,2,2,2
1,1,1,1,1
1,1,1,1,1
}

MrTAToad

Quote from: msx on 2011-May-27
I'm very clumsy, could you give an example, Gernot?
Code (glbasic) Select
LOCAL array%[]; DIMDATA array%[],1,2,3,4,1,2,3,4,1,2,3,4

LOCAL row%=1,column%=2

DEBUG array%[(row%*4)+column%]

msx

#7
Fantastic, very good solution. One-dimensional array used as a Multi-dimensional array.

Very clever. Thank you.

Moebius

#8
In these tilemap cases, it's just as neat to use DATA statements in GLB:
Code (glbasic) Select
STARTDATA Level1:
    DATA   1,2,1,1,1
    DATA   1,2,1,1,1
    DATA   1,2,2,2,2
    DATA   1,1,1,1,1
    DATA   1,1,1,1,1
ENDDATA

If you really want a multidimensional "DIMDATA", then submit a feature request, if you think it's more important than finishing off polishing Android support  :good:
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

msx

Quote from: Serpent on 2011-May-27
If you really want a multidimensional "DIMDATA", then submit a feature request, if you think it's more important than finishing off Android support  :good:

I'm already happy, thanks.  :whistle:

XanthorXIII

Maybe this should a feature that is implemented in a future version of GLBasic?
Owlcat has wise