• 🏆 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!

[vJASS] 2-D Array in Struct

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,337
Hi,

When I try to use a 2-D array as a struct field, I get an error: "Unexpected [10]"
where the array is "integer array m[5][10]"

I suspect this is an oversight by how the structs are made, since everything becomes an array.

However, 2-D arrays work fine in global blocks...

So how can I use the 2-D array syntax without using the simulation trick? Does someone have a working version where 2-D arrays are allowed as struct fields...
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
vJass doesnt support 2d arrays as struct members. You only can have 1d arrays as members or 2d arrays as globals/static members.

If you really need 2d arrays as members you need to write your own implementation.
 
Level 15
Joined
Aug 7, 2013
Messages
1,337
vJass doesnt support 2d arrays as struct members. You only can have 1d arrays as members or 2d arrays as globals/static members.

If you really need 2d arrays as members you need to write your own implementation.

Right, I wasn't sure if it was intentional or just because it was decided not to handle a field that is a 2-D array.

I had to simulate my own. It makes the code uglier to read, but works.
 

LeP

LeP

Level 13
Joined
Feb 13, 2008
Messages
539
You could use Dynamic Arrays with array-members.

JASS:
// not tested
// uses cohadars version of jh
type myArr extends integer array [23]

struct foo
  myArr arr[42]

  static method create takes nothing returns foo
    local foo this = allocate()
    local integer i
    for i = 0 to arr.size -1
      set arr[i]=myArr.create()
    endfor
  endmethod

  method onDestroy takes nothing returns nothing
    local integer i
    for i = 0 to arr.size -1
      call arr[i].destroy()
    endfor
  endmethod
endstruct
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
One for the first dim and another for the second dim ^^

I think you misunderstood what 2d arrays are:

1d array of size 10:
[x x x x x x x x x x]

2d array of size 10x10:
[x x x x x x x x x x]
[x x x x x x x x x x]
[x x x x x x x x x x]
[x x x x x x x x x x]
[x x x x x x x x x x]
[x x x x x x x x x x]
[x x x x x x x x x x]
[x x x x x x x x x x]
[x x x x x x x x x x]
[x x x x x x x x x x]

As you can see you need more than two 1d arrays to create a 2d array. (2d arrays contain ((size of 1. dimension) x (size of 2. dimension)) fields)
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
JASS:
struct s
    private TableArray myTable
    private method show takes nothing returns nothing
        set myTable[20].real[542] = 45.68
        set myTable[21].string[5731] = "My Custom String"
        call BJDebugMsg(myTable[21].string[5731])
    endmethod
    private static method onInit takes nothing returns nothing
        set myTable = TableArray[0x2000]  //will make the first dimension 0 to 8191
    endmethod
endstruct

http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
but keep in mind that if you use array inside struct, you effectivelly destroy your struct, because you can maximally allocate 8190/N instances, where N is highest struct member array size, so if you have like:

JASS:
struct A
    integer array in[100]
    real array re[50]
endstruct

you can only allocate struct A 81 times
 
Level 15
Joined
Aug 7, 2013
Messages
1,337
but keep in mind that if you use array inside struct, you effectivelly destroy your struct, because you can maximally allocate 8190/N instances, where N is highest struct member array size, so if you have like:

JASS:
struct A
    integer array in[100]
    real array re[50]
endstruct

you can only allocate struct A 81 times

Don't understand yet why this is.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
because what it does is, it takes the 8191 range, and cuts it to the biggest size of the array, because the array is, well, array, so in order to not overlap, it has to reserve those members

Example:
(the A is from previous post)
JASS:
local A a = A.create()
local A b = A.create()
set a.in[5] = 8
set b.in[5] = 9

becomes

JASS:
local integer a = A__allocate() //notice, if you dont overload create method
                                         //JassHelper will inline the call
local integer b = A__allocate()
set A__in[5 + a * 100] = 8
set A__in[5 + b * 100] = 9
(I typed it, didnt copy so may be a slightly different, but this is the idea)

It basically has to make sure that doing thistype(0).in[99] wont overlap with thistype(1).in[0], so it takes space

Also what Malhorne said is possible, but at that point, you should consider talking to psychiatrist, because that will destroy performance as you know it, appocalypse
 
Status
Not open for further replies.
Top