Changes between Version 9 and Version 10 of Triggers


Ignore:
Timestamp:
Jun 23, 2014, 6:06:41 PM (10 years ago)
Author:
sanderd17
Comment:

re-document the initialisation

Legend:

Unmodified
Added
Removed
Modified
  • Triggers

    v9 v10  
    7777==== Data initialisation ====
    7878
    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 }
     79Storing 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
     84var cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger);
     85
     86// Add data to it
     87cmpTrigger.executedTriggers = 0;
     88cmpTrigger.killedUnits = 0;
     89cmpTrigger.state = "initialising";
    8990}}}
    9091
     
    104105'''Note:''' both the `Trigger` prototype and the data you add directly to `this` can be accessed via `this`. But only the data added directly to `this` will be saved in a saved game. So although it's possible to add other stuff to the prototype than just functions, it shouldn't change throughout the game. Also note that functions can't be saved in saved games. So in short, functions should always be defined under the prototype, and never change.
    105106
    106 == Registering functions ==
     107== Registering triggers ==
    107108
    108109When you have your actions made, you need to bind them to a certain event. This is done via the predefined RegisterTrigger function. Triggers can be enabled in any method, but you usually need to enable some at the start of the game. You can use the following schema:
     
    110111{{{
    111112#!js
    112 Trigger.prototype.InitGame = function(data)
     113// get the cmpTrigger object
     114var cmpTrigger = Engine.QueryInterface(SYSTEM_ENTITY, IID_Trigger);
     115
     116// register the trigger directly
     117var myData = {"enabled": true};
     118cmpTrigger.RegisterTrigger("OnPlayerCommand", "MyAction", myData);
     119}}}
     120
     121The 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
     123The 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
     125When 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
     129Trigger.prototype.MyTriggerRegisteringAction = function(data)
    113130{
    114131    var myData = {"enabled": true};
    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
     137Enabling and disabling triggers in action functions happens as shown:
     138
     139{{{
     140#!js
     141Trigger.prototype.MySecondAction = function(data)
     142{
     143    this.EnableTrigger("OnPlayerCommand", "MyAction");
     144    // OR
     145    this.DisableTrigger("OnPlayerCommand", "MyAction");
     146};
    129147}}}
    130148