Multiplayer
Multiplayer
Section titled “Multiplayer”Players at one computer (two at a keyboard, on controllers) don’t need a network: see Several players at one machine.
Network play is for games built for Windows: a browser can’t make the connections it needs, so a game built for the web says network play isn’t there and plays alone (see In a browser).
Any scene becomes a network game with a Multiplayer component, and the Multiplayer template is one to start from: a menu to type your name and a host’s address into, and an arena for up to eight players with slimes, hearts, crates to hide behind and a scoreboard. One machine hosts and runs the game; everyone else’s shows the host’s world, sends the host their keys, and moves their own character itself, so it never waits on the connection. Cameras, the HUD, menus and music stay on each machine and follow that player’s own character, and so do each player’s settings, their audio and visual offsets among them (see Rhythm games). The pieces:
- Multiplayer, on any entity in the scene: Max Players, the Port to host on, the Player each one plays (an entity in the scene, which the host plays and everyone else gets a copy of, or a prefab; empty for a strategy game, where players command teams), Teams for team games (“Blue, Red”: players are shared out between them, appear at their team’s spawns and can’t hurt each other unless Friendly Fire is on; in a strategy game they’re the factions players take, and a computer Opponent stands aside for a person playing its team), Color Players (each player’s character in their own color, or their team’s, and their shots too), Score Kills (a point for defeating another player or an enemy), Send Rate, Smoothing (how far behind the host everyone else’s characters are shown, so they glide), View Distance (for a big world, like an MMO: only what’s within it is sent to each player), Remember Players (a player who leaves gets their character back as it was when they join again, even in a later game) and Exit Scene (where to go when the host leaves).
- Player Spawn marks where players appear (for a Team, or anyone). The Multiplayer’s Teams are the teams every genre shares (see Teams): a player’s character is on its player’s team, so it takes that team’s turns, plays that strategy faction and pays with that team’s items. Name Tag shows the name of the player something belongs to in its Text: put it on a child above the player. Scoreboard lists who’s playing, with their score and ping, and how the game’s going (“Hosting on 192.168.1.5:7777”, “Joining…”, why it ended), optionally only while a key like Tab is held. Text Field is a box to type into: a player’s name, or the address to join.
- Buttons have Host Game (then go to their Scene), Join Game (the address typed into a Text Field, or with none, the first game found on the local network) and Leave Game. A pause menu doesn’t stop a network game: the game goes on, and that player’s keys just don’t reach it until it’s closed.
- What’s sent is every saved property of every component, a script’s as much as Griff’s, whenever it changes. A component says otherwise for itself:
[NetLocal]on its class keeps its entity on each machine (as cameras, HUDs, menus and music are),[PlayerMovement]marks no-code movement the player’s own machine runs,[Sent]sends a value kept only while playing ([Sent] public float Charge { get; private set; };[Sent(ByMover = true)]lets the player moving the entity decide it),[NotSent]keeps a saved one to each machine, andINetStateadds values that need turning into something sendable (a match-3 board sends its gems as lines) or skips a property just now.INetSecretkeeps an entity from some players altogether, so their machines never get it: the cards of a face-down deck (only how many there are is sent) and of another side’s hidden hand. - Networked changes how one entity takes part: Sync shares it with everyone (the default for everything but cameras, HUDs, menus and music) or keeps it Local to each machine; Movement says whether a player’s own machine moves their character (the default for characters with a movement component: instant to control) or the host does from their keys (nobody can cheat); Always Sent ignores the view distance; Owner is the player it belongs to, set by the game.
- What works for every player with no code: platformer, top-down, grid and first-person movement (each player turns and walks on their own screen), weapons, turrets and attacks driven by their own keys and aim, health, damage and respawning (the host’s spikes and checkpoints count for every player), pickups, doors, levers, buttons and dialogue (read on each player’s own screen), enemies that chase or shoot at the nearest player, a strategy game’s commander (each player selects and orders their own team; the orders go to the host), a block world’s digging, building, hotbar, crafting and sandbox tools (everyone in the same world, with their own tools and undo), slingshots (a player pulls one on their own screen and the host launches the shot; things break on the host and their pieces come to everyone), pinball tables (a player’s buttons, stick and mouse reach the host, which flips, shoots and nudges for them; a table a player owns answers to them alone), and card games (each player plays their own side’s hand with their own mouse or keys, the host shuffles and deals, and nobody’s machine is sent the deck’s order or another side’s hidden hand; see Card games). Messages sent on the host reach everyone, so their HUD counters and sound players work too, and the host’s sounds play for everyone.
- Going between scenes: the host goes and everyone follows. A door’s Arrive At places the players’ characters where they arrive, a scene that remembers how it was left (Remember Scene) is remembered by the host and comes back for everyone, and values carried by a Save As are kept in the host’s save data. A Minigame can’t be played during a network game: it takes one player’s screen and input from a world that’s everyone’s, so it says so instead (see Minigames).
- Game > Play with 2 Players (Ctrl+F5, or 3 or 4) plays as several players at once: the Game tab hosts and each other player gets a window of their own, joined over a pretend network inside the editor. Click a window to play as that player. Game > Pretend a Slow Connection adds lag and a few lost packets to see how the game feels on a bad connection.
- Exported games host and join from their own menus. On the command line,
--host [port]hosts straight away,--join <address>joins,--name <name>sets the player’s name, and--server [port]runs a dedicated server: the start scene, hosted for everyone and played by nobody, as a console program with no window (it never loads the desktop’s UI, WPF).--mute(orGRIFF_MUTE=1) plays with no sound, never opening the sound device.