-
Notifications
You must be signed in to change notification settings - Fork 18
Event Listeners v7
- 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 level in which an event listener is registered determines how long it will stay active.
-
nodeGame: some event listeners are registered by nodeGame before a game is even created. These listeners are never removed.
-
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.
-
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.
-
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.
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.
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
Go back to the wiki Home.
Copyright (C) 2021 Stefano Balietti
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.