Warcraft 3 documentation
vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

[Snippet] RegisterAnyUnitEvent No New Posts Codes & Snippets

Started by
moyack

0 Members and 1 Guest are viewing this topic.

[Snippet] RegisterAnyUnitEvent
on: February 02, 2012, 11:18:36 AM
Category: Execution
Language: vJASS

Description.
A library to reduce the time writing the same shit. I took some code ideas from a similar resource approved here. This one supports all the possibilities in calling the trigger. If user defines the condition and action functions this function will return the respective trigger, so it can be controlled by other instance (just in case).

If the user only sets a condition or action, then it will return null because this trigger handles a lot of conditions and actions and it shouldn't be accessible by the user.


Usage:
For functions without triggersleepaction or waits, it's recommended to use
Code: jass
  1. call RegisterAnyUnitEvent(<Event>, code Function, null)
For functions with triggersleepaction or waits, it's recommended to use
Code: jass
  1. call RegisterAnyUnitEvent(<Event>, null, code Function)
For triggers which condition and action codes must be separated (it could happen) you set this:
Code: jass
  1. private function Conditions takes nothing returns boolean
  2.     return GetSpellAbilityId() == 'A000'
  3. endfunction
  4.  
  5. private function Actions takes nothing returns nothing
  6.     // your stuff...
  7. endfunction
  8.  
  9. private function init takes nothing returns nothing
  10.     local trigger t = RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, function Conditions, function Actions)
  11. endfunction


Actual code
Code: jass
  1. /*
  2. //************************************************************************//
  3. //                    RegisterAnyUnitEvent by moyack                      //
  4. //************************************************************************//
  5. // Code improved with the idea developed by Magtheridon96 , Bribe, azlier //
  6. //************************************************************************//
  7.  
  8. A library to reduce the time writing the same shit. I took some code ideas from a similar
  9. resource approved here. This one supports all the possibilities in calling the trigger.
  10. If user defines the condition and action functions this function will return the respective
  11. trigger, so it can be controlled by other instance (just in case).
  12.  
  13. If the user only sets a condition or action, then it will return null because this
  14. trigger handles a lot of conditions and actions and it shouldn't be accessible by the user.
  15.  
  16.  * For functions without triggersleepaction or waits, it's recommended to use:
  17.     call RegisterAnyUnitEvent(<Event>, code Function, null)
  18.  
  19.  * For functions with triggersleepaction or waits, it's recommended to use:
  20.     call RegisterAnyUnitEvent(<Event>, null, code Function)
  21. */
  22. globals
  23.     private trigger array ts
  24. endglobals
  25.  
  26. function RegisterAnyUnitEvent takes playerunitevent e, code cond, code act returns nothing
  27.     local integer i = GetHandleId(e)
  28.     local trigger t
  29.     if act != null then
  30.         set t = CreateTrigger()
  31.         call TriggerRegisterAnyUnitEventBJ(t, e)
  32.         if cond != null then
  33.             call TriggerAddCondition(t, Filter(cond))
  34.         endif
  35.         call TriggerAddAction(t, act)
  36.         set t = null
  37.     else
  38.         if ts[i] == null then
  39.             set ts[i] = CreateTrigger()
  40.             call TriggerRegisterAnyUnitEventBJ(ts[i], e)
  41.         endif
  42.         call TriggerAddCondition(ts[i], Filter(cond))
  43.     endif
  44. endfunction
  45.  
  46. endlibrary

Examples:
Examples
Condition only
Code: jass
  1. scope test initializer init
  2.  
  3. private function Conditions takes nothing returns boolean
  4.     // your code... please DO NOT use waits or triggersleeaction...
  5.     return false // please always set it to false
  6. endfunction
  7.  
  8. private function init takes nothing returns nothing
  9.     // that's the recommended way, it's faster and nice :)
  10.     call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, function Conditions, null)
  11. endfunction
  12.  
  13. endscope
Action Code Only
Code: jass
  1. scope test initializer init
  2.  
  3. private function Actions takes nothing returns nothing
  4.     // your stuff...
  5. endfunction
  6.  
  7. private function init takes nothing returns nothing
  8.     // It's valid but not as faster as using conditions. Use it when you have in the code
  9.     // Polledwait(), triggersleepaction() or wait()
  10.     call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, null, function Actions)
  11. endfunction
  12.  
  13. endscope
Condition and action separated
Code: jass
  1. scope test initializer init
  2.  
  3. private function Conditions takes nothing returns boolean
  4.     return GetSpellAbilityId() == 'A000'
  5. endfunction
  6.  
  7. private function Actions takes nothing returns nothing
  8.     // your stuff...
  9. endfunction
  10.  
  11. private function init takes nothing returns nothing
  12.     call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, function Conditions, function Actions)
  13. endfunction
  14.  
  15. endscope

Optionally I can add textmacros for other kind of events, but I left it in this way because this is the most used by me.
« Last Edit: December 19, 2017, 07:59:19 PM by moyack »



Re: [Snippet] RegisterAnyUnitEvent
Reply #1 on: February 09, 2012, 12:48:09 PM

http://www.hiveworkshop.com/forums/2098379-post2.html

I'm too lazy to type that out again =P




[Snippet] RegisterAnyUnitEvent
Reply #3 on: July 25, 2013, 07:50:49 AM

A small make up to the first post has been made :)


 

Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...