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