boardgameio / boardgame.io

State Management and Multiplayer Networking for Turn-Based Games
https://boardgame.io
MIT License
9.93k stars 707 forks source link

[Question] Additional config for a game #448

Closed DrummerSi closed 4 years ago

DrummerSi commented 4 years ago

How is it possible to set options for a game?

I can see that Client.game only has the option 'numPlayers'.. But say I had multiple board layouts, I'd like to pass through which board to use.. ie: board: 1, boards: 2, etc.

render(){
    const HQClient = Client({
        game: HQGame, //map is NOT transferred to game class
        board: HQBoard //map data is transferred to board class
    })

    console.log(this.props)
        return (
            <div>
                GAME
                <HQClient map={this.state.mapData} /> //map prop set here
            </div>
        )
    }
nicolodavis commented 4 years ago

Any additional props that you pass to the client component are passed down to the board. So, you can have your board component be a wrapper that is able to switch between layouts like this:

const HQBoard = ({ layout }) => {
  if (layout == 'board 1') return <Board1/>;
  if (layout == 'board 2') return <Board2/>;
};

render() {
    const HQClient = Client({
        game: HQGame,
        board: HQBoard,
    })

    return (
            <div>
                GAME
                <HQClient layout="board 1" />
            </div>
    );
}
nicolodavis commented 4 years ago

If you want to pass options to the game config, then you should use setupData.

nicolodavis commented 4 years ago

Hope that answers your question! Feel free to continue discussion here or on Gitter.

DrummerSi commented 4 years ago

Thanks for the speedy response.. unfortunately it seems the setupData is only available via the game creation API, which is setup for multiplayer only.. I wanted it offline, and to build a basic AI to play against.. Which I understand multiplayer doesn't support.

nicolodavis commented 4 years ago

If I understand your game correctly, you want to pass an option to change something UI related (a board layout) and not something that affects moves etc.?

DrummerSi commented 4 years ago

Are you familiar with the boardgame Heroquest?

It takes place on the same playing board, but each quest adds furniture and doors to the board, changing the layout each time you play.. Because these items change the board layout, they would be required on the game logic to verify moves are legal, etc.

So what I hoped I could do is something like initialise the Client with a quest name.. The client can then load a related json file for the visual tiles, while the game engine will load the json file to track which tiles are valid movement spots, etc

This looks a great framework, but I realise this may be slightly more complex that what it was designed for.

nicolodavis commented 4 years ago

For these cases you should typically perform the selection (the choice of layout) outside of boardgame.io and then initialize the game and client once you do have the exact layout (and relevant game logic) that you want to use.