System FadeUnitTimed

Larcenist

REP: Respect, Envy, Prosperity?
Reaction score
211
Out of pure boredom I sat down and started to code this little system, and this is the result.

What does the system do?


FadeUnitTimed allows you to make a unit fade from A transparency to B transparency over C seconds, and then fade in/out from B transparency back to A transparency over D seconds.

Confusing? Let's have us an example then will we?
JASS:

call FadeUnitTimed(GetTriggerUnit(), 100, 100, 100, 0, 100, 2, true, 4)


1) GetTriggerUnit(). This is the unit that will be faded.

2) 100, 100, 100. This is the values for red, green and blue of the unit, entered in percentual values to make things easier for GUI users. Unless you've triggered some tinting on your unit just use 100 in each of these three fields.

3) 0, 100. This is the start and end transparency of the unit, also in percent. In my case the unit starts with 0% transparency and ends with 100% transparency.

4) 2, true, 4. The first number here, 2, is how many seconds it will take for the unit to fade from (in my case) 0 to 100%. The next value, true, is a boolean to tell the system if you want the unit to regain it's starting transparency after the fade. The last number is the time it takes to regain the starting transparency (4 seconds to go from 100% to 0% transparency in my case).


System code:

Note: Coded in vJASS and requires NewGen WE in order to work.
JASS:

//==============================================================================================================\\
//                                                                                                              \\
//                        FadeUnitTimed library v2.0c by Larcenist.                                             \\
//                                                                                                              \\
//==============================================================================================================\\

//==============================================================================================================\\
//                                                                                                              \\
//       Implementation instructions: Copy this trigger, FadeUnitTimed and place it into your map.              \\
//                                                                                                              \\
//==============================================================================================================\\

//==============================================================================================================\\
//                                Available functions:                                                          \\
//                                                                                                              \\
//==============================================================================================================\\
//                                                                                                              \\
//FadeUnitTimed(unit, red, green, blue, start transparency, end transparency, fadetime, turnback, returnrate)   \\
//                                                                                                              \\
//red, green and blue values: Real values that ranges from 0 to 100. This is the percentual tinting of the unit \\
//that is being called, 100 in each field is normal tinting. Only use other values than 100 if your unit has    \\
//triggered tinting since the change will be permanent.                                                         \\
//                                                                                                              \\
//start transparency: real value that ranges from 0 to 100. The percentual initial fading of the unit that is   \\
//being called, 0 means no initial fading.                                                                      \\
//                                                                                                              \\
//end transparency: real value that ranges from 0 to 100. The percentual fading the unit that is being called   \\
//will have when the effect ends, 100 means 100% transperancy.                                                  \\
//                                                                                                              \\
//fadetime: real value that indicates the time for the full effect to finish. Calculated in seconds. if turnback\\
//equal to false this will do nothing.                                                                          \\
//                                                                                                              \\
//turnback: Boolean value, true means that the unit regains normal transperancy when the effect finishes,       \\ 
//false means that the unit retain it's new transparency.                                                       \\
//                                                                                                              \\
//returnrate: Real value that indicates the time for the unit to return to the entered start transparency.      \\
//                                                                                                              \\
//==============================================================================================================\\



library FadeUnitTimed

private struct SData
    unit fadeunit
    real currentfaderate
    real endfaderate
    real fadeamount
    real returnrate
    real startrate
    integer red
    integer green
    integer blue
    boolean turnback
    boolean fadeout
endstruct

globals
    private timer array Timer
    private SData array Data
    
    private integer Counter = 0
    private real TimerPeriod = 0.03125
endglobals

function FadeUnitTimedReturnCallback takes nothing returns nothing
    local SData data
    local integer i = Counter
    local integer Alpha
    local boolean Check
    loop
        exitwhen i <= 0
        if Timer<i> == GetExpiredTimer() then
            set data = Data<i>
            if data.fadeout == true then
                set data.currentfaderate = data.currentfaderate - data.fadeamount
                set Check = data.currentfaderate &lt; data.endfaderate
            else
                set data.currentfaderate = data.currentfaderate + data.fadeamount
                set Check = data.currentfaderate &gt; data.endfaderate
            endif
            if Check then
                call PauseTimer(Timer<i>)
                call data.destroy()
                call DestroyTimer(Timer<i>)
                set Timer<i> = null
                set Timer<i> = Timer[Counter]
                set Data<i> = Data[Counter]
                set Counter = Counter - 1
            else
                set Alpha = R2I((100 - data.currentfaderate) * 255 * 0.01)
                
                call SetUnitVertexColor(data.fadeunit, data.red, data.green, data.blue, Alpha)
            endif
        endif
        set i = i - 1
    endloop
            
endfunction

function FadeUnitTimedCallback takes nothing returns nothing
    local SData data
    local integer i = Counter
    local integer Alpha
    local boolean Check
    loop
        exitwhen i &lt;= 0
        if Timer<i> == GetExpiredTimer() then
            set data = Data<i>
            if data.fadeout == true then
                set data.currentfaderate = data.currentfaderate + data.fadeamount
                set Check = data.currentfaderate &gt; data.endfaderate
            else
                set data.currentfaderate = data.currentfaderate - data.fadeamount
                set Check = data.currentfaderate &lt; data.endfaderate
            endif
            if Check then
                call PauseTimer(Timer<i>)
                if data.turnback == true then
                    set data.endfaderate = data.startrate
                    if data.fadeout == true then
                        set data.fadeamount = ((data.currentfaderate - data.endfaderate) / data.returnrate) * TimerPeriod
                    else
                        set data.fadeamount = ((data.endfaderate - data.currentfaderate) / data.returnrate) * TimerPeriod
                    endif
                    call TimerStart(Timer<i>, TimerPeriod, true, function FadeUnitTimedReturnCallback)
                else
                    call DestroyTimer(Timer<i>)
                    call data.destroy()
                    set Timer<i> = null
                    set Timer<i> = Timer[Counter]
                    set Data<i> = Data[Counter]
                    set Counter = Counter - 1
                endif
            else
                set Alpha = R2I((100 - data.currentfaderate) * 255 * 0.01)
                
                call SetUnitVertexColor(data.fadeunit, data.red, data.green, data.blue, Alpha)
            endif
        endif
        set i = i - 1
    endloop
            
endfunction

function FadeUnitTimed takes unit u, real red, real green, real blue, real startfade, real endfade, real fadetime, boolean turnback, real returnrate returns nothing
    local SData data
    local integer Alpha
    local real FadeTime
    
    set Counter = Counter + 1
    
    if Timer[Counter] == null then
        set data = SData.create()
        set Timer[Counter] = CreateTimer()
        set Data[Counter] = data
        set data.fadeunit = u
        set data.endfaderate = endfade
        set data.startrate = startfade
        if fadetime &lt; TimerPeriod then
            set FadeTime = TimerPeriod
        else
            set FadeTime = fadetime
        endif
        if startfade &lt; endfade then
            set data.fadeamount = ((endfade - startfade) / FadeTime) * TimerPeriod
            set data.currentfaderate = startfade
            set data.startrate = startfade
            set data.fadeout = true
        else
            set data.fadeamount = ((startfade - endfade) / FadeTime) * TimerPeriod
            set data.currentfaderate = startfade
            set data.fadeout = false
        endif
        set data.red = R2I(red * 255 * 0.01)
        set data.green = R2I(green * 255 * 0.01)
        set data.blue = R2I(blue * 255 * 0.01)
        set data.turnback = turnback
        if returnrate &lt; TimerPeriod then
            set data.returnrate = TimerPeriod
        else
            set data.returnrate = returnrate
        endif
        
        set Alpha = R2I((100 - startfade) * 255 * 0.01)
        if Alpha &lt; 0 then
            set Alpha = 0
        elseif Alpha &gt; 255 then
            set Alpha = 255
        endif
    
        call SetUnitVertexColor(u, data.red, data.green, data.blue, Alpha)
        call TimerStart(Timer[Counter], TimerPeriod, true, function FadeUnitTimedCallback)
    else
        call BJDebugMsg(&quot;Timer is not nulled or still in use&quot;)
    endif
    
endfunction

endlibrary</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>


Version notes:
Version 2.0c: Fixed a hardly noticable mini-bugg with units fading out.
Version 2.0b: Fixed some stupid "bug" with invisible units flashing before starting to fade in.
Version 2.0: System now supports fading both in and out.
Version 1.0: Initial release.
 

Attachments

  • FadeUnitTimed v2.0c.w3x
    21.4 KB · Views: 181

Larcenist

REP: Respect, Envy, Prosperity?
Reaction score
211
Does that one allow a simple fade-back to the original transparency of the unit that's being faded without any extra functions and stuff? In that case I guess I missed that.

This system was made to be able to work without any other special system-requirements too.
 
Reaction score
456
>Does that one allow a simple fade-back to the original transparency of the unit that's being faded without any extra functions and stuff?
Yes if I understood what you mean. It allows you to do anything to the faded unit.

>This system was made to be able to work without any other special system-requirements too.
Usually people have some kind of an attachement system in their map. So it's not a problem :p..

But let's not talk about that system anymore.

..........

You've inlined the function PercentToInt many times. You should not.
 

Larcenist

REP: Respect, Envy, Prosperity?
Reaction score
211
Yeah I kinda noticed that going through the code too. I'm currently working on a new version that'll allow fading in both directions using nothing but the three main-functions, FadeUnitTimed and the two callback functions. I'll clean up those PercentToInt in the next version that hopefully includes this new update.
 

Larcenist

REP: Respect, Envy, Prosperity?
Reaction score
211
Updated:

The system now supports both fading units in and out with a turnback. I didn't really change a lot just removed some useless lines, so I'm pretty confident there is a lot of optimization possibilities here.

Please report any bugs you find here, the only one I know of just now is that if you spam this system 5-6 times / second it wont pass the data properly, still working on this issue.
 

Larcenist

REP: Respect, Envy, Prosperity?
Reaction score
211
Updated:

Fixed some minor details with the system. Now testable for both fading in/out with and without returning transparency.
 
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