Game Functions
The game fn
specifier is used to declare the function that is the entrypoint
when a user navigates to play the game.
Conventionally, most games will only have one game fn
called Main
,
but you can have as many as you like.
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 Sendable as they need to be sent across the network.
Parameters with certain names have special meanings:
maxHumanPlayers
(Number): 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
(Boolean): Whether spectators are allowed to watch this game. See Spectating to learn more.allowRejoining
(Boolean): If a player is already in a game and requests to join a new game with the same parameters, then the player will exit and rejoin the existing game rather than starting a new game. This is a simple way to implement respawning. They only works if the game is a multiplayer game and is not locked yet.
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.