Custom Healthbar

This bundle is marked as useful / simple. Simplicity is bliss, low effort and/or may contain minor bugs.
  • Like
Reactions: deepstrasz
The following code shows the intended way to utilize the healthbar. Essentially it is a healthbar meant to be displayed only for a few moments before fading away. For best results, disable default healthbars. This can be useful if you are making a deception map where you do not want a player to easily locate where units are via their healthbar. Feel free to modify the model/texture to fit your needs.
JASS:
library HealthBar uses TimerUtils, UnitDex
    globals
        private constant real REFRESH =0.03125
        private constant real ALPHA_RATE = REFRESH * 255 * 2
        private effect array HealthBars
        private real array HealthBarDur
        private real array HealthBarAlpha
        private boolean array Destroy
    endglobals

    private function Move takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local integer id = GetTimerData(t)
        local unit u = GetUnitById(id)
        call BlzSetSpecialEffectPosition(HealthBars[id],GetUnitX(u),GetUnitY(u),BlzGetUnitZ(u))
        if not Destroy[id] then
            set HealthBarDur[id] = HealthBarDur[id] - REFRESH
            if HealthBarDur[id] <= 0 or not UnitAlive(u) then
                set Destroy[id] = true
                set HealthBarAlpha[id] = 255
            endif
        else
            set HealthBarAlpha[id] = HealthBarAlpha[id] - ALPHA_RATE
            call BlzSetSpecialEffectAlpha(HealthBars[id],MathRound(HealthBarAlpha[id]))
            if HealthBarAlpha[id] <= 0 then
                call DestroyEffect(HealthBars[id])
                set HealthBars[id] = null
                set Destroy[id] = false
                call ReleaseTimer(t)
            endif
        endif
        set t = null
        set u = null
    endfunction

    function AddHealthBar takes unit u, real dur, real per returns nothing
        local integer id = GetUnitUserData(u)
        set Destroy[id] = false
        set HealthBarDur[id] = dur
        if HealthBars[id] != null then
            call BlzSetSpecialEffectAlpha(HealthBars[id],255)
        else
            set HealthBars[id] = AddSpecialEffect("HealthBarv18.mdx",GetUnitX(u),GetUnitY(u))
            call BlzSetSpecialEffectScale(HealthBars[id],BlzGetUnitRealField(u,ConvertUnitRealField('ussc')))
            call BlzSetSpecialEffectTimeScale(HealthBars[id],0)
            call TimerStart(NewTimerEx(id),REFRESH,true,function Move)
        endif
        call BlzSetSpecialEffectTime(HealthBars[id],1 - per)
    endfunction
        
endlibrary
Previews
Contents

Custom Healthbar (Model)

Reviews
General Frank
Might be useful to many map makers.
Top