79 | | There's a special method (`InitGame`), that gets called when loading a new game, so it can be used for data initialisation. You can initialise any counters or other data there. |
80 | | |
81 | | {{{ |
82 | | #!js |
83 | | Trigger.prototype.InitGame = function(data) |
84 | | { |
85 | | this.executedTriggers = 0; |
86 | | this.killedUnits = 0; |
87 | | this.state = "initialising"; |
88 | | } |
| 79 | Storing data you want to keep track of should happen at the end of you script, and be done in the `Trigger` component. By doing so, you can use the data later on (in the `Trigger` prototype functions) with the `this` keyword. |
| 80 | |
| 81 | {{{ |
| 82 | #!js |
| 83 | // get the cmpTrigger object |
| 84 | var cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger); |
| 85 | |
| 86 | // Add data to it |
| 87 | cmpTrigger.executedTriggers = 0; |
| 88 | cmpTrigger.killedUnits = 0; |
| 89 | cmpTrigger.state = "initialising"; |
112 | | Trigger.prototype.InitGame = function(data) |
| 113 | // get the cmpTrigger object |
| 114 | var cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger); |
| 115 | |
| 116 | // register the trigger directly |
| 117 | var myData = {"enabled": true}; |
| 118 | cmpTrigger.RegisterTrigger("OnPlayerCommand", "MyAction", myData); |
| 119 | }}} |
| 120 | |
| 121 | The first parameter of the trigger is the event on which it will be executed. In the example, the trigger will be executed for every player command. See the table below for a list of all possible events. The second parameter is the name of the action, which has to be defined under the Trigger prototype. And the third part is a data object. For most triggers, this data will be just enabled = true or false. But for more complicated triggers (range triggers, time triggers, ...) this data can contain other elements, such as the the distance of the range trigger, or the interval of the timer. Again, see the data in the table below. |
| 122 | |
| 123 | The combination of event and action name must be unique. This combination can be used to enable and disable triggers. Registering a trigger twice isn't possible, and tou will be warned when you do that. |
| 124 | |
| 125 | When you have your first triggers registered, they can fire actions, but actions can again register new triggers. As this time, it happens within the `Trigger` prototype, you can access `cmpTrigger` simply with `this`. |
| 126 | |
| 127 | {{{ |
| 128 | #!js |
| 129 | Trigger.prototype.MyTriggerRegisteringAction = function(data) |
115 | | this.RegisterTrigger("OnPlayerCommand", "MyAction", myData); |
116 | | }; |
117 | | }}} |
118 | | |
119 | | The first parameter of the trigger is the event on which it will be executed. In the example, the trigger will be executed for every player command. See the table below for a list of all possible events. The second parameter is the name of the action, which has to be defined under the Trigger prototype. And the third part is a data object. For most triggers, this data will be just enabled = true or false. But for more complicated triggers (range triggers, time triggers, ...) this data can contain other elements, such as the the distance of the range trigger, or the interval of the timer. Again, see the data in the table below. |
120 | | |
121 | | The combination of event and action name must be unique. This combination can be used to enable and disable triggers. Registering a trigger twice isn't possible, and tou will be warned when you do that. |
122 | | |
123 | | Enabling and disabling triggers happens as shown: |
124 | | |
125 | | {{{ |
126 | | #!js |
127 | | this.EnableTrigger("OnPlayerCommand", "MyAction"); |
128 | | this.DisableTrigger("OnPlayerCommand", "MyAction"); |
| 132 | this.RegisterTrigger("OnPlayerCommand", "MySecondAction", myData); |
| 133 | }; |
| 134 | }}} |
| 135 | |
| 136 | |
| 137 | Enabling and disabling triggers in action functions happens as shown: |
| 138 | |
| 139 | {{{ |
| 140 | #!js |
| 141 | Trigger.prototype.MySecondAction = function(data) |
| 142 | { |
| 143 | this.EnableTrigger("OnPlayerCommand", "MyAction"); |
| 144 | // OR |
| 145 | this.DisableTrigger("OnPlayerCommand", "MyAction"); |
| 146 | }; |