Command

Updated for v0.3.3

A command consists of zero or more properties and values to be sent from the game client to the server instance.

To make a command that is compatible with nengi, do the following:

ExampleCommand

class ExampleCommand {
    constructor(x, y, isMouseDown) {
        this.mouseX = x
        this.mouseY = y
        this.isMouseDown = isMouseDown
    }
}
ExampleCommand.protocol = {
    mouseX: nengi.Number,
    mouseY: nengi.Number,
    isMouseDown: nengi.Boolean
}

After creating this definition, be sure to add it to the nengiConfig protocols.

nengiConfig

    commands: [
        ['ExampleCommand', require('./path/to/ExampleCommand')]
    ],

usage from the game client

client.addCommand(new ExampleCommand(40, 50, false))

Add as many commands as needed throughout the course of a frame on the client, and then invoke client.update() at the end of the frame.

See the manual page for nengi.Client for more information about the command api.

serverside

var cmd = null
while (commandSet = instance.getNextCommand()) {
    var tick = commandSet.tick
    var client = commandSet.client // client who sent the command

    for (var i = 0; i < commandSet.commands.length; i++) {
        var command = commandSet.commands[i] // the actual command

        if (command.protocol.name === 'ExampleCommand') {
            // { mouseX: 40, mouseY: 50, isMouseDown: false, protocol: { name: 'ExampleCommand', ... }}
        }
    }
}

From here the command can be used to affect the game state on the server.