Skip to main content

Game Functions

The game fn specifier is used to declare the function that is the entrypoint when a user navigates to play the game. There can only be one game fn in a program, and conventionally it would be called Main.

pub page fn World.HomePage() {
ContentScreen {
// Calling Main returns an `intent` to enter the game, but the button must be clicked to invoke the intent
Button(Main, tier=Color:Primary, width=25) { "Play" }
}
}

// This `game` function is the entrypoint to the game.
// The `maxHumanPlayers=5` parameter means that, by default, the game is a multiplayer game with a maximum of 5 players.
pub game fn World.Main(maxHumanPlayers=5) {
// start the game
}

Declaring a game function

A game function is declared using the game keyword followed by the fn keyword and the function name. By convention, the function should be called Main.

A game function can take any number of parameters. All parameters must be serializable as they need to be sent across the network.

Be aware that parameters with certain names have special meanings:

  • maxHumanPlayers defines the maximum number of human players in the game. Setting this to a value higher than 1 makes the game a multiplayer game. Any other player looking for a game at the same time with the same parameters will be added to the same game. See Multiplayer to learn more.
  • allowSpectators defines whether spectators are allowed to watch this game. See Spectating to learn more.

Entering the game

Calling a game function does not actually enter the game, nor does it execute any statements in the body of the game function. Instead, it returns an intent to enter to the game with the given parameters. This intent can be attached to a Button. When the button is clicked, the intent will cause the user's browser to enter to the game.