• 🏆 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!
  • ✅ Time to vote for the top 3 models! The POLL for Hive's 6th HD Modeling Contest: Mechanical is now open! 📅 Poll close on July 16, 2024! 🔗 Cast your vote now!

Trying to reset a timer when a unit takes damage

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
JASS:
private function OnDamage takes nothing returns boolean 
        local timer t 
        local unit damaged = GetTriggerUnit()
        if LoadUnitHandle(hash, GetHandleId(damaged), 1) == null then
            set t = CreateTimer()
            call SaveTimerHandle(hash, GetHandleId(damaged), 1, t)
            call SaveUnitHandle(hash, GetHandleId(t), 1, damaged)
            call TimerStart(t, DURATION, false, function OnTimerEnd)
        else 
            set t = LoadTimerHandle(hash, GetHandleId(damaged), 1)
            call TimerStart(t, DURATION, false, function OnTimerEnd)
        endif
        return false 
    endfunction

It doesn't apear to reset the timer, must i first pause then destroy then create a new one, and then finally save the new timer? :>

Solved it without using a timer. But still intrested in what one has to do to reset a timer.
 
Last edited:
Level 17
Joined
Apr 27, 2008
Messages
2,455
I'm not sure that i remember how hashtables work but maybe it's because you use the same keys.
I mean the handle id of the unit and 1, for the timer and the unit.

My guess is that the timer is overwritten by the unit because the type unit and timer have the same "father" type (i don't know the english name).
JASS:
type widget             extends     agent  // an interactive game object with life
type timer              extends     agent
type unit               extends     widget
But you should be able to store other native types with the same keys such as boolean, integer, real.

Debug it with some text messages.
 
Level 24
Joined
Aug 1, 2013
Messages
4,657
Out of combat system :D

JASS:
globals
    
    hashtable udg_OOC_Hashtable = InitHashtable()
    
endglobals

library oocSystem
    
    function OOC_IsInCombat takes unit whichUnit returns boolean
        return HaveSavedHandle(udg_OOC_Hashtable, GetHandleId(whichUnit), 0)
    endfunction

    function OOC_OutOfCombat takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local integer id = GetHandleId(t)
        call FlushChildHashtable(hash, GetHandleId(LoadUnitHandle(hash, id, 0)))
        call FlushChildHashtable(hash, id)
        
        //  <to-do>
        //call event "A unit leaves combat"
        
        call DestroyTimer(t)
        set t = null
    endfunction

    function OOC_SetInCombat takes unit whichUnit, real duration returns nothing
        local integer id = GetHandleId(whichUnit)
        local timer t = LoadTimerHandle(udg_OOC_Hashtable, id, 0)
        
        if t == null then
            set t = CreateTimer()
            call SaveTimerHandle(hash, id, 0, t)
            call SaveUnitHandle(hash, GetHandleId(t), 0, whichUnit)
        endif
        call TimerStart(t, duration, false, function OOC_OutOfCombat)
        set t = null
        
        //  <to-do>
        //call event "A unit enters combat"
        
    endfunction
    
endlibrary

Your problem is that you save the unit under the handle id of the timer.
And you try to load the unit under the id of the unit.
That ofcourse wont work.
You can use that script which I just wrote 5 minutes ago :D
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
Not sure that is what I need, even though the events are similar. I was making a fire-stacking ability, spawning fire after X damage. But I wanted it to clear the stack after X time. I've got the stacking part down, albeit maybe not the best solution. As for the fire its kind of boring and slow, not sure how to design it.

Currently the fire unit starts with 30% hp/size and will increase in damage based on its hp/size. It gain hp through attacks. Not sure how to improve it, but it feels slow and boring right now, and it can't spread.
 
Last edited:
Status
Not open for further replies.
Top