multi-dimensional arrays

Posted By: Doriel

multi-dimensional arrays - 08/10/07 12:05

I know that arrays exist in cscript. (e.g. Var myArray[10];) But is it possible to have multi-dimensional arrays?

I want to do something like this:

var myArray[10][20];

I tried it but it's not working. Actually, it allows me to define it but when I try to set a value, it gives me an error.

myArray[4][18] = 10;


Any ideas? Are there any other constructs out there that I could use instead if this isn't possible?


Regards,

Doriel
Posted By: Fenriswolf

Re: multi-dimensional arrays - 08/10/07 17:53

Hi Doriel,

since there are no real multidimensional arrays in c-script you can fake them by doing the following (described in the manual):
Quote:


Multidimensional arrays can be defined in C-Script by multiplying the index by the array width. Suppose you need an array in two dimensions, like a 10*20 grid of height values. It is defined this way:

var heightmap[200]; // 200 = 10*20
The array value at position (j,i) can then be accessed through:

heightmap[j*20 + i] = 10; // j = 0..9, i = 0..19





However in lite-c you can use multidimensional arrays as is usual in other languages.
Posted By: Michael_Schwarz

Re: multi-dimensional arrays - 08/10/07 17:53

Multidiemnsional arrays are not posible. Period.
Posted By: Shadow969

Re: multi-dimensional arrays - 08/10/07 18:31

Quote:

Multidiemnsional arrays are not posible. Period.



sorry, what do you mean?
Posted By: dudeBot

Re: multi-dimensional arrays - 08/10/07 18:44

Though it’s true that multi-dimensional arrays are not
a “data type,” as Fenriswolf mentioned, you can access
a single dimensional array as a multi-dimensional easily with
script.

When I have done this, I define variables to represent
the number of rows and columns in the array.

myArray[10][20] has 10 rows and 20 columns.


So, I would do this:

var gRows = 10;
var gColumns = 20;
myArray[200];

To instantiate the array, you will need 2 loops,
an outer loop to step through each row, and an inner
loop to step through each column, using the gRows and
gColumns variables to control the number of iterations in each loop.

I like to use “set” and “get” functions to access the array,
this way I can sort of treat it like a multi-dimensional array
and can do custom “index out of range” error checking.

To set a value, I pass indexes just as if I were accessing
a multi-dimensional array.

myArray[4][18] = 10 becomes setArrayValue(4, 18, 10)

In the function I can test to see if a passed value is
“out of range” and then I can access the array position
like this:

myArray[(4*gRows)+18] = 10;

The “get” function does the same thing, except it is used to
get the value (I guess that was obvious )

Doing this helps me to create a conceptual model, treating the single
dimensional array as a multi-dimensional one.

Rather than posting a bunch code on the forum, I have
created a .wdl that has all of the functions I mentioned.
You, or anyone else interested, can download it here:
http://www.ionstudios.net/arrayExample.zip

Hope this helps.
Posted By: dudeBot

Re: multi-dimensional arrays - 08/14/07 16:30

I wanted to post a correction to my last post on this topic
and the code that I made available on my website.

I've been working on a "jezz ball" game in flash, which I will
eventually "port" over to 3DGS. In it, I'm using a single dimensional
array to "build" the blocks on the board and to track block
states to deterinime the grid regions the ball can pass through.
In doing this, I noticed an error.

The script that I included in the post: myArray[(4*gRows)+18] = 10;
will only work if the rows and columns in the array are the same.

It will not work if there are 10 rows and 20 columns.

Changing the script to myArray[(4*gColumns)+18] = 10; is what is needed
in this case.

I have also updated and uploaded the .wdl I created
to reflect changes made in the function that insantiates the array,
as well as the "get" and "set" functions. The updated script can
be found here: http://www.ionstudios.net/3DGS/arrayExample.zip

The link I posted in the last response is now "dead."

Sorry if anyone had looked at or used this script and things didn't work
correctly. The new script will work for arrays where the rows = columns
and the rows != columns
© 2024 lite-C Forums