Multiplayer
Easel makes multiplayer games effortless. Developers can code as if everyone is in one shared world, making multiplayer games as easy to code as singleplayer games. Easel takes care of all state synchronization and even performs client-side prediction, completely automatically!
Maximum players
To enable multiple players to join a game, the maxHumanPlayers
parameter should be set in your game fn
.
If maxHumanPlayers
is greater than 1, then the game will be a multiplayer game.
That means, as long as the game has not been locked (by calling LockGame
),
other players who are searching for a game with the exact same set of parameters
may be added to the same game.
pub game fn World.Main(maxHumanPlayers=4) {
// this is now a multiplayer game with a maximum of 4 players
}
See Entrypoints for more information on declaring a game
function.
Note that the server has an internal limit on how many maximum human players can be in a game at once,
and so it may not achieve the requested maxHumanPlayers
if it is too high.
Currently, the server limit is 16, but this may increase over time.
Players may also be limited to only joining games within their region. See Regions for more information.
Locking the game
Some games have the concept of a lobby, where players can join and leave before the game starts. In Easel, there is no separate lobby. Instead, the game begins in an "unlocked" mode where players can freely join and leave. Then, at some point, perhaps based pressing a "start" button once a minimum number of players have joined, the game becomes "locked", no new players can join, and the game can begin.
Call the LockGame function to lock the game. Once the game is locked, players cannot join or leave. This should be used when the game is about to start. For example, if your game is a racing game, call LockGame just before the race starts.
While LockGame
stops new players from joining,
the CommenceGame function marks the beginning of the game.
Because in most games both of these occur at the same time,
calling CommenceGame
will first lock the game if it hasn't been already
(equivalent to calling LockGame
) before starting the game.
Once a game has been concluded using ConcludeGame, the game is also considered locked. In other words, players cannot join a game that has already ended.
Leaving players
When a player leaves, whether their player entity gets despawned or not depends on whether the game is locked.
If the game is not locked, the player entity will be despawned when they leave.
If the game is locked, the player entity will never be despawned, even if they leave.
Instead, their IsPresent property will change to false
.
This is useful because sometimes the player entity owns other entities which are essential
for the remaining players to be able to complete the game.
It may be a good idea to watch the IsPresent
property using a with
block
and make a bot take over control of the player entity when the player leaves
so that the game can be completed.
The only change due to locking is whether the player entity is despawned or not. Signals like await BeforePlayerLeave still do fire just the same after the game is locked.
Rejoining the same game
If a game is not locked, then if a player clicks a button to join a new game, they will just end up leaving and rejoining the same game as a new player. This can be used as a simple way way to code respawning in a game.
RaisedButton(Main, backgroundColor=Color:Primary) { "Play Again" }
To stop players from rejoining an existing game, you should make sure to call LockGame
or ConcludeGame
to lock the game.
Instantaneous effect
LockGame applies instantly, even though it may take a fraction of a second for the server to get the message and stop adding players. Any players who are added within this fraction of a second will see the locked game but will not join it. As soon as their client detects the situation they will be reassigned to a new game.
This means your game can rely on no more players being joiniing the instant LockGame
is called.
This means it is safe to call LockGame
and immediately begin the game.
Catching up
When a player joins a game, their client will need to replay all inputs that have been made so far. An "Entering the game..." message will be shown to the player while they are catching up. This can take a while if the game has been running for a long time.
One way to improve this is to enable a feature called snapshotting in network.toml
.
This will cause the game to capture a snapshot of the game state at regular intervals.
This snapshot can be sent to new players when they join, allowing them to catch up much faster.
This feature increases the CPU and bandwidth usage of the client, so it is disabled by default.
To learn how to enable it, see Snapshotting.
Otherwise, during the phase before LockGame when players are still joining, it is best to avoid complicated calculations that may slow down joining the game for new players. For example, wait until your game begins before adding bot enemies to the game.
Eliminating players
The Eliminate function can be used to signal that a player has been eliminated from the current game. In a battle royale, this could mean when a player dies and will not come back. Eliminated players will be changed from colored to grey on the Scoreboard to help players keep track of who is still in the game. The engine is also constantly optimizing the network latency for only the players who are still in the game, and so eliminating players will improve the experience for the remaining players.