• 🏆 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] system prints (null) instead of unit

Status
Not open for further replies.
Level 23
Joined
Apr 16, 2012
Messages
4,041
I tried to make a script for map that judges who killed unit with damage dealt instead of just last hit so I wrote this:
JASS:
library GetKiller uses UnitIndexer, DmgSys, TimerUtils, GetUnitCountOnMap //by edo494
    
    globals
        private TableArray damage
        private unit array killer
        private integer array lastindex
    endglobals

    function GetDamageDoneToUnit takes unit tounit, unit which returns real
        return damage[GetUnitUserData(tounit)].real[GetUnitUserData(which)]
    endfunction
    
    function GetKiller takes unit deadunit returns unit
        return killer[lastindex[GetUnitUserData(deadunit)]]
    endfunction
    
    function GetKillerId takes unit deadunit returns integer
        return GetUnitId(killer[lastindex[GetUnitUserData(deadunit)]])
    endfunction
    
    private function setKiller takes nothing returns nothing
        local unit u = GetUnitById(GetTimerData(GetExpiredTimer()))
        local integer i = 1
        local integer o = GetTimerData(GetExpiredTimer())
        local integer unitsonmap = GetUnitCount()
        call ReleaseTimer(GetExpiredTimer())
        if IsUnitType(u, UNIT_TYPE_DEAD) then
            loop
                exitwhen i > unitsonmap
                if damage[o].real[i] > damage[o].real[i-1] then
                    set killer[o] = GetUnitById(i)
                endif
                set i = i + 1
            endloop
        endif
        set u = null
    endfunction
    
    private function IncreaseDamageDone takes nothing returns nothing
        local integer take = GetUnitUserData(GetTriggerUnit())
        local integer deal = GetUnitUserData(GetEventDamageSource())
        set damage[take].real[deal] = damage[take].real[deal] + GetEventDamage()
        set lastindex[take] = take
        call TimerStart(NewTimerEx(take), 0, false, function setKiller)
    endfunction
    
    private module init
        static method onInit takes nothing returns nothing
            local code c = function IncreaseDamageDone
            call DmgSys.register(c)
            set damage = TableArray[0x2000]
        endmethod
    endmodule

    private struct s_init extends array
        implement init
    endstruct
    
endlibrary
but when I do:
  • Untitled Trigger 001
    • Events
      • Unit - A unit Dies
    • Conditions
    • Actions
      • Custom script: call BJDebugMsg(GetUnitName(GetKiller(GetTriggerUnit())))
it shows (null) all the time

ps: there is a GetUnitCount() which counts unit on map and the whole DmgSys is wrote by me also but they are flawless, if I print the damage[..].real[..] it prints the damage unit dealt to other unit and GetUnitcount also returns unit on map correctly. lastindex variable is an attempt to overcome UnitIndexer deindexing the unit

Please, if you spot any error I didnt spot let me know
 
Status
Not open for further replies.
Top