• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

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.
This has the same problem as most of these models, animation speed instead of animation number. Though I wonder if you managed to fix the problem with the bar inaccuracy.
This is intentional, because you can now set the animation time for effects, allowing for a light weight model with precise accuracy.
 
Top