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

Question about an array of structs

Status
Not open for further replies.
Level 3
Joined
Jan 15, 2010
Messages
47
Hey,

I have a struct with some stuff that I am using for a custom stat system. I am not sure why, whether I am getting the syntax wrong, or some other logical error, but I can't save/read values correctly.

I have the following code in my map header:

JASS:
struct heroData
   integer MeleeDamageModifier = 0
   integer SpellDamageModifier = 0
   integer DodgeChance = 0
endstruct

globals
   heroData array hData
endglobals

And then I have a function that does these actions when I type "-test"

JASS:
private function Actions takes nothing returns nothing
   set hData[0].DodgeChance = 10
   set hData[1].DodgeChance = 20
   call MessageAll("Dodge Chance for P1 = "+I2S(hData[0].DodgeChance))
   call MessageAll("Dodge Chance for P2 = "+I2S(hData[1].DodgeChance))
endfunction

My output is:

Dodge Chance for P1 = 20
Dodge Chance for P2 = 20

So its overriding the value or I am reading it incorrectly or something. Any ideas?

Thanks
-enyeas
 
Level 13
Joined
Mar 16, 2008
Messages
941
Well, you need to allocate the instances? Use create? Or, if you want to use it as arrays, type this:
JASS:
struct heroData extends array
   integer MeleeDamageModifier = 0
   integer SpellDamageModifier = 0
   integer DodgeChance = 0
endstruct

private function Actions takes nothing returns nothing
   set hData[0].DodgeChance = 10
   set hData[1].DodgeChance = 20
   call MessageAll("Dodge Chance for P1 = "+I2S(hData[0].DodgeChance))
   call MessageAll("Dodge Chance for P2 = "+I2S(hData[1].DodgeChance))
endfunction
 
Level 3
Joined
Jan 15, 2010
Messages
47
What would be the difference with allocating the instances or extending it as an array? And would I just type something like

JASS:
globals
   heroData array hData.Create()
endglobals

Thanks for the help, it makes sense now and i'm not completely baffled, which is a nice step forward :) Not too many tutorials out there on using arrays of structs :/
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
Vjass is normal JASS but with some shortcuts...

You can not call non native functions in the globals part of the map.

You have to go...
set hData[0] = heroData.create() //May have spelling or case wrong with the create.
set hData[1] = heroData.create()
set hData[0].DodgeChance = 10
set hData[1].DodgeChance = 20
rest actions here...

Read the documentation, it will tell you all you need to know.
 
Level 3
Joined
Jan 15, 2010
Messages
47
Okay, so that would all go in a function that starts 0.1 seconds into the game? Thanks

edit: attempted and works successfully, thanks guys :)
 
Level 13
Joined
Mar 16, 2008
Messages
941
Just to answer your other question:

A struct that extends array isn't a normal struct anymore. You cant "create", "allocate" or "destroy" them, the behave like normal arrays and you can't use arrays inside of them.
For example usefull, if you require a struct per player, making it an array to access the structs via playernumber.
 
Status
Not open for further replies.
Top