YolkfolkThe Dizzy Fansite 0 logged in0 playing
Community discussion

Two Dimensional Global Arrays

Started by macon on 9 January 2010 • 3,570 views

6 posts
#54626

I can't work out how to declare a 2 dimensional global array (called a table in gs). I do have a work around but I would have thought there was an easier way of doing it.

Because it needs to be global I have put the following line in gamedef.gs (The name of the array is 'draw')

tab draw;

I want the array to have 20 rows with 5 columns but I only know how to declare the rows which I do at the start of the game.

draw = tab(20);

What do I have to do to get an array of draw(20,5)? draw = tab(20,5) throws an error.

My work around is

draw = tab(20); for (count = 0; count < 20 ; count ++) { draw[count] = {0,0,0,0,0}; }

The reason I need to declare the array rather than generating it on the fly is because the value in any cell can be changed at any time. Not declaring it gives an 'out of bounds' error.

👍 0
#68000
I'll explain how to later when I'm on my computer rather than my blackberry, but if you can't wait til then, RRD uses it to keep track of the scores for each room
👍 0
#68001

this is the code for RRD:

tab roomtab

this is to set all the cells of a 25x18 cell table to 0:

roomtab = tab(25);
	for(i=0;i&lt;25;i+=1)
	{
		roomtab<i>=tab(18);
	}

	for(x=0;x&lt;25;x+=1)
	{
		for(y=0;y&lt;18;y+=1)
		{
			roomtab[x][y]=0;
		}
	}

I hope that helps.

👍 0
#68006
It still uses a loop to create the columns. I wondered if it was possible to declare it with one command like in some other programming languages. Its no big deal as I am happy with my work around which also uses a loop.
👍 0
#68027
no, you can't create it all at once
such a table (matrix) is in fact a list of lists (table of tables)

there is another way, to use a single table (as a list of columns* rows entries) and compute the needed position like this:
my_table = tab( columns*rows )
...
idx = x + y * columns
value = my_table[idx]

and from an index you can get the coordinates back as
x = idx % columns
y = idx / columns

Take care not to abuse tables creation or string operations per frame. It works well if the table is initialized with the proper size at startup and then you just write and read values from it. But creating (allocating) tables and strings each frame is not a good idea and it may influence the frame rate if abused.
👍 0
#68030
I used to have to use that method in my Adventure Game Studio games as its scripying language used to only allow one dimensional arrays.
👍 0