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

Help me finish this spellIndex struct?

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
it lacks proper de-allocation and deindexing...

JASS:
struct SpellIndex
        integer array spell[15]
        integer index = 0
        static integer n = 0
        
        static method giveSpellsToUnit takes unit u returns nothing
            local thistype this = LoadInteger(hash, GetHandleId(u), 0)
            local integer i = 0
            call BJDebugMsg("ehh")
            loop
                exitwhen i == this.index
                call BJDebugMsg("HELLO!")
                call UnitAddAbility(u, this.spell[i])
                set i = i + 1
            endloop
        endmethod
        
        method add takes integer spell returns nothing
            call SaveInteger(hash, this, spell, .index)
            set .spell[.index] = spell
            set .index = .index + 1
        endmethod
        
        method remove takes integer spell returns nothing 
            local integer i = LoadInteger(hash, this, spell)
            set .index = .index -1
            set .spell[i] = .spell[.index]
        endmethod
        
        static method create takes unit u returns SpellIndex
            local thistype this = .allocate()
            call SaveInteger(hash, GetHandleId(u), 0, this)
            set SpellIndex.n = SpellIndex.n + 1
            return this
        endmethod
    endstruct

To be more specific, I'm not sure how to flush that saved integer on deindex or get the thing to work properly.

I'm trying to make a system that if you select a object a menu appears and the options are depending on the SpellIndex here.

A example of the usage would be that if you click on a rune above a door. You get the Option to Open it. When you Open it, that spell is deindexed and Close Door Spell is added to the spellindex so that next time Open Door option appears instead.
 
Last edited by a moderator:
store the unit:
JASS:
    struct SpellIndex
        integer array spell[15]
        integer index = 0
        unit host // added
        static integer n = 0
        
        static method giveSpellsToUnit takes unit u returns nothing
            local thistype this = LoadInteger(hash, GetHandleId(u), 0)
            local integer i = 0
            call BJDebugMsg("ehh")
            loop
                exitwhen i == this.index
                call BJDebugMsg("HELLO!")
                call UnitAddAbility(u, this.spell[i])
                set i = i + 1
            endloop
        endmethod
        
        method add takes integer spell returns nothing
            call SaveInteger(hash, this, spell, .index)
            set .spell[.index] = spell
            set .index = .index + 1
        endmethod
        
        method remove takes integer spell returns nothing 
            local integer i = LoadInteger(hash, this, spell)
            set .index = .index -1
            set .spell[i] = .spell[.index]
        endmethod

        // added
        method destroy takes nothing returns nothing
            call FlushChildHashtable(hash, this)
            call RemoveSavedInteger(hash, GetHandleId(.host), 0)
            set SpellIndex.n = SpellIndex.n - 1
            call .deallocate()
        endmethod
        
        static method create takes unit u returns SpellIndex
            local thistype this = .allocate()
            set this.host = u // added
            call SaveInteger(hash, GetHandleId(u), 0, this)
            set SpellIndex.n = SpellIndex.n + 1
            return this
        endmethod
    endstruct
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
Made a test. it finds the setup added OPEN_DOOR ability. But when it removes as below and i reselect the same interaction no spells are found?

JASS:
private function OpenDoor takes nothing returns nothing 
        local unit u = GetTriggerUnit()
        local real x = GetUnitX(u)
        local real y = GetUnitY(u)
        local rect r = Rect(x - RADIUS, y - RADIUS, x + RADIUS, y + RADIUS)
        local integer i = GetPlayerId(GetTriggerPlayer())
        call EnumDestructablesInRect(r, null, function OpenDoorBlock)
        call SetDoodadAnimationRect(r, DOOR1, DOOR1_OPEN, false)
        call SetDoodadAnimationRect(r, DOOR2, DOOR2_OPEN, false)
        call RemoveRect(r)
        call spelldex[SpellIndex.getArray(curInteraction[i])].remove(AB_OPEN_DOOR)
        call spelldex[SpellIndex.getArray(curInteraction[i])].add(AB_CLOSE_DOOR)
        call KillUnit(u)
        set  r = null
        set u = null
    endfunction

Also another issue is that even if only 1 unit is set to be getting the abilities all units of that type get the ability. here is the setup:
JASS:
private function Setup takes nothing returns nothing
        set spelldex[SpellIndex.n] = SpellIndex.create(gg_unit_n001_0255)
        call spelldex[SpellIndex.n].add(AB_OPEN_DOOR)
    endfunction
 
Last edited:
Status
Not open for further replies.
Top