# [VJASS] 2-D Arrays give error

Discussion in 'Warcraft Editing Tools' started by sethmachine, Jan 4, 2014.

1. ### sethmachine

Joined:
Aug 7, 2013
Messages:
1,318
Resources:
0
Resources:
0
Hi,

I used this example from the manual, but it gives me an error.

I don't care about the easy trick for simulating 2-D arrays, I'd just like to use the syntax out of the box. The manual says its in VJASS, what am I doing wrong?

2D arrays

A quick improvement from sized arrays, is the ability to have two-dimensional arrays, n-dimensional arrays are not implemented, if you really need it very hard, contact me.

Two dimensional arrays in vJass, since vJass is implemented on top of Jass, are just normal arrays in disguise, using a multiplication trick to convert 2-dimension indexes into a one-dimension one. The way to declare one of these arrays is: <type> array name[width][height], notice the real size of the array is width*height, this size suffers the same limitations as normal array's size, it cannot go above approximately 40800, and if this size is bigger than 8191, you will be using slower function calls instead of array lookups and multiple arrays in the final script, etc.

The field size would return this total size we are talking about, the fields height and width return the ones we used to declare the array. As with sized arrays, you can use constants for the width and size.

Code (vJASS):

globals
integer array mat1 [10][20]

constant integer W=100
constant integer H=200
integer array mat2 [W][H]

endglobals

function test takes nothing returns nothing
local integer i=0
local integer j=0
local integer c=0
call BJDebugMsg(I2S(mat1.size)) //displays 200 (10 * 20)

//fill the array:
loop
exitwhen (i==mat1.width)
set j=0
loop
exitwhen (j==mat1.height)
set c=c+1
set mat1[i][j]=c
set j=j+1
endloop

set i=i+1
endloop

call BJDebugMsg( I2S( mat1[0][1]  )  ) //displays 2

call BJDebugMsg( I2S( mat2.width) ) //displays 100
endfunction

2. ### muzzel

Joined:
Jun 27, 2008
Messages:
1,303
Resources:
2
JASS:
1
Wurst:
1
Resources:
2
If you get unexpected behaviour try to debug it. E.g. add messages which output the variables of interest...

3. ### edo494

Joined:
Apr 16, 2012
Messages:
3,846
Resources:
5
Spells:
1
JASS:
4
Resources:
5
You wrote what is said in manual, but you didnt say what is wrong, no code shown(the one is also taken from the manual)

What do you mean by using them "outside of box"?

4. ### sethmachine

Joined:
Aug 7, 2013
Messages:
1,318
Resources:
0
Resources:
0
There's no unexpected behavior as if I add in in "integer array mat1 [20][30]" I get an error immediately. But when I comment it out, no more errors.

Well the 2-D array isn't working. Out-of-the-box means I don't need to find a 2-d array library or get a different version of the JGNP to make the 2-d array syntax work.

5. ### edo494

Joined:
Apr 16, 2012
Messages:
3,846
Resources:
5
Spells:
1
JASS:
4
Resources:
5
what version of JNGP do you have? What version of JassHelper does it have?

6. ### sethmachine

Joined:
Aug 7, 2013
Messages:
1,318
Resources:
0
Resources:
0
JassHelper is version 0.A.2.B

IF you mean the newgen editor, it's version 1.4.0.1

Would appreciate a link to a correct version that fixes this problem. Need that 2-d array syntax pronto.

IF it helps, the 2-d arrays are used as struct fields. Would this be problematic (don't see why though...)

This is where I got my JNPG: http://www.wc3c.net/showthread.php?t=88142

Last edited: Jan 4, 2014

### Code Moderator

Joined:
Nov 11, 2006
Messages:
7,430
Resources:
18
Icons:
1
Spells:
4
Tutorials:
9
JASS:
4
Resources:
18
That is likely the problem. When you have this:
Code (vJASS):
struct A
unit caster
endstruct

"caster", in vanilla JASS, is actually a unit array.

That's why when you make an arrayed struct member, you have to add a size (it is actually making a 2D array, sort of):
Code (vJASS):
struct A
unit caster[20]
endstruct

And as far as I know, there is no jasshelper feature to make anything beyond a 2-dimensional array. I mean, you can always do some clever hackery with Table, or something like this:

8. ### sethmachine

Joined:
Aug 7, 2013
Messages:
1,318
Resources:
0
Resources:
0
I ultimately used the simulation trick. Makes the code harder to read, but it works obviously.

9. ### muzzel

Joined:
Jun 27, 2008
Messages:
1,303
Resources:
2
JASS:
1
Wurst:
1
Resources:
2
*Facepalm* Why didnt u say that earlier.

10. ### sethmachine

Joined:
Aug 7, 2013
Messages:
1,318
Resources:
0
Resources:
0
Well I don't really see any theoretical reason why structs couldn't have 2-D arrays as fields. I guess it wasn't seen as necessary given the implementation of the struct fields, which I didn't think of at first.

Also, the manual should probably state: 2-D Arrays cannot be used as struct fields due to....

It doesn't say that anywhere. You'd have to figure it by knowing the implementation of struct fields. Manuals shouldn't have puzzles in them