1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. We have recently started the 16th edition of the Mini Mapping Contest. The theme is mini RPG. Do check it out and have fun.
    Dismiss Notice
  4. Dismiss Notice
  5. The Highway to Hell has been laid open. Come along and participate in the 5th Special Effect Contest.
    Dismiss Notice
  6. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[JASS] Need help creating a function

Discussion in 'Triggers & Scripts' started by Megafyr, Jul 11, 2012.

  1. Megafyr

    Megafyr

    Joined:
    Oct 29, 2007
    Messages:
    957
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    I want to make a function that fades in a unit. I currently have this:

    Code (vJASS):
    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?
     
  2. Mr_Bean

    Mr_Bean

    Joined:
    Feb 11, 2011
    Messages:
    1,823
    Resources:
    6
    Maps:
    2
    Spells:
    4
    Resources:
    6
    Does this script work? I think using TriggerSleepAction isn't too good, so you should use a struct with timers if you want it to work for multiple units at the same time.
     
  3. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,426
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    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:
    Code (vJASS):
    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.
     
  4. Megafyr

    Megafyr

    Joined:
    Oct 29, 2007
    Messages:
    957
    Resources:
    1
    Tutorials:
    1
    Resources:
    1
    Thanks alot! Thank was exactly what I needed.