Snippet UnitFadeTransparency

Andrewgosu

The Silent Pandaren Helper
Reaction score
716
This is just a simple snippet/(mini) system which allows you to gradually "fade" units over time.


The argument list explanation:

function UnitFadeTransparency takes unit whichUnit, real duration, real startTrans, real endTrans returns nothing

- unit whichUnit - the unit you want to fade
- real duration - the time it takes to fade the unit
- real startTrans - the start transparency in %. (100 for fully visible)
- real endTrans - the end transparency in % (0 for fully invisible)​

An example call using GUI:

Trigger:
  • Actions
    • Set period = (Random real number between 3.00 and 5.00)
    • Set endTrans = (Random real number between 0.00 and 50.00)
    • Custom script: call UnitFadeTransparency(udg_tmpUnit , udg_period, 100 ,udg_endTrans)

(Sorry for pseudo variable...)

N.B! Requires CSData and CSSafety (included in the demo map)

The code:

JASS:
library UNITFADE
    //ver 1.0
    
    globals
        private constant real TIMER_VAL = 0.03125
    endglobals
    
    private struct fadeinfo
        unit fadeWhich
        
        real transAdd
        real transCur = 0
        integer trans
        
        real ticks = 0
        real ticksEnd
    endstruct
    
    private function fadeunit_callback takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local fadeinfo data = GetCSData(t)
        
        set data.ticks = data.ticks + 1
        
        if (data.ticks <= data.ticksEnd) then      
            set data.transCur = data.transCur + data.transAdd   
            if (data.transCur <= -1) then
                set data.transCur = data.transCur + 1
                set data.trans = data.trans - 1
                call SetUnitVertexColor(data.fadeWhich, 255, 255, 255, data.trans)
            else
                set data.transCur = data.transCur + 1
                set data.trans = data.trans + 1
                call SetUnitVertexColor(data.fadeWhich, 255, 255, 255, data.trans)
            endif
        
        else
            call data.destroy()
            call ReleaseTimer(t)
        endif
    endfunction
    
    private function fadeunit_exec takes unit whichUnit, real period, real startTrans, real endTrans returns nothing
        local timer t = NewTimer()
        local fadeinfo data = fadeinfo.create()

        set data.fadeWhich = whichUnit
        set data.transAdd = ((endTrans - startTrans) * TIMER_VAL / period) * 2.55
        set data.trans = R2I(startTrans * 2.55)
        set data.ticksEnd = (period * RAbsBJ(data.transAdd) / TIMER_VAL)
        call SetCSData(t, data)        
        call TimerStart(t, TIMER_VAL, true, function fadeunit_callback)
    endfunction
    
    function UnitFadeTransparency takes unit whichUnit, real duration, real startTrans, real endTrans returns nothing
        call fadeunit_exec.execute(whichUnit, duration, startTrans, endTrans)
    endfunction
endlibrary



Anyway, feedback is appreciated!


And, as always, happy mapping!


Andrewgosu
 

Attachments

  • UnitFadeTransparency_Andrewgosu_2009.w3x
    59.8 KB · Views: 203

Flare

Stops copies me!
Reaction score
662
And why does fadeunit_exec exist? Couldn't you just do everything in the FadeUnitTransparency function? Not really sure why you would want it that way (other than for the sake of starting a new thread, but why would there be a need for that?)

EDIT: Oops, just realised you did mention CSData and CSSafety requirement - was expecting it to be closer to top of post :eek:
 

Darius34

New Member
Reaction score
30
CSData + CSSafety = TimerUtils. Why not use that? :p

This could actually run with no external systems and a single timer, if you want. But then, I suppose lots of people use TimerUtils.

Flare is right; you don't need the wrapper function.

Good work, I guess.
 

Joker(Div)

Always Here..
Reaction score
86
CSData != TimerUtils
TimerUtils = Half CSSafety

Although looking at the code, TimerUtils would make more sense.
 

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
Could you add a function to halt the fade and set it back to 100% visible? I've seen this in the other 2 fade systems I've seen, but not yours.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top