• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[VJASS] 2-D Arrays give error

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,337
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.

JASS:
   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
 
Level 15
Joined
Aug 7, 2013
Messages
1,337
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.
 
Level 15
Joined
Aug 7, 2013
Messages
1,337
what version of JNGP do you have? What version of JassHelper does it have?

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:
IF it helps, the 2-d arrays are used as struct fields. Would this be problematic (don't see why though...)

That is likely the problem. When you have this:
JASS:
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):
JASS:
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:
http://www.thehelper.net/threads/n-dimensional-arrays-useful.137133/
 
Level 15
Joined
Aug 7, 2013
Messages
1,337
*Facepalm* Why didnt u say that earlier.

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 :p
 
Status
Not open for further replies.
Top