the art of module interfaces

Jass Tutorials

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


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


The trigger evaluation is not necessarily needed in all cases as sometimes the events may be run directly within the struct.