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

the art of module interfaces

moyack · 367

0 Members and 1 Guest are viewing this topic.

the art of module interfaces
on: March 21, 2012, 03:44:14 AM

This will tutorial goes over how to create an interface with a module.

When people create interfaces, they typically use function interfaces, interfaces, or if they are cool, plain old triggers. There is a way to create an interface for a struct without having to rely on dynamic code by cleverly using static ifs.

Let's say that when an event occurs in the root struct, like a unit is indexed or a player types a chat message, it calls certain methods within the derived structs. These methods are normally called via stub methods and extension, function interfaces, or plain interfaces, but they can be called by using triggers and static ifs or by a direct method call in some cases (TimerQueue for example).

Code: jass
  1. struct ParentStruct extends array
  2.     private static trigger eventTrig = CreateTrigger()
  3.     private static trigger fireTrig = CreateTrigger()
  4.     static readonly thistype data = 0
  5.  
  6.     private static method onEvent takes nothing returns boolean
  7.         local integer prevData = data
  8.         set data = 5 //event data
  9.         call TriggerEvaluate(fireTrig)
  10.         set data = prevData
  11.     endmethod
  12.  
  13.     private static method onInit takes nothing returns nothing
  14.         call TriggerAddCondition(eventTrig, Condition(function thistype.onEvent))
  15.     endmethod
  16.  
  17.     static method register takes boolexpr bc returns nothing
  18.         call TriggerAddCondition(fireTrig, bc)
  19.     endmethod
  20. endstruct
  21.  
  22. module Mod
  23.     static if thistype.fire.exists then
  24.         private static method firerer takes nothing returns boolean
  25.             call thistype(ParentStruct.data).fire()
  26.             return false
  27.         endmethod
  28.  
  29.         private static method onInit takes nothing returns nothing
  30.             call ParentStruct.register(Condition(function thistype.firerer))
  31.         endmethod
  32.     endif
  33. endmodule
  34.  

And a struct
Code: jass
  1. struct Bleh extends array
  2.     private method fire takes nothing returns nothing
  3.     endmethod
  4.  
  5.     implement Mod
  6. endstruct
  7.  

The trigger evaluation is not necessarily needed in all cases as sometimes the events may be run directly within the struct.
« Last Edit: December 15, 2017, 12:24:59 PM by moyack »



 

Power of Corruption - A Warcraft III altered melee map   Chaos Realm - The world of Game modders and wc3 addicts     WC3JASS.com - The JASS Vault   Jetcraft - A Starcraft II mod