GLBasic forum

Main forum => GLBasic - en => Topic started by: Charlie on 2011-May-27

Title: Multi-Dimensional Array Question
Post by: Charlie on 2011-May-27
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 }
Title: Re: Multi-Dimensional Array Question
Post by: Kitty Hello on 2011-May-27
No. Use a loop and DATA segments.
Or: use 1D arrays and multiply the Y component yourself (that's what I do with SPRITE2MEM)
Title: Re: Multi-Dimensional Array Question
Post by: msx on 2011-May-27
I'm very clumsy, could you give an example, Gernot?
Title: Re: Multi-Dimensional Array Question
Post by: backslider on 2011-May-27
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
Title: Re: Multi-Dimensional Array Question
Post by: msx on 2011-May-27
That may help Charlie but not me. What I need is a quick way to fill the array with known values​​.
Title: Re: Multi-Dimensional Array Question
Post by: Charlie on 2011-May-27
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
}
Title: Re: Multi-Dimensional Array Question
Post by: MrTAToad on 2011-May-27
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%]
Title: Re: Multi-Dimensional Array Question
Post by: msx on 2011-May-27
Fantastic, very good solution. One-dimensional array used as a Multi-dimensional array.

Very clever. Thank you.
Title: Re: Multi-Dimensional Array Question
Post by: Moebius on 2011-May-27
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:
Title: Re: Multi-Dimensional Array Question
Post by: msx on 2011-May-27
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:
Title: Re: Multi-Dimensional Array Question
Post by: XanthorXIII on 2011-May-27
Maybe this should a feature that is implemented in a future version of GLBasic?