With actions you can execute different kind of background-functions like start a new game, save the game, quit the game, switch to another layout and many other functions. You can even define your own actions if necessary. We already used actions in our first example for our own title-screen to start a new game or to quit the game. Now lets take a more closer look at it. For our title-screen we used an action to start a new game:

 

{

      "type": "ui.Button",

      "params": { "text": "New Game", "action": { "name": "newGame" } },

      "style": "titleScreenButton"

}

 

If we click on that button, a new game will start. Sometimes it is also necessary to pass additional data to an action like in the following example:

 

{

      "type": "ui.Button",

      "params": { "text": "Load Game", "action": { "name": "switchLayout", "params": { "name": "loadMenuLayout" } } },

      "style": "titleScreenButton"

}

 

Here we execute the action "switchLayout" which switches to a new layout. But we need to specify the name of the layout we want to switch to.

 

The logic of an action is defined in the action's target. By default the target is the scene-component like Component_LayoutSceneBehavior or Component_GameSceneBehavior. In the above example the action "newGame" is defined in Component_LayoutSceneBehavior script just as a regular method.

 

newGame: (sender, params) ->

        ...........................

 

A method which is called as an action-method should always provide two parameters: sender and params. The sender is the object which triggered this action, in our case it would be the button-object. The params-objects contains the data we passed to the action via the params-attribute.

 

So the name of the action must match the name of one of the target object's methods. The method takes the sender object as first and the params-object as second parameter.

 

It is not necessary but recommended to use "action_" prefix for an action-method like "action_myMethod" to make clear that this method can be safely executed as an action-method.