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

[JASS] Struct arrays

Status
Not open for further replies.
Level 4
Joined
Dec 10, 2005
Messages
73
JASS:
struct Branch
   //data
endstruct

globals
   Branch array STBRANCH[500]
endglobals

function test takes nothing returns nothing
   set STBRANCH[1] = Branch.create()
   set STBRANCH[2] = Branch.create()
endfunction

Is this correct usage of struct arrays. It's sort of funny looking to me as in the globals I have made it an array but have to still create it.
I saw that you could also do

JASS:
struct Branch extend array
   //data
endstruct

But not sure what how this works.
 
Level 4
Joined
Nov 23, 2007
Messages
113
Regardless of what method you use, you still need to create an instance for each array element. Simply defining a struct array type doesn't allocate any space for it.
 
Level 4
Joined
Nov 23, 2007
Messages
113
so arrayed structs:
JASS:
struct name extends array
     real x
     real y
endstruct

function bla takes nothing returns nothing
     set name[1].x = 5.
     set name[2].y = 6.
endfunction

so it works like this :O


you don't have to allocate arrayed structs... so if you don't know anything don't post pls

I can assure you that your above code will not work as is. Simply defining a struct array type (as you've done above) does not allocate any space for the array elements. You need to at least declare a global array of type "name" or an array of type "struct" to allocate the array elements, or allocate them manually if declared locally. Therefore, your bla() function is accessing elements that have not been allocated.

I'm simply pointing this out to the OP to avoid confusion because regardless of what method you use, the array elements need to be allocated either by global declaration or local code.
 
Level 4
Joined
Nov 23, 2007
Messages
113
are u kidding.... it works like it is there... please don't try to fix something that don't need to be fixed

Actually, I'm thinking of
type nameA extends name array[n]

which creates a type without allocation.

I'll wait til I'm awake to comment then ;) In the meantime, try not to let your God complex expand too far outside the hive.
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
I can assure you that your above code will not work as is. Simply defining a struct array type (as you've done above) does not allocate any space for the array elements. You need to at least declare a global array of type "name" or an array of type "struct" to allocate the array elements, or allocate them manually if declared locally. Therefore, your bla() function is accessing elements that have not been allocated.

I'm simply pointing this out to the OP to avoid confusion because regardless of what method you use, the array elements need to be allocated either by global declaration or local code.

Read.

struct name extends array

Read it.
 
Level 4
Joined
Nov 23, 2007
Messages
113
Level 4
Joined
Dec 10, 2005
Messages
73
Alrighty, So the first method I would need use .create() and .destroy() and define a global for it, but for the second, I could use as it.

Do I have to define how big the array must be for both cases?

Can someone also post an example of how to pass struct from one function to another.

Thanks for the help
 
Level 14
Joined
Nov 23, 2008
Messages
187
1. As Schurke stated, there's no need to create and destroy.
2. No.
3. You only need to pass its number in array. Example:

JASS:
struct playerdata extends array
  integer a
  integer b
endstruct

function onTimer takes nothing returns nothing
  local integer i = GetTimerData(GetExpiredTimer())
  // or any other attaching function
  // local integer i = LoadInteger(ht, GetHandleId(GetExpiredTimer()), 0)
  set playerdata[i].a = playerdata[i].a + 1
  set playerdata[i].b = playerdata[i].b - 1
  // . . .
endfunction

function Start takes nothing returns nothing
  // . . .
  set playerdata[5].a = 123
  set playerdata[5].b = 456
  call SetTimerData(t, 5)
  // or any other attaching function
  // call SaveInteger(ht, GetHandleId(t), 0, 5)
  // . . .
endfunction
 
Level 4
Joined
Dec 10, 2005
Messages
73
Just to be clear. The way I'm using struct global at the moment is

JASS:
struct Branch 
integer a
endstruct

globals
Branch array STBRANCH
endglobals

And with this way I have to use .create() and .destroy(), otherwise it will keep saving data to first slot of the array.

And thanks Shadow Daemon for point 3.
 
Status
Not open for further replies.
Top