• 🏆 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!

Fade from Invisibility

Status
Not open for further replies.
Level 6
Joined
Apr 12, 2009
Messages
156
I'm attempting to create a trigger that slowly fades a unit from invisibility (0% transparency) to complete corporeality (100% transparency) over a duration close to 3 seconds. Now I've tried an ugly trigger with waits, but that isn't very good in any way, so I was curious if anyone else knew a better way to achieve this, be it via triggers or an ability. Thanks!
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
JASS:
struct s_fadeunit
    unit    u                   = null
    integer alpha   
    
    integer alphatotal
    real    fadetime
    
    integer                 index
    static s_fadeunit array stack
    static integer          n               = 0 
    
    static timer            refresh         = CreateTimer()
    static constant real    refreshPeriod   = 0.025
    
    
    method onDestroy takes nothing returns nothing
        set thistype.n = thistype.n-1
        set thistype.stack[this.index] = thistype.stack[thistype.n]
        set thistype.stack[this.index].index = this.index
        //* removes the struct from the stack;
    endmethod
    
    
    
    static method onRefresh takes nothing returns nothing
        local integer i
        local s_fadeunit fu
        
        set i = thistype.n-1
        loop
            exitwhen i < 0
            set fu = thistype.stack[i]
            
            if fu != 0 then
                set fu.alpha = fu.alpha - R2I(fu.alphatotal/fu.fadetime)
                if fu.alpha <= 0 then
                    call fu.destroy()
                endif
            
            endif
            
            set i = i-1
        endloop
    
    endmethod
    
    
    
    static method create takes unit u, integer alpha, real fadetime returns s_fadeunit
        local s_fadeunit fu = s_fadeunit.allocate()
        
        set fu.u = u
        set fu.alpha = alpha
        call SetUnitVertexColor(u, 255, 255, 255, alpha)        // i used 255 as the value for Red/Green/Blue for simplicity
        
        set fu.alphatotal = alpha
        set fu.fadetime = fadetime
        
        //* the purpose of this is so that only one timer needs to be used to update the entire stack
        set fu.index = thistype.n
        set thistype.stack[thistype.n] = fu
        set thistype.n = thistype.n+1
        
        return fu
    endmethod
    
    
    
    static method onInit takes nothing returns nothing
        call TimerStart(thistype.refresh, thistype.refreshPeriod, true, function thistype.onRefresh)
    endmethod

endstruct

In order to use it you would do:

JASS:
call s_fadeunit.create( yourUnit, initialAlpha, duration )

The rest should be handled for you.
 
Status
Not open for further replies.
Top