Event Listeners v7 - nodeGame/nodegame GitHub Wiki

  • status: complete
  • version: 7.x
  • follows from: Game Setup

Event listeners are functions that are executed when a specific event occurs. There exists two types of events:

  • internal: they are generated within the game or by the nodeGame engine. The event can have any name; a full list is available of nodeGame events can be found here.

  • incoming: they are triggered by an incoming message from the server. The generated event is in the form of 'in.[target].[action]', where [target] and [action] are properties of the incoming game messages. The listener function receive as parameter the game message sent from the server.

// Registering an internal event listener.
node.on('LOCAL_EVENT', function(a, b) {
    // This is local event.
});
// Triggering a local event.
node.emit('LOCAL_EVENT', 1, 2);

// Registering an incoming event listener
node.on('in.say.DATA', function(msg) {
    // I got a data msg!
});
// Registering an incoming event listener for DATA msgs.
node.on.data('foo', function(msg) {
    // I got a data msg labeled "foo"!
});

The Hierarchy of Event Listeners

The level in which an event listener is registered determines how long it will stay active.

  1. nodeGame: some event listeners are registered by nodeGame before a game is even created. These listeners are never removed.

  2. game: event listeners that are registered in the initialization function of a client type, will stay active as long as the game is not stopped or restarted.

  3. stage: event listeners that are registered in the initialization function of a stage of the game sequence, will be removed after entering the next stage.

  4. step: event listeners that are registered in the initialization function or in the step callback function of a step will be removed as soon as another steps begins.

Incoming event listeners defined in a step

An event listener registered inside the step callback function catches events generated in the same step, as it is disposed before the next step begins.

Events generated by incoming messages arriving in the same step but before the execution of the step callback function (for instance, while loading the frame), are buffered and emitted only after the step callback has been executed.

Accessing the event listeners

All events listeners are managed through the node.events object. Four sub-objects contain the listeners registered at each one of the levels described above:

  • node.events.ng
  • node.events.game
  • node.events.stage
  • node.events.step

Next

Sources

⚠️ **GitHub.com Fallback** ⚠️