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

[vJASS] UnitEvents

Status
Not open for further replies.
Level 11
Joined
Apr 6, 2008
Messages
760
Im trying out Unitevent. I really dont know how to use this system. I looked thru the demo code and did the same for remove. It wont trigger when i remove a unit that is registered.

So what i need is this system to unregister the unit that is removed



JASS:
library KMS uses UnitIndexer, Table, UnitEvent

    private struct Data extends array
        readonly UnitIndex Unit
        
        static Table T
        
        private static integer instanceCount = 0
        private static thistype recycle = 0
        private thistype recycleNext
        
        static method create takes unit u returns thistype
            local thistype this
            
            if (recycle == 0) then
                set instanceCount = instanceCount + 1
                set this = instanceCount
            else
                set this = recycle
                set recycle = recycle.recycleNext
            endif
            
            set Unit = GetUnitId(u)
            
            if Unit != 0 then
                call Unit.lock()
            else
                call this.destroy()
                return 0
            endif
            
            set T[GetUnitId(u)] = this
            call BJDebugMsg("registered")
            return this
        endmethod
        
        static method Unregister takes unit u returns nothing
            local thistype this = T[GetUnitId(u)]
            
            call Unit.unlock()
            call T.remove(GetUnitId(u))
            call BJDebugMsg("unregistered")
            call .destroy()
        endmethod
        
        method destroy takes nothing returns nothing
            set recycleNext = recycle
            set recycle = this
        endmethod
        
        
        static method onInit takes nothing returns nothing
            set T = Table.create()
        endmethod
        
        // This wont trigger
        private method remove takes nothing returns nothing
            call Unregister(unit)
        endmethod
        
        // This wont trigger neither
        private static method filter takes unit u returns boolean
            call BJDebugMsg("Filter")
            return T.has(GetUnitId(u))
        endmethod
        
        implement UnitEventStruct
        
    endstruct
    
    public function RegisterUnitOutputs takes unit u returns nothing
        call Data.create(u)
    endfunction
    
    public function UnregisterUnitOutputs takes unit u returns nothing
        call Data.Unregister(u)
    endfunction
    
    public function GetUnitTable takes unit u returns Data
        return Data.T[GetUnitId(u)]
    endfunction
    
endlibrary
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
If the "filter" method isn't firing, ".remove" won't fire either.

You are adding work to yourself.

Don't call methods from above where the method is declared. This should throw a syntax error in most cases but with structs it allows really bad coding practices at the expense of a really poorly compiled code.

The .remove method should pass its "local thistype this" argument to the "Unregister" function so you don't call GetUnitId from that particular function. You are adding work to yourself here, too.

But really, if the "filter" method doesn't get called, ever, then you should message Nestharus because it would be his system that isn't working in that case. Either that or you are using an older version of Unit Indexer.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
I'll look into it

edit
works for me

JASS:
library KMS uses UnitIndexer, Table, UnitEvent

    private struct Data extends array
        readonly UnitIndex Unit
        
        static Table T
        
        private static integer instanceCount = 0
        private static thistype recycle = 0
        private thistype recycleNext
        
        static method create takes unit u returns thistype
            local thistype this
            
            if (recycle == 0) then
                set instanceCount = instanceCount + 1
                set this = instanceCount
            else
                set this = recycle
                set recycle = recycle.recycleNext
            endif
            
            set Unit = GetUnitId(u)
            
            if Unit != 0 then
                call Unit.lock()
            else
                call this.destroy()
                return 0
            endif
            
            set T[GetUnitId(u)] = this
            call BJDebugMsg("registered")
            return this
        endmethod
        
        static method Unregister takes unit u returns nothing
            local thistype this = T[GetUnitId(u)]
            
            call Unit.unlock()
            call T.remove(GetUnitId(u))
            call BJDebugMsg("unregistered")
            call .destroy()
        endmethod
        
        method destroy takes nothing returns nothing
            set recycleNext = recycle
            set recycle = this
        endmethod
        
        
        static method onInit takes nothing returns nothing
            set T = Table.create()
        endmethod
        
        // This wont trigger
        private method remove takes nothing returns nothing
            call Unregister(unit)
        endmethod
        
        // This wont trigger neither
        private static method filter takes unit u returns boolean
            call BJDebugMsg("Filter")
            return T.has(GetUnitId(u))
        endmethod
        
        implement UnitEventStruct
        
    endstruct
    
    function RegisterUnitOutputs takes unit u returns nothing
        call Data.create(u)
    endfunction
    
    function UnregisterUnitOutputs takes unit u returns nothing
        call Data.Unregister(u)
    endfunction
    
    function GetUnitTable takes unit u returns Data
        return Data.T[GetUnitId(u)]
    endfunction
    
endlibrary

struct tester extends array
    private static method onInit takes nothing returns nothing
        local unit u = CreateUnit(Player(0), 'hfoo', WorldBounds.centerX, WorldBounds.centerY, 270)
        
        call RegisterUnitOutputs(u)
        
        call RemoveUnit(u)
        
        set u = null
    endmethod
endstruct

Displays registered, filter, and then unregistered

You've obviously installed it incorrectly ; )

edit
Also, you don't need to lock the unit index for what you are doing.
 
Status
Not open for further replies.
Top