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

[JASS] Need help creating a function

Status
Not open for further replies.
Level 19
Joined
Oct 29, 2007
Messages
1,184
I want to make a function that fades in a unit. I currently have this:

JASS:
function fade takes unit u returns nothing

local integer i = 0

    loop
    
    set i = i + 4

    call SetUnitVertexColor(u, 255, 255, 255, i)
    call TriggerSleepAction(0.03)
    
    exitwhen i > 255
    
    endloop


endfunction

Will this stack if I call this function on multiple units simultaniously? If not, how can I create something equivalent?
 
It will work on multiple units but it may not have the result you want. It will look a bit choppy because of the triggersleepaction. The best results would come from using a timer.

Requires jasshelper:
JASS:
globals
    hashtable fadeHash = InitHashtable()
endglobals

function fadeApply takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local integer i = LoadInteger(fadeHash, GetHandleId(t), 1) + 4
    
    if i <= 255 then
        call SetUnitVertexColor(LoadUnitHandle(fadeHash, GetHandleId(t), 0), 255, 255, 255, i)
        call SaveInteger(fadeHash, GetHandleId(t), 1, i)
    else
        call FlushChildHashtable(fadeHash, GetHandleId(t))
        call PauseTimer(t)
        call DestroyTimer(t)
    endif

    set t = null
endfunction

function fade takes unit u returns nothing
    local timer t = CreateTimer()
    call SaveUnitHandle(fadeHash, GetHandleId(t), 0, u)
    call SaveInteger(fadeHash, GetHandleId(t), 1, 0)
    call TimerStart(t, 0.03, true, function fadeApply)
    set t = null
endfunction

Of course, this can be improved through the use of structs and timer systems etc.. But this should work fine.
 
Status
Not open for further replies.
Top