Snippet OnUnitDamage

Dirac

22710180
Reaction score
147
For a more advanced version (with OnAttack and OnKill events) visit this link:
http://www.hiveworkshop.com/forums/submissions-414/snippet-onunitevent-204367/
JASS:
library OUD uses /*

*/  AIDS    /* thehelper.net/forums/showthread.php/130752-Advanced-Indexing-Data-Storage
*/  Damage  /* thehelper.net/forums/showthread.php/131287-Damage

//  A snippet that provides useful tools for registering when a specific
//  unit deals damage. Great for maps that need good damage filtering.
***********************************************************************

    API

    function OnUnitDamage takes unit whichUnit, boolexpr condition returns triggercondition
//      Everytime an unit deals damage the given condition will be fired, uses the
//      same API for returning values as DamageEvent. Returns a triggercondition if
//      you wish to later undo this.
    
    function RemoveOnUnitDamage takes unit whichUnit, triggercondition condition returns nothing
//      Takes the triggercondition previously returned by the OnUnitDamage call and
//      removes it from the evaluation list.
    
    function ClearOnUnitDamage takes unit whichUnit returns nothing
//      This happens automatically when the unit is deindexed, but if you wish to
//      clear all the evaluation list for a given unit, use this.

**********************************************************************/

//  How does it work: it creates a new trigger the first time you attach a condition
//  to an unit, this trigger will never be destroyed, but constantly recycled, and it
//  adds the given boolexpr as a condition to it, this trigger is stored inside an array
//  with the unit's user data as it's index. Every time an unit takes damage it checks if
//  the damage source's index trigger exists, if so, it fires it and therefore all the
//  conditions are evaluated. If the unit is deindexed the trigger's condition are cleared
//  and it's ready for reuse for the next unit.

    globals
        private trigger array e
    endglobals
    private function onDamage takes nothing returns boolean
        local integer i=GetUnitUserData(GetEventDamageSource())
        if not(null==e<i>) then
            call TriggerEvaluate(e<i>)
        endif
        return false
    endfunction
    function RemoveOnUnitDamage takes unit whichUnit, triggercondition condition returns nothing
        call TriggerRemoveCondition(e[GetUnitUserData(whichUnit)],condition)
    endfunction
    function ClearOnUnitDamage takes unit whichUnit returns nothing
        call TriggerClearConditions(e[GetUnitUserData(whichUnit)])
    endfunction
    function OnUnitDamage takes unit whichUnit, boolexpr condition returns triggercondition
        local integer i=GetUnitUserData(whichUnit)
        if null==e<i> then
            set e<i>=CreateTrigger()
        endif
        return TriggerAddCondition(e<i>,condition)
    endfunction
    private struct OUD_Struct extends array
        private method AIDS_onDestroy takes nothing returns nothing
            call DestroyTrigger(e[GetUnitUserData(.unit)])
        endmethod
        private static method AIDS_onInit takes nothing returns nothing
            local trigger trig=CreateTrigger()
            call Damage_RegisterEvent(trig)
            call TriggerAddCondition(trig,Condition(function onDamage))
            set trig=null
        endmethod
        //! runtextmacro AIDS()
    endstruct
endlibrary</i></i></i></i></i>
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top