• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJass] UnitFadeSystem (UFS)

Level 1
Joined
Jun 20, 2009
Messages
1
Hey there,

this is my first vJass system, and it seems to work fine.
The only requirement is PerfectUnitIndexing (PUI) by Cohadar
To know what it does and so on read the comments made in the script.

JASS:
library Fade initializer Init_Fade requires PUI
// --- FadeUnitSystem by Jimmy --- //
//  Requirements:
//      *PUI
//      *JNGP 
//
//  Setup:
//      (Currently not available. Will add something when I have the time.)
//
//  Purpose:
//      *This is a system to prevent Units from Spawning/Removing without 
//       any effect. I hate the way blizzard spawns units!
// 
//  How to use:
//      There are 4 functions in this script:
//          function UnitFadeOutSimple takes unit whichUnit returns nothing
//          -- Fades out the given unit over 1 second. Looks better than
//             simply removing the unit. REMOVES THE UNIT AT THE END OF FADING!
//      
//          function UnitFadeInSimple takes unit whichUnit returns nothing
//          -- Fades in an existing unit over 1 second. You don't have to
//             change the starting alpha-value to 0.
//
//          function UnitFadeOutComplex takes unit whichUnit, integer afterTimeInMs returns nothing
//          -- Does the same as UnitFadeOutSimple but with a delay (given in ms). 
//             YOU CAN USE THIS IN FORGROUP AND LOOPS!
//
//          function UnitFadeInComplex takes unit whichUnit, integer afterTimeInMs returns nothing
//          -- Does the same as UnitFadeInSimple but with a delay (given in ms).
//             YOU CAN USE THIS IN FORGROUP AND LOOPS!
//
//  Pros:
//      *Better Effect than standard-blizzard-removing/-spawning.
//      *Uses no Unique resources and is full MUI.
//      *You can use this system for special-effect-dummys and buildings, too.
//      *Easy to use.
//      *Low-Performance
//      *Uses no return-bug :)
//
//  Cons:
//      *Needs PUI to be implemented.
//      *Currently no Options. Will add then later.
//      *The Shadows are not influenced by fading. Only from removing. :(
//      *Don't know more. Give feedback to increase this list ;)
//
//  System:
//      The system itself is extreme easy-going.
//      All it does is changing the vertex-coloring in .01 sec steps for all units in the saved groups.
//      The groups are made of given units from the function described at top.
//
//  Thanks to:
//      *Cohadar for his brilliant PUI-System and its header i've copied, pasted and changed^^
//      *Vexorian for simply being brilliant ;)
//      *The fact, I have currently no hobbies -.-
//      *Blizzard Entertainment for creating W3 and it's editor.
//
//  How to Import:
//      Create a Trigger named Fade, convert it to custom text and replace the content with this script.
//
//  Last word(s):
//      GL and HF
// ------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------------------- //
globals 
    private integer array VertexAlpha
    private unit TempUnit
    private group FadeOutUnits
    private group FadeInUnits
    trigger Fade
endglobals
// -- Fade Functions -- //
function UnitFadeInSimple takes unit whichUnit returns nothing
    set VertexAlpha[GetUnitIndex(whichUnit)] = 100
    call SetUnitVertexColor(whichUnit, 255, 255, 255, 0)
    call GroupAddUnit(FadeInUnits, whichUnit)
    call EnableTrigger(Fade)
endfunction

function UnitFadeInComplex takes unit whichUnit, real afterTimeInMs returns nothing
    if(IsUnitInGroup(whichUnit, FadeOutUnits))then
        debug call BJDebugMsg("Error: Unit ("+GetUnitName(whichUnit)+") is fading out at the moment!")
        return
    endif
    if(afterTimeInMs <0)then
        call UnitFadeInSimple(whichUnit)
        return
    endif
    set VertexAlpha[GetUnitIndex(whichUnit)] = 100 + R2I(afterTimeInMs / 10)
    call SetUnitVertexColor(whichUnit, 255, 255, 255, 0)
    call GroupAddUnit(FadeInUnits, whichUnit)
    call EnableTrigger(Fade)
endfunction

function UnitFadeOutSimple takes unit whichUnit returns nothing
    if (IsUnitInGroup(whichUnit, FadeInUnits))then
        debug call BJDebugMsg("Unit is fading in at the moment")
        return
    endif
    call SetUnitVertexColor(whichUnit, 255, 255, 255, 255)
    set VertexAlpha[GetUnitIndex(whichUnit)] = 0
    call GroupAddUnit(FadeOutUnits, whichUnit)
    call EnableTrigger(Fade)
    set whichUnit = null
endfunction

function UnitFadeOutComplex takes unit whichUnit, real afterTimeInMs returns nothing
    if(IsUnitInGroup(whichUnit, FadeInUnits))then
        debug call BJDebugMsg("Error: Unit ("+GetUnitName(whichUnit)+") is fading in at the moment!")
        return
    endif
    if(afterTimeInMs <0)then
        call UnitFadeOutSimple(whichUnit)
        return
    endif
    call SetUnitVertexColor(whichUnit, 255, 255, 255, 255)
    set VertexAlpha[GetUnitIndex(whichUnit)] = 0 - R2I(afterTimeInMs / 10)
    call GroupAddUnit(FadeOutUnits, whichUnit)
    call EnableTrigger(Fade)
endfunction

//-- FadeIN --//
private function Conditions takes nothing returns boolean
    if((CountUnitsInGroup(FadeInUnits) == 0) and (CountUnitsInGroup(FadeOutUnits) == 0))then
        call DisableTrigger(GetTriggeringTrigger())
        return false
    endif
    return true    
endfunction

private function ForGroupActionsIn takes nothing returns nothing
    set TempUnit = GetEnumUnit()
    set VertexAlpha[GetUnitIndex(TempUnit)] = VertexAlpha[GetUnitIndex(TempUnit)] -1
    call SetUnitVertexColor( TempUnit, 255, 255, 255, PercentTo255(100 - VertexAlpha[GetUnitIndex(TempUnit)])) 
    if ( VertexAlpha[GetUnitIndex(TempUnit)] == 0) then
        call GroupRemoveUnit(FadeInUnits, TempUnit )
    endif
    set TempUnit = null    
endfunction

private function ForGroupActionsOut takes nothing returns nothing
    set TempUnit = GetEnumUnit()
    if(IsUnitInGroup(TempUnit, FadeInUnits))then
        call GroupRemoveUnit(FadeOutUnits, TempUnit )
    else
        call SetUnitVertexColor( TempUnit, 255, 255, 255, PercentTo255(100 - VertexAlpha[GetUnitIndex(TempUnit)]) )
        set VertexAlpha[GetUnitIndex(TempUnit)] = VertexAlpha[GetUnitIndex(TempUnit)] + 1 
        if ( VertexAlpha[GetUnitIndex(TempUnit)] == 100 and (not (IsUnitInGroup(TempUnit, FadeInUnits)))) then
            call GroupRemoveUnit(FadeOutUnits, TempUnit )
            call RemoveUnit(TempUnit)
        endif
    endif
    set TempUnit = null
endfunction

private function Actions takes nothing returns nothing
    call ForGroup( FadeOutUnits, function ForGroupActionsOut )
    call ForGroup( FadeInUnits, function ForGroupActionsIn )
    if (CountUnitsInGroup(FadeOutUnits) == 0 and CountUnitsInGroup(FadeInUnits) == 0)then
        call DisableTrigger(GetTriggeringTrigger())
    endif
endfunction

private function Init_Fade takes nothing returns nothing
    set Fade = CreateTrigger()
    set FadeInUnits = CreateGroup()
    set FadeOutUnits = CreateGroup()
    call DisableTrigger(Fade)
    call TriggerRegisterTimerEvent(Fade, 0.01, true)
    call TriggerAddCondition(Fade, Condition(function Conditions))
    call TriggerAddAction(Fade, function Actions)
endfunction
endlibrary

A test-map is attached, but not required to download.

Jimmy17

PS: Btw. my true nickname is just Jimmy without 17, so the author given in the code is me :D
 

Attachments

  • FadeTestMap.w3x
    26.6 KB · Views: 67
  • Fade as the library name is too generic, make it something unique.
  • (FUS) - Shouldn't it be UFS?
  • Your effect for spawning units is constant, you made nothing configurable.
  • Why is Fade (the trigger) public?
  • Your 0.01 interval for the timer should be 0.03 or greater.
  • Instead of TriggerRegisterTimerEvent, you should have just used a timer. This will save you a trigger handle and adding the conditions/actions function calls.
 
This is very laggy - CountUnitsInGroup loops through the whole group to count it - this means the system will get much slower if lots of units are fading, especially at 0.01 timer interval. Instead of doing CountUnitsInGroup you should have global integers which increase when a unit is added and decrease when a unit is removed from the group.
 
Top