| 34 | This should show a yellow warning in the top left of your screen when the map loading is finished. |
| 35 | |
| 36 | '''Tip:''' See [wiki:Logging Logging] for more logging options. |
| 37 | |
| 38 | == Writing the trigger scripts == |
| 39 | |
| 40 | Trigger scripts are executed when the map is completely loaded, but right before the game starts. So you can already access all existing entities and their properties s.a. position, owner, ... This is the right time to bind actions to certain events, a.k.a. registering triggers. |
| 41 | |
| 42 | === Action functions === |
| 43 | |
| 44 | Actions are regular JavaScript functions, registered under the `Trigger` prototype. |
| 45 | |
| 46 | {{{ |
| 47 | #!js |
| 48 | Trigger.prototype.MyAction = function(data) |
| 49 | { |
| 50 | // Do Something |
| 51 | }; |
| 52 | }}} |
| 53 | |
| 54 | Note that these functions only exist, and aren't executed by default. They're only executed when bound to a trigger, or when a different function in your trigger calls it. Chaining functions like the following example is possible, and a good way to split up complicated logic. |
| 55 | |
| 56 | {{{ |
| 57 | #!js |
| 58 | Trigger,prototype.MyAction = function(data) |
| 59 | { |
| 60 | var entities = this.GetRelevantEntities(data.player); |
| 61 | // Do something with those entities |
| 62 | }; |
| 63 | |
| 64 | Trigger.prototype.GetRelevantEntities = function(player) |
| 65 | { |
| 66 | // calculate something |
| 67 | return relevantEntities; |
| 68 | }; |
| 69 | }}} |
| 70 | |
| 71 | The `this` keyword refers to the `Trigger` object anything defined under the prototype + additional data, which makes it easy to use. |
| 72 | |
| 73 | === Keeping track of data === |
| 74 | |
| 75 | Quite often, you'll want to keep track of data that changes throughout the game (number of units killed, number of times a certain trigger is executed, ...). As this data changes during the game, it's important that the last saved data is also loaded when loading a saved game, or when a player rejoins in a multiplayer game. Keeping track of data also happens under the `Trigger` object, so with the `this` keyword. |
| 76 | |
| 77 | ==== Data initialisation ==== |
| 78 | |
| 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 | } |
| 88 | }}} |
| 89 | |
| 90 | ==== Data usage ==== |
| 91 | |
| 92 | After data initialisation, you can just use it in other trigger functions. |
| 93 | |
| 94 | {{{ |
| 95 | #!js |
| 96 | Trigger.prototype.MyAction = function(data) |
| 97 | { |
| 98 | this.executedTriggers++; // will enlarge the counter with one |
| 99 | this.killedUnits += TriggerHelper.GetKilledEntities(data); // execute a function with separate logic, and add it to a counter |
| 100 | } |
| 101 | }}} |
| 102 | |
| 103 | '''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. |
| 104 | |
| 105 | == Registering functions == |