• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Array of structs?

Status
Not open for further replies.
JASS:
local t array var
Struct-type variables are just integer values associated with an index, so the above would compile tolocal integer array var

You also can't define array sizes in JASS. They are all limited to 8191. However structs can simulate array sizes for it's members at the cost of how many instances your struct can have.

http://www.wc3c.net/vexorian/jasshelpermanual.html
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
Thank you. That makes perfect sense.

But if I make an array of some struct, how do I ensure that the array is declared AFTER the struct is declared?

e.g. if I try to initialize an array of some struct which doesn't exist yet, shouldn't I get an error?

Can structs have arrays as members? I read in the manual which comes with jass gen pack that array members are not supported yet...

JASS:
myStruct array s
.
.
.
struct myStruct
endstruct

should given an error, but

JASS:
struct myStruct
endstruct
.
.
.
myStruct array S

should be ok?
 
But if I make an array of some struct, how do I ensure that the array is declared AFTER the struct is declared?

e.g. if I try to initialize an array of some struct which doesn't exist yet, shouldn't I get an error?

You can use thetypeorrequireskeywords.

JASS:
type SomeStruct extends integer

globals
    SomeStruct var
endglobals

struct SomeStruct
endstruct

JASS:
library other requires lib
    globals
        SomeStruct var
    endglobals
endlibrary

library lib
    struct SomeStruct
    endstruct
endlibrary
 
Last edited:
Level 15
Joined
Aug 7, 2013
Messages
1,338
Ah so the compiler doesn't figure it out automatically and just make sure to put the struct first (if it exists).

If I have a chain of dependent structs though will it not get a bit annoying to read all the require statements?
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
Then usetype somestruct extends integer

Thanks you again--sorry but had a quick question on a point you made, that you can't declare sizes of arrays in JASS. If that is true, I am wondering why the previous developer had this code:

JASS:
globals
    integer array FS_result_t2[79]
    integer array FS_result_t3[79]

    integer array FS_result_sp[MAX_SP_FUSIONS]
    integer array FS_req1_sp[MAX_SP_FUSIONS]
    integer array FS_req2_sp[MAX_SP_FUSIONS]
    integer array FS_fl_sp[MAX_SP_FUSIONS]
.
.
.
.
endglobals
 
Struct members are internally global arrays, so that determines what their initial values are. The internal code won't initialize it to anything unless you write it out. For example:
JASS:
struct Test
    unit u
endstruct
When you call Test.create(), it won't set u to anything at all. By default, it will return null because global arrays default to null.

For integers, they default to 0. For booleans, they default to false. I don't remember whether strings refer to "" or null.

Note that this is only true for arrays (struct members are internally arrays, so that is why it is fine). If you have a local variable or a global variable, they count as uninitialized (unless you give them a value to point to):
JASS:
local unit u
call KillUnit(u)
call BJDebugMsg("This message won't display.")
 
Level 15
Joined
Aug 7, 2013
Messages
1,338
Struct members are internally global arrays, so that determines what their initial values are. The internal code won't initialize it to anything unless you write it out. For example:
JASS:
struct Test
    unit u
endstruct
When you call Test.create(), it won't set u to anything at all. By default, it will return null because global arrays default to null.

For integers, they default to 0. For booleans, they default to false. I don't remember whether strings refer to "" or null.

Note that this is only true for arrays (struct members are internally arrays, so that is why it is fine). If you have a local variable or a global variable, they count as uninitialized (unless you give them a value to point to):
JASS:
local unit u
call KillUnit(u)
call BJDebugMsg("This message won't display.")

What about this situation?

JASS:
struct myStruct A
endstruct
...
globals
myStruct array s
endglobals
//are all the members of s initialized to 0 or null?
s[0] == 0 //returns true?
s[0] == null //returns true?
 

LeP

LeP

Level 13
Joined
Feb 13, 2008
Messages
543
Thank you. That makes perfect sense.

But if I make an array of some struct, how do I ensure that the array is declared AFTER the struct is declared?

e.g. if I try to initialize an array of some struct which doesn't exist yet, shouldn't I get an error?

Can structs have arrays as members? I read in the manual which comes with jass gen pack that array members are not supported yet...

JASS:
myStruct array s
.
.
.
struct myStruct
endstruct

should given an error, but

JASS:
struct myStruct
endstruct
.
.
.
myStruct array S


should be ok?

Read again. Array members are supported.
Also for public structs you don't have to do anything. JassHelper figures its all out.

Thanks you again--sorry but had a quick question on a point you made, that you can't declare sizes of arrays in JASS. If that is true, I am wondering why the previous developer had this code:

JASS:
globals
    integer array FS_result_t2[79]
    integer array FS_result_t3[79]

    integer array FS_result_sp[MAX_SP_FUSIONS]
    integer array FS_req1_sp[MAX_SP_FUSIONS]
    integer array FS_req2_sp[MAX_SP_FUSIONS]
    integer array FS_fl_sp[MAX_SP_FUSIONS]
.
.
.
.
endglobals

It's a vJass extension. Just read about it in the manual.

You can use thetypeorrequireskeywords.

JASS:
type SomeStruct extends integer

globals
    SomeStruct var
endglobals

struct SomeStruct
endstruct

JASS:
library other requires lib
    globals
        SomeStruct var
    endglobals
endlibrary

library lib
    struct SomeStruct
    endstruct
endlibrary

Then usetype somestruct extends integer

Again, for public structs you don't have to do anything. And when you have to forward declare something you do it like this keyword MyStruct. public/private modifiers are allowed.
I mean you still should use library and requires but not for simple things as struct-decleration.
 
Status
Not open for further replies.
Top