• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Respawn Problem

Status
Not open for further replies.
Level 5
Joined
Nov 7, 2005
Messages
78
I have the following code to respawn creeps. However, occasionally a creep fails to respawn. Can anyone see any issues with this?

JASS:
    globals
        private hashtable htGlobal = InitHashtable()
        private trigger tDeath     = CreateTrigger()
    endglobals
    
    private interface we_Entity
        public unit u
        public unit uTarget
        
        method onDeath takes nothing returns nothing defaults nothing
    endinterface
    
    struct we_AI extends we_Entity
        public real x = 0.00
        public real y = 0.00
        public real fFacing = 270.00
        private real fRespawnTime = 60.00
        
        public method setRespawnTime takes real time returns nothing
            if (time < 0.00) then
                set this.fRespawnTime = 0.00
            else
                set this.fRespawnTime = time
            endif
        endmethod
        
        static method create takes unit u returns we_AI
            local we_AI this = we_AI.allocate()
            set this.u = u
            call SaveInteger( htGlobal, GetHandleId(this.u), S2I("WE_ENTITY"), this )
            call TriggerRegisterUnitEvent( tDeath, this.u, EVENT_UNIT_DEATH )
            return this
        endmethod
        
        method onDestroy takes nothing returns nothing
            set this.u       = null
            set this.uTarget = null
        endmethod
        
        static method respawn takes nothing returns nothing
            local timer t = GetExpiredTimer()
            local we_AI this = LoadInteger( htGlobal, GetHandleId(t), 0 )
            local unit u = CreateUnit( GetOwningPlayer(this.u), GetUnitTypeId(this.u), this.x, this.y, this.fFacing )
            call SetHeroLevel( u, GetHeroLevel(this.u), false )
            call RemoveSavedInteger( htGlobal, GetHandleId(this.u), S2I("WE_ENTITY") )
            call RemoveUnit(this.u)
            set this.u = u
            call SaveInteger( htGlobal, GetHandleId(u), S2I("WE_ENTITY"), this )
            call TriggerRegisterUnitEvent( tDeath, this.u, EVENT_UNIT_DEATH )
            call RemoveSavedInteger( htGlobal, GetHandleId(t), 0 )
            call DestroyTimer(t)
            set u = null
            set t = null
        endmethod
        
        method onDeath takes nothing returns nothing
            local timer t = CreateTimer()
            call TimerStart( t, this.fRespawnTime, false, function we_AI.respawn )
            call SaveInteger( htGlobal, GetHandleId(t), 0, this )
            set t = null
        endmethod
        
    endstruct

    function we_CreateEntityAI takes unit u returns we_AI
        local we_AI e = we_AI.create(u)
        set e.x = GetUnitX(u)
        set e.y = GetUnitY(u)
        set e.fFacing = GetUnitFacing(u)
        return e
    endfunction

    function we_GetUnitEntity takes unit u returns we_Entity
        return LoadInteger( htGlobal, GetHandleId(u), S2I("WE_ENTITY") )
    endfunction
    
    private function entOnDeath takes nothing returns nothing
        local we_Entity e = we_GetUnitEntity(GetTriggerUnit())
        call e.onDeath()
    endfunction
    
    private function initSys takes nothing returns nothing
        call TriggerAddAction( tDeath, function entOnDeath )
    endfunction
 
Level 5
Joined
Nov 7, 2005
Messages
78
I used ReviveHero(). It didn't work. As for your first question, you'll notice I'm removing the dead unit, not the new one.

EDIT: I should also mention that the system is structured the way it is because it's part of a larger system.
 
Level 22
Joined
Nov 14, 2008
Messages
3,256
Ah right. Well you probably should save it into a private member instead of a public interface. If you look into my herorevival it actually works so I don't see the problem with it :/ also you notice that you register a new event for every single unit that pass threw this and dies right? The trigger would kinda lagg, maybe use some sort of dynamic triggers usage.
 
Level 5
Joined
Nov 7, 2005
Messages
78
I can see why that might cause slowdowns, but why should that prevent the system from working?

Also, you say 'according to damage detection systems'. How would one test what you're referring to (that the event is not unregistered)?

EDIT: Also, you mentioned dynamic triggers. I had considered that, but I figured that one trigger with many events would be more efficient than many triggers with one event each. Is this not the case?
 
Status
Not open for further replies.
Top