Alfred Young
Alfred Young

Reputation: 437

Best way to communicate server-generated map to client

I am starting out with lance-gg and am building a game in which a new game map is generated every few minutes. The client game engine needs to receive the generated map. Currently I am registering the map as a serializable object and adding a new instance to the game world when the server starts.

class Map extends serialize.DynamicObject {
    constructor(id, width, height) {
        super(id);
        this.class = Map;
        this.width = width;
        this.height = height;
        this.netScheme = {
            tiles: { type: "CLASSINSTANCE" },
        };
    }
    randomTiles() {
        const tiles = [];
        _.forEach(_.range(this.width), (x) => {
            tiles[x] = [];
            _.forEach(_.range(this.height), (y) => {
                tiles[x][y] = _.random(0, 1);
            });
        });
        return tiles;
    }
}

The map is a double array of integers and might be quite large so I'd like to avoid implementing it as part of a netscheme (which I dont think I'm doing correctly here anyway) as it doesn't need to be updated very often. I would also like to keep a reference to it in the game world to keep it separate from objects that actually change position.

What is the best way to do this?

Upvotes: 2

Views: 281

Answers (1)

Gary Weiss
Gary Weiss

Reputation: 688

Lance will only broadcast game objects if their netscheme'd data has changed. Lance will also broadcast all of the game objects when a new user has connected.

In your case, this means that if the map only changes every few minutes, then you should be fine leaving it as a game object, and relying on Lance to detect changes and broadcast when necessary.

An alternative approach is to emit the Map data directly to users using socket.emit approach as described in this stack overflow question: Sending "secret" data to individual players in Lance Game.

Upvotes: 2

Related Questions