Two Dimensional Global Arrays

Need help with using DizzyAGE then look here
Post Reply
macon
Hard Boiled Egg
Posts: 174
Joined: Mon Apr 30, 2007 1:08 pm
Location: Grimsby, England

Two Dimensional Global Arrays

Post by macon » Sat Jan 09, 2010 11:32 pm

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')

Code: Select all

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.

Code: Select all

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

Code: Select all

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.

delta
Hard Boiled Egg
Posts: 3965
Joined: Fri Feb 09, 2007 7:08 pm
Location: North West
Contact:

Post by delta » Sun Jan 10, 2010 7:06 am

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
Image

Image

"Quotes from the internet may not be genuine" - Abraham Lincoln

delta
Hard Boiled Egg
Posts: 3965
Joined: Fri Feb 09, 2007 7:08 pm
Location: North West
Contact:

Post by delta » Sun Jan 10, 2010 9:27 am

this is the code for RRD:

Code: Select all

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

Code: Select all

	roomtab = tab(25);
	for(i=0;i<25;i+=1)
	{
		roomtab[i]=tab(18);
	}

	for(x=0;x<25;x+=1)
	{
		for(y=0;y<18;y+=1)
		{
			roomtab[x][y]=0;
		}
	}
I hope that helps.
Image

Image

"Quotes from the internet may not be genuine" - Abraham Lincoln

macon
Hard Boiled Egg
Posts: 174
Joined: Mon Apr 30, 2007 1:08 pm
Location: Grimsby, England

Post by macon » Sun Jan 10, 2010 12:48 pm

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.

xelanoimis
Team Yolkfolk
Team Yolkfolk
Posts: 1014
Joined: Sun Jan 21, 2007 6:34 pm
Contact:

Post by xelanoimis » Wed Jan 13, 2010 11:04 am

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.

macon
Hard Boiled Egg
Posts: 174
Joined: Mon Apr 30, 2007 1:08 pm
Location: Grimsby, England

Post by macon » Wed Jan 13, 2010 2:25 pm

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.

Post Reply