SubClient

Sub-games and the Role of SubClient

In some games, it may be necessary to create nested game structures, where a main game spawns sub-games with their own independent state and logic. Race Protocol supports this concept through sub-games. A sub-game is essentially a child game that is launched and managed by the main game. It has its own game handler, state, and event stream, but it is still connected to the main game and can communicate with it through events.

The SubClient class in the RACE JS SDK provides the interface for interacting with sub-games on the client side. It is similar to the AppClient in terms of functionalities, but it specifically focuses on managing the interaction between the client and a particular sub-game.

Creating and Using SubClient

To create a SubClient instance, you first need an initialized AppClient instance. You can then use the subClient function of the AppClient, providing the following information:

  • subId: The unique identifier of the sub-game you want to connect to.

  • onEvent: A callback function that will be invoked whenever an event occurs within the sub-game.

Here's an example of creating and using a SubClient:

// Assuming appClient is an initialized AppClient instance

const subId = 1; // Replace with your sub-game ID

const subClient = await appClient.subClient({
  subId,
  onEvent: (context, state, event) => {
    // Handle sub-game events here
  },
});

// Now you can use subClient to interact with the sub-game, 
// submit events, and access its state information.

Once you have a SubClient instance, you can use its functions to:

  • Connect to the sub-game: The attachGame function connects the client to the sub-game's event stream.

  • Submit events: The submitEvent function allows players to submit custom events to the sub-game.

  • Handle events: The SubClient provides event listeners that can be used to react to incoming events and update the sub-game UI.

  • Access game context and state: The SubClient provides access to the sub-game's GameContext and handler state.

By utilizing the SubClient, developers can build client-side applications that interact with both the main game and its sub-games, enabling complex and engaging gameplay experiences within the Race Protocol framework.

Last updated