- Joined
- Jun 20, 2011
- Messages
- 249
JASS:
//! zinc
library OnUnitEvent requires /*
*/ DamageEvent /* hiveworkshop.com/forums/jass-functions-413/snippet-damageevent-186829/
*/ ,UnitIndexer /* hiveworkshop.com/forums/jass-functions-413/system-unit-indexer-172090/
*/ ,RegisterPlayerUnitEvent /* hiveworkshop.com/forums/submissions-414/commonevent-203338/
// A snippet that provides useful tools for incomplete events such as
// Unit Takes Damage or Unit Attacked and completes them with their
// respective opposite event (Unit Deals Damage or Unit Attacks)
***********************************************************************
API
function OnUnitDamage takes unit whichUnit, code callback returns triggercondition
function OnUnitAttack takes unit whichUnit, code callback returns triggercondition
function OnUnitKill takes unit whichUnit, code callback returns triggercondition
// Everytime an unit deals damage, attacks or kills another unit the given condition
// will be fired, use the DamageEvent API to return event values, such as DamageEvent.source.
// Remember that it takes CODE but it must return boolean same as if it were a
// condition. Returns triggercondition, store it in a var if you intend to remove the
// event later.
function RemoveOnUnitDamage takes unit whichUnit, triggercondition whichCondition returns nothing
function RemoveOnUnitAttack takes unit whichUnit, triggercondition whichCondition returns nothing
function RemoveOnUnitKill takes unit whichUnit, triggercondition whichCondition returns nothing
// Takes the previously returned triggercondition and removes the proper event.
function ClearOnUnitDamage takes unit whichUnit returns nothing
function ClearOnUnitAttack takes unit whichUnit returns nothing
function ClearOnUnitKill 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 the global event fires it checks if
// the unit triggering the event matches, 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.
{
trigger a[];
trigger b[];
trigger c[];
//! textmacro OnUnitEvent_MethodSetUp takes NAME,VAR
function OnUnit$NAME$(unit u,code f)->triggercondition{
integer i=GetUnitUserData(u);
if(null==$VAR$[i]) $VAR$[i]=CreateTrigger();
return TriggerAddCondition($VAR$[i],Filter(f));}
function RemoveOnUnit$NAME$(unit u, triggercondition tc){
TriggerRemoveCondition($VAR$[GetUnitUserData(u)],tc);}
function ClearOnUnit$NAME$(unit u){
TriggerClearConditions($VAR$[GetUnitUserData(u)]);}
//! endtextmacro
public{
//! runtextmacro OnUnitEvent_MethodSetUp("Damage","a")
//! runtextmacro OnUnitEvent_MethodSetUp("Attack","b")
//! runtextmacro OnUnitEvent_MethodSetUp("Kill" , "c")
}
function onDamage()->boolean{
TriggerEvaluate(a[DamageEvent.sourceId]);
return false;}
function onAttack()->boolean{
integer i=GetUnitUserData(GetAttacker());
TriggerEvaluate(b[i]);
return false;}
function onDeath()->boolean{
integer i=GetUnitUserData(GetKillingUnit());
TriggerEvaluate(c[i]);
return false;}
function onRemoval()->boolean{
TriggerClearConditions(a[GetIndexedUnitId()]);
TriggerClearConditions(b[GetIndexedUnitId()]);
TriggerClearConditions(c[GetIndexedUnitId()]);
return false;}
module Init{
static method onInit(){
RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_ATTACKED,function onAttack);
RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH,function onDeath);
RegisterEvent(Condition(function onDamage),DamageEvent.ANY);
RegisterUnitIndexEvent(Condition(function onRemoval),UnitIndexer.DEINDEX);}
}
struct Initializer[]{
module Init;}
}
//! endzinc
Last edited: