Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, Ayumi), 1,395 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
multi-dimensional arrays #147043
08/10/07 12:05
08/10/07 12:05
Joined: Jun 2007
Posts: 28
D
Doriel Offline OP
Newbie
Doriel  Offline OP
Newbie
D

Joined: Jun 2007
Posts: 28
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

Re: multi-dimensional arrays [Re: Doriel] #147044
08/10/07 17:53
08/10/07 17:53
Joined: Jan 2007
Posts: 221
F
Fenriswolf Offline
Member
Fenriswolf  Offline
Member
F

Joined: Jan 2007
Posts: 221
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.

Re: multi-dimensional arrays [Re: Doriel] #147045
08/10/07 17:53
08/10/07 17:53
Joined: Aug 2003
Posts: 7,439
Red Dwarf
Michael_Schwarz Offline
Senior Expert
Michael_Schwarz  Offline
Senior Expert

Joined: Aug 2003
Posts: 7,439
Red Dwarf
Multidiemnsional arrays are not posible. Period.


"Sometimes JCL reminds me of Notch, but more competent" ~ Kiyaku
Re: multi-dimensional arrays [Re: Michael_Schwarz] #147046
08/10/07 18:31
08/10/07 18:31
Joined: Oct 2006
Posts: 873
S
Shadow969 Offline
User
Shadow969  Offline
User
S

Joined: Oct 2006
Posts: 873
Quote:

Multidiemnsional arrays are not posible. Period.



sorry, what do you mean?

Re: multi-dimensional arrays [Re: Doriel] #147047
08/10/07 18:44
08/10/07 18:44
Joined: Apr 2005
Posts: 67
InnerSpace
dudeBot Offline
Junior Member
dudeBot  Offline
Junior Member

Joined: Apr 2005
Posts: 67
InnerSpace
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.

Re: multi-dimensional arrays [Re: dudeBot] #147048
08/14/07 16:30
08/14/07 16:30
Joined: Apr 2005
Posts: 67
InnerSpace
dudeBot Offline
Junior Member
dudeBot  Offline
Junior Member

Joined: Apr 2005
Posts: 67
InnerSpace
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


Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1