Default Step Properties v7 - nodeGame/nodegame GitHub Wiki

Default Step Properties

Game developers can define their step properties (as many as needed), but some names are reserved by nodeGame. The most important reserved properties are:

  • frame: string|object
    An HTML resource to load in the frame (browser-only). By default, all steps within the same stage re-use the same frame loaded at step 1 of the stage, unless otherwise speecified. the frame is reloaded only between stages, or consecutive rounds. For specific options, see the loadFrame method of the Window API.

  • init: function
    Defines an initialization function executed before each stage / step. Listeners registered here are available throughout the stage / step.

  • exit: function
    Defines an exit function executed before after the stage / step was completed, and before the next one begins. Listeners registered here are deleted immediately.

  • timer: number|object
    Configures the internal timer and Visual Timers objects, if any. If the parameter is a number, it specifies the duration of the timer in milliseconds. If object, it allows full configuration of the timer. For details, see Visual Timer.

  • stepRule: function|string
    Defines the condition for the client to enter into the next step / stage. If the function returns TRUE, the next stage in the game-sequence will be executed.

Further Explanations and Examples

Done callback

The done callback receives the same input parameters of node.done and can modify them or completely halt the procedure to terminate the current step if some conditions are not satisfied.

stager.extendStep('instructions', {
    cb: function() {
       // In this example, we just call node.done after 2 seconds.
       setTimeout(function() {
           // The value passed to node.done is evaluated by the done callback.
           node.done({ foo: 'bar' });
       }, 2000);
    },
    // The done callback evaluates if conditions for terminating the
    // current step are satisfied and/or modifies done parameters.
    done: function(results) {
        // results is equal to { foo: 'bar'}.         
        // If some conditions are not satisfied, the done callback can
        // block the execution by returning false.
        if (results.foo !== 'bar') return false;
        // The done callback can also modify the results that will sent
        // to the server.
        results.foo = 'bar2';
        return results;
        // Notice: for retro-compatibility returning true is ignored and
        // not sent to the server. If you need to send a truthy value,
        // consider sending 1, or [true].
    }
});

Init and Exit callbacks

It is possible to execute callbacks just before and after a certain step is executed. Just add the init and exit properties to the step object. For example:

stager.extendStep('instructions', {
    init: function() {
        // See the next section of the tutorial
        // to know more about the `node.on` command.
        node.on('myEvent', function() {
             console.log('I am valid only during this step');
        });
        console.log('I am executed before the step callback');
    },
    exit: function() {
        console.log('I come after the step is done, before a new ones begins.');
    }
});

Likewise, it is possible to add init and exit callbacks to the stage object as well.

// Notice extend**Stage**.
stager.extendStage('game', {
    init: function() {
        node.on('myEventInGame', function() {
             console.log('I am valid through all the steps and all the ' +
                         'rounds of the stage game.');
        });
        console.log('I am executed before any step in the game stage.');
    },
    exit: function() {
        console.log('I come after the stage is done, before a new one begins.');
    }
});

Important! The init and exit properties behave differently from other properties: they are not inherited by each step inside the stage. This means that, for instance, the init property of the stage is executed only once at the beginning of the stage, regardless of how many rounds and steps the stage has got.

Additional Step properties

  • minPlayers: array [number, function]
    Defines the minimum number of players that need to be connected simultaneously in order to continue the game. As soon as the requirement is not met the callback function is invoked.

  • maxPlayes: array [number, function]
    Defines the maximum number of players that need to be connected simultaneously in order to continue the game. As soon as the requirement is not met the callback function is invoked.

  • exactPlayers: array [number, function]
    Defines an exact number of players that need to be connected simultaneously in order to continue the game. As soon as the requirement is not met the callback function is invoked.

  • globals: object
    Defines properties that will be available at run-time under node.game.globals.

Next

Step Callback Functions

See Also

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