Skip to main content

Game Functions

The game fn defines an entrypoint for a particular game mode in your 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, backgroundColor=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
SpawnEachPlayer owner {
// ...
}
}

Game function parameters​

You can give your game function any number of parameters. All parameters must be Sendable as they need to be sent across the network. Here is an example of a game function with two parameters, difficulty and maxHumanPlayers:

pub game fn World.Main(difficulty=5, maxHumanPlayers=5) {
// start the game
}

Special parameters​

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.

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 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.

pub page fn World.HomePage() {
ContentScreen {
Button(Main(difficulty=1), backgroundColor=Color:Primary) { "Easy Mode" }
Button(Main(difficulty=10), backgroundColor=Color:Primary) { "Hard Mode" }
}
}

Matchmaking​

When a player clicks a button that enters a game, they will be matched with any other players who have entered the game with the exact same set of parameters as them.

Per-player configuration​

The parameters to a game fn are always the same for all players in a game. If they were different, the players would be matched to different games. Need to send unique data for each player? There are two main methods:

  • For persistent data: Preferences can be used to store arbitrary data that is unique to each player. They are often used to store player settings, like their chosen loadout, their character skin, or their progress in the game. Preferences can be modified outside a game, for example on a settings page, and then read from within a game to customize the experience for that player.
  • For transient data: PlayerConfig is designed for sending one-off transient data from outside the game to inside the game for each player. Since it is transient, it is best used for things that are only relevant for the current game session, like their vote for the next game mode or map to play on.

Using PlayerConfig​

Let's say we have a game where players can vote for the next map. Voting is a desire, not a requirement, so players should still be matched with others who voted differently from them. Once players join the game, the map with the highest number of votes will be selected.

In our case, our player wants to vote for the Desert map. We could do this as follows:

let config = { voteForMap = $desert }
RaisedButton(PlayIntent(Main, config=), backgroundColor=Color:Primary) { "Enter Next Game" }

Notice that we decorate the call to the game function Main with a PlayIntent, specifying the unique configuration data for that player in the config parameter.

Now, once the player is ready, they click the "Enter Next Game" button to actually enter the game. Because voteForMap is specified via config and not via a parameter to Main, it does not affect the matchmaking. The player may be matched with other players who did not vote for the Desert map, widening the pool of players they can be matched with, and letting them get into the game sooner.

Once in-game, use PlayerConfig to access the configuration for each player:

let NumVotesPerMap = {}

pub game fn World.Main() {
SpawnEachPlayer owner {
NumVotesPerMap[owner.PlayerConfig.voteForMap] += 1

Content {
RaisedButton(PressIntent<startGame>, backgroundColor=Color:Primary) { "Start Game" }
}
on Pressed<startGame> { CommenceGame }
}

once GameCommenced {
let _, mapId = NumVotesPerMap.MaxBy(|count, mapId| count)
// start the game with the winning map
}
}