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

Transform using New Table

Status
Not open for further replies.
Level 23
Joined
Feb 6, 2014
Messages
2,466
I have a spell here that will damage the target unit after the delay. What is the equivalent code if I were to use Bribe's New Table

Furthermore, what do you recommend the TimerUtils to used? Should I used Vexorian's or Magtheridon's. Or should I use TimerTools?

JASS:
scope DelayDamage initializer OnInit
    
    globals
        hashtable ht = InitHashtable()
    endglobals

    private function damageTarget takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local unit tgt = LoadUnitHandle(ht, GetHandleId(t), 0)
        local unit u = LoadUnitHandle(ht, GetHandleId(t), 1)
        call BJDebugMsg(GetUnitName(u) + " has delay damaged " + GetUnitName(tgt))
        call UnitDamageTarget(u, tgt, 9000, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_MAGIC, WEAPON_TYPE_WHOKNOWS)
        
        call FlushChildHashtable(ht, GetHandleId(t))
        call ReleaseTimer(t)
        set t = null
        set u = null
        set tgt = null
    endfunction
    
    private function Casted takes nothing returns boolean
        local unit u
        local unit tgt
        local timer t 

        if (GetSpellAbilityId() == 'AHtb') then
            set u = GetTriggerUnit()
            set tgt = GetSpellTargetUnit()
            set t = NewTimer()
            call BJDebugMsg(GetUnitName(u) + " has casted storm bolt on " + GetUnitName(tgt))
            call SaveUnitHandle(ht, GetHandleId(t), 0, tgt)
            call SaveUnitHandle(ht, GetHandleId(t), 1, u)
            call TimerStart(t, 2., false, function damageTarget)
            set u = null
            set tgt = null
        endif
        return false
    endfunction

    //===========================================================================
    private function OnInit takes nothing returns nothing
        local trigger t = CreateTrigger(  )
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT )
        call TriggerAddCondition(t, Condition( function Casted ) )

        set t = null
    endfunction

endscope
 
Level 23
Joined
Feb 6, 2014
Messages
2,466
I used a consistent parent (handle id of timer) so I think i only need one Table type right? How about the child? I used two different child for a single parent, will it be like this:?
JASS:
tb.unit[0] = tgt
tb.unit[1] = u
Also, is the table created globally? How can I specify that the parent is the handle id of the timer?
 
Yes, the table would be a global. However, you'd need as many Tables as you have indices (in this case 2).

Use TimerUtils by Mag96

TimerUtils has a Set/GetTimerData function which can be used with structs. Structs allow you to work with simpler structures(internally they use arrays, but in an abstract way ), so you don't have to do multiple hashtable reads that way.

Again, you could also just use 2 Tables if you didn't want to introduce that extra complexity yet. There is a big learning curve to structs.

You should also use SpellEffectEvent to minimize unnecessary code and boolean checks.

Here’s how I would do this resource. Uses TimerUtilsEx, SpellEffectEvent and Alloc:

http://www.hiveworkshop.com/forums/graveyard-418/system-timerutilsex-204500/
http://www.hiveworkshop.com/forums/jass-resources-412/snippet-spelleffectevent-187193/
http://www.hiveworkshop.com/forums/jass-resources-412/snippet-alloc-192348/

JASS:
library DelayDamage requires TimerUtilsEx, SpellEffectEvent, Alloc
private struct data extends array
    
    implement Alloc
    
    unit u
    unit tgt
    
    static method damageTarget takes nothing returns nothing
        local thistype this = ReleaseTimer(GetExpiredTimer())
        call BJDebugMsg(GetUnitName(u) + " has delay damaged " + GetUnitName(tgt))
        call UnitDamageTarget(u, tgt, 9000, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_MAGIC, WEAPON_TYPE_WHOKNOWS)
        call deallocate()
    endmethod
    
    static method casted takes nothing returns nothing
        local thistype this = allocate()
        set u = GetTriggerUnit()
        set tgt = GetSpellTargetUnit()
        call BJDebugMsg(GetUnitName(u) + " has casted storm bolt on " + GetUnitName(tgt))
        call TimerStart(NewTimerEx(this), 2., false, function thistype.damageTarget)
    endmethod

    static method onInit takes nothing returns nothing
        call RegisterSpellEffectEvent(‘AHtb’, function thistype.casted)
    endmethod
endstruct
endlibrary
 
Last edited:
Status
Not open for further replies.
Top