• 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.

Problem with struct

Status
Not open for further replies.
Hello people :D

I've got a problem I just can't figure out how to fix it.
Take a look at this structure:

JASS:
struct IDATA
    integer array Resistances  [7]
    integer array DamageBonus  [7]
    integer array PassiveBonus [6]
    integer array StatBonus    [6]
    integer array SkillBonus   [6]
    integer array SkillLevel   [6]
    
     // -- Used for gems only --
    boolean array Applies [6]
    
    boolean Common = true
    integer id     = 0
    integer Skills = 0
    integer MaxGems = 0
    
    static method create takes integer Item, boolean c returns IDATA
        local IDATA it = IDATA.allocate()
        local integer i = 0
        set it.id = Item
        if c then
            // -- HT_ITEMTYPES is a constant key --
            call SaveInteger( TheHashtable, HT_ITEMTYPES, Item, it )
        endif
        loop
            exitwhen i >= 6
            set it.Resistances[i] = 0
            set it.DamageBonus[i] = 0
            set it.PassiveBonus[i] = 0
            set it.StatBonus[i] = 0
            set it.SkillBonus[i] = 0
            set it.SkillLevel[i] = 0
            set it.Applies[i] = false
            set i = i + 1 
        endloop        
        return it
    endmethod
endstruct

The idea is an item type with several attributes like resistances, damage bonus etc (for varius elements). My problem is with the "Applies" member.

See this declaration:

JASS:
    // -- ARMOR is a constant integer (= 6)
    // -- Topaz --
    set x = IDATA.create('G003', true)
    set x.Applies[ARMOR] = true
    // -- Diamond --
    set x = IDATA.create('G000', true)
    set x.Applies[ARMOR] = true

If I use a debug message to show "true" or "false" depending on x.Applies[...] value, it only shows correctly for Diamond. It shows false for all indexes for Topaz.
However, if I invert the order, those things also invert. Moreover, if I just put anoter IDATA.create in front of Diamond, it stops working too.

JASS:
    // -- Diamond --
    set x = IDATA.create('G000', true)
    set x.Applies[ARMOR] = true
    set x = IDATA.create('G000', true)
    // -- Diamond does not work anymore --

Note that if I remove the "set it.Applies = false" line in the struct create method, this problem is solved. But I'd still like to know what's causing this =/

I've thought about struct quantity limit due to the number of arrays in the structure. But even if I divide 8100 by 7 there will still be more than 1000 usable struct - and I have no more than 10 created structs.

Can someone help me?

Thanks in advance :D
Hossomi
 
Level 13
Joined
Nov 22, 2006
Messages
1,260
I haven't read the whole post, but I noticed something....

This is probably a stupid question, but doesn't using indexes of x.Applies which are higher than 5 cause problems? I thought that when you declare the size of an array, for example 6 (like you have here), the available indexes start from 0, not from 1, so they are 0, 1, 2, 3, 4, 5, and you're using 6 as the index (ARMOR). I think any index higher than 5 shouldn't work right...or maybe I just got that part wrong.

EDIT: Thinking about the fact that your errors disappear when you remove that set it.Applies[i] = false part, I've developed a different theory on how arrays with specified size work. Maybe 6 means that any six indexes are available, but once declared, no other index works. For example, you can initialize indexes 1, 3, 8, 10, 13, 17, but after that index numbers like 2, 4, 5, 9 etc. won't work.

Back to the point, in this loop:

JASS:
loop
            exitwhen i >= 6
            set it.Resistances[i] = 0
            set it.DamageBonus[i] = 0
            set it.PassiveBonus[i] = 0
            set it.StatBonus[i] = 0
            set it.SkillBonus[i] = 0
            set it.SkillLevel[i] = 0
            set it.Applies[i] = false
            set i = i + 1
        endloop

Your initializing first 6 indexes, but starting from 0 (so up to 5).
 
Status
Not open for further replies.
Top