2d arrays....

Started by Squinky, Thu 13/11/2003 03:47:18

Previous topic - Next topic

Squinky

Okay, I've heard about this whole array thing for a while and would like to learn some more. I work with this old dude that used to program with something called r-tran or something and he's pretty much laid out the concept for me. But I would'nt mind seeing an example of how they would be used in ags....

Now If I remember right, this isn't officially supported...and I think I heard once that if its done wrong it could screw up a game pretty bad...or maybe I'm retarded, who knows...

So anybody wanna show a quick little example?

Gilbert

"Real" 2D array is not possible, but you can fake one via a 1D array.

Eg you want to have a "2D" array of 5*4 integers, just define:
int blah[19]; //5*4=20, this defines elements #0 thru #19

When you want to refer to element, say [2][3], just use blah[11] (13=2*4+3). In general if you want to refer to element [a], just use blah[a*4+b]. (that "4" is because there are 4 possible numbers in the 2nd index)

To find a and b back from c of blah[c], just do:
a=c/4;
b=c-(a*4);

Eg, for blah[11], it refer to element [2][3] of the "2D" array, because:
2=11/4; //note integer division
3=11-(2*4);

After

Remember, Gilbot is using indices from 0..n-1 for the a and b values as well.

I believe the old dude was talking about Fortran, which is very much a living language.

As for uses, apart from the obvious spatial correspondence, they can be used to store relationships.
Example, you have 3 characters, related as follows:

Jane loves Mary
Jane fears Eric
Mary hates Jane
Mary loves Eric
Eric knows Jane
Eric fears Mary

So,
Code: ags

_____[col]___0_____1_____2___
row]...... Jane: Mary: Eric:
0___ Jane: ----- hates knows
1___ Mary: loves ----- fears
2___ Eric: fears loves -----

and col rel[col*3+row] row describes it.
Just be sure to remember which way around you are doing it.

MrColossal

* MrColossal fears Mary!
"This must be a good time to live in, since Eric bothers to stay here at all"-CJ also: ACHTUNG FRANZ!

Gilbert

;D

Hey! That example is not realistic, there should be relation among the same person:

Eric worships Eric!

After

 ;D At least it's a variable.

Pumaman

Quote from: Gilbot V7000a on Thu 13/11/2003 04:13:06
Eg you want to have a "2D" array of 5*4 integers, just define:
int blah[19]; //5*4=20, this defines elements #0 thru #19

Just to be clear, that should be:

int blah[20];

AGS does not do array bounds checking, so declaring the array too small can lead to strange errors.

The easiest way to access the "2D" array would be to create two functions:

function getBlah(int x, int y) {
 return blah[y * 5 + x];
}
function setBlah(int x, int y, int value) {
 blah[y * 5 + x] = value;
}

and then use them in your script.

Paper Carnival

hmm, I think your solution is not the best Gillbot (but maybe I didn't quite get what you mean)

var[2*(1+1)] would be exactly the same to var[4*(0+1)]

Squinky

#8
Okay, thanks everybody. I think I'm getting it now. I tried out a little tester game that didn't work though.

Global Script

int blah[20];

function getBlah(int x, int y) {
return blah[y * 5 + x];
}
function setBlah(int x, int y, int value) {
blah[y * 5 + x] = value;
}
Script Header

/**/import function getBlah();
/**/import function setBlah();

room script

look at hotspot 1

setBlah(4,3,5);  

look at hotspot 2

if (getBlah(4,3)==5){
DisplaySpeech (EGO,"Help!");
}
else if (getBlah (4,3)==>5){
DisplaySpeech (EGO,"Its not right yet");  
}
}

Now, when I try to compile, it's telling me I have the wrong number of parameters in getBlah.
1.Is this even the right way of doing it?
2.Do I need to import the int blah into the script header?
3.Am I even defining int blah [20]; in the right place?
4. And should I define all the positions values somewhere, or do they all start at 0?

Thanks again for the help and possible future help everybody.

After

#9
You must provide the correct types (but not names) in the import statement.

import function getBlah(int, int);
import function setBlah(int, int ,int);

[edit: oh, there were questions...]
Yes.
No. The room script doesn't access the array itself; it just calls functions.
Yes.
It's a good policy, but I think AGS zeros undefined values. And if not, your code can simply avoid using them before they are defined.

Squinky

Rock on After! That did it, and now I'm one happy bastard...


Hell yeah!

Gilbert

#11
Quote from: Guybrush Peepwood on Thu 13/11/2003 20:34:42
var[2*(1+1)] would be exactly the same to var[4*(0+1)]

Well, I don't really know how you came up with these indices. When you have fixed the dimensions of a 2D array, the formula is fixed.
Example, to make an array of 3*2 (=6) elements, you do:
int blah[6];

So to refer to element [ x ][ y ], we use the index:
x*2+y

So to sum up, the respective indices are:
[ 0 ][ 0 ] should use index 0*2+0=0
[ 0 ][ 1 ] should use index 0*2+1=1
[ 1 ][ 0 ] should use index 1*2+0=2
[ 1 ][ 1 ] should use index 1*2+1=3
[ 2 ][ 0 ] should use index 2*2+0=4
[ 2 ][ 1 ] should use index 2*2+1=5

So there won't be any clash in indices.

SMF spam blocked by CleanTalk