Multiplayer
Renzora has multiplayer built in. One copy of your game runs as the server, which decides what really happens, and every player's copy is a client that shows it. You mark which things in your scene are shared, and the engine keeps them in step on every screen.
This page shows how to turn a scene into a multiplayer game, test it from the editor, and talk between players from a Rust script. New to scripts? Read Rust scripts first.
Ways to run a session
| Role | How it starts | Has a player? |
|---|---|---|
| Host (listen server) | the game launched with --host | Yes: the host plays too, as player 1 |
| Dedicated server | the game launched with --server | No: it runs headless, with no window |
| Client | launched with --connect address:port, or a script calls connect | Yes |
If both --host and --server are given, --host wins.
Multiplayer runs over UDP. A web build has no multiplayer, because a browser cannot open a UDP connection: there, everything behaves as if the game were offline.
Testing from the editor
You don't need to start several copies by hand. Add a [network] section to your project's project.toml and say what Play should start:
[network]
play_mode = "host" # "single" (default), "host" or "server"
play_clients = 2 # extra client windows that join automatically
play_mode | What Play starts |
|---|---|
single | The game on its own, as usual. |
host | The game as the host, plus play_clients extra windows that join it. |
server | A headless dedicated server, then the game and play_clients extra windows as its clients. |
Every window joins over your own machine, so this works with no network at all. Stopping Play closes them all.
The other [network] settings, all optional:
| Setting | Default | Meaning |
|---|---|---|
server_addr | 127.0.0.1 | The address clients connect to. |
port | 7636 | The port the server listens on and clients connect to. |
tick_rate | 64 | How many times a second a dedicated server updates. |
max_clients | 32 | How many players can join. |
Making things shared
Select an entity and use Add in the Inspector. The networking components are:
| Component | What it does |
|---|---|
| Networked | Share this entity. The server sends it to every player and keeps its position up to date. Nothing is shared without this. |
| Network Transform | Optional: how the position is shared. Moved By (Server or Owner), Interpolate (smooth the movement between updates), Sync Rotation, Sync Scale. |
| Player Spawner | Gives every player who joins their own avatar. Set Player Prefab to the avatar scene and Spawn Radius to how far apart they appear. |
| Network Owner | Read-only. Shows who owns the entity: Server, or Player N. |
A door, a coin or an enemy placed in your scene with Networked becomes one shared door, coin or enemy on every screen. Things without it stay local to each player, which is right for scenery and effects.
Who moves what
Moved By on Network Transform decides whose copy is in charge of the position:
- Server (the default): the server moves it and everyone else follows. Use it for anything the rules own: enemies, doors, moving platforms. A cheating player cannot move these.
- Owner: the player who owns it moves it, and the server passes that on. This is what makes your own avatar respond instantly instead of waiting for the server. Avatars from a Player Spawner are set up this way for you.
Player avatars
- Make your player character and save it as its own scene, for example
prefabs/player.bsn, with the script that moves it attached. You don't need to add any networking components to it: the spawner does that for each avatar it creates. - In your level, add an empty entity and give it a Player Spawner. Pick
prefabs/player.bsnas the Player Prefab. - Press Play with
play_mode = "host".
The server spawns one avatar per player around the spawner (the host gets one too), each owned by its player. When a player leaves, their avatar goes with them.
Scripting multiplayer
Everything is in renzora::network.
Run game logic once, not on every screen
Every player's copy of the game runs every script. For a script that moves a character or decides who scored, only one copy should act. has_authority answers "is this copy in charge of this entity?":
use bevy::prelude::*;
use renzora::ScriptCtx;
use renzora::network::has_authority;
fn update(ctx: &mut ScriptCtx) {
let me = ctx.entity();
if !has_authority(ctx.world(), me) {
return; // Another copy moves this one; we just show it.
}
let dt = ctx.delta();
let keys = ctx.get_resource::<ButtonInput<KeyCode>>().cloned();
let Some(keys) = keys else { return };
let mut dir = Vec3::ZERO;
if keys.pressed(KeyCode::KeyW) { dir.z -= 1.0; }
if keys.pressed(KeyCode::KeyS) { dir.z += 1.0; }
if keys.pressed(KeyCode::KeyA) { dir.x -= 1.0; }
if keys.pressed(KeyCode::KeyD) { dir.x += 1.0; }
if let Some(mut t) = ctx.get_mut::<Transform>() {
t.translation += dir.normalize_or_zero() * 5.0 * dt;
}
}
renzora::script!(update);
On an avatar, the owning player has authority, so each player moves only their own character. On something the server moves, only the server does. With nobody connected, everything answers yes, so the same scene still plays single-player.
Other questions you can ask (each takes ctx.world()):
| Function | Answers |
|---|---|
is_server(world) | Is this copy the server (host or dedicated)? |
is_client(world) | Is this copy a client of a server elsewhere? |
is_connected(world) | Is there a live session? |
is_mine(world, entity) | Does this entity belong to this player? |
local_peer_id(world) | This player's id. The host is 1, a dedicated server is 0. |
players(world) | The id of every player in the session. |
Sending messages between players (RPCs)
An RPC is a named message with a few values attached, sent to other players. The usual pattern is: a client asks the server to do something, the server checks it and tells everyone.
use renzora::network::{Rpc, RpcTarget};
// On a client: "I want to open this door."
Rpc::new("open_door")
.arg("door", "gate_01")
.to(RpcTarget::Server)
.send(ctx.world());
| Target | Goes to |
|---|---|
RpcTarget::Server | The server only. |
RpcTarget::All | Everyone, including the sender. |
RpcTarget::Others | Everyone except the sender. The default. |
RpcTarget::Peer(id) | One player. |
Arguments can be numbers, whole numbers, true/false, text or a Vec3. RPCs always arrive, in order. For something you resend every frame anyway, add .unreliable() to send it the cheap way.
To receive RPCs, add a hooks function to your script. Every script with one hears every RPC, so check the name:
use bevy::prelude::*;
use renzora::{ScriptCtx, ScriptHook};
use renzora::network::{is_server, Rpc, RpcArgs, RpcTarget};
fn update(_ctx: &mut ScriptCtx) {}
fn hooks(ctx: &mut ScriptCtx, hook: &ScriptHook) {
match hook {
ScriptHook::Rpc { name, args, from } => {
if *name == "open_door" && is_server(ctx.world()) {
let door = args.str("door").unwrap_or_default().to_string();
info!("Player {from} opened {door}");
// Tell everyone, so every screen plays the animation.
Rpc::new("door_opened")
.arg("door", door)
.to(RpcTarget::All)
.send(ctx.world());
}
}
ScriptHook::PlayerJoined { id } => info!("Player {id} joined"),
ScriptHook::PlayerLeft { id } => info!("Player {id} left"),
_ => {}
}
}
renzora::script!(update, hooks = hooks);
args reads values by name: args.f32("speed"), args.i64("count"), args.bool("on"), args.str("door"), args.vec3("at").
PlayerJoined and PlayerLeft are only heard on the server.
With nobody connected, an RPC sent to Server or All is delivered straight back to your own scripts, so a game written for multiplayer still works alone.
Spawning shared things
Only the server can create something everyone sees. spawn_prefab makes a networked copy of a scene file, and every player gets it:
use renzora::network::{is_server, spawn_prefab, OwnerKind};
if is_server(ctx.world()) {
let at = Transform::from_xyz(0.0, 1.0, 0.0);
spawn_prefab(ctx.world(), "prefabs/coin.bsn", at, OwnerKind::Server);
}
On a client it does nothing and logs a warning. A client that wants something spawned sends the server an RPC asking for it.
Joining a server from a menu
A client can join from a script, for example from a "Join" button in your Game UI:
// Leave the address empty, or the port 0, to use the project's [network] values.
renzora::network::connect(ctx.world(), "192.168.1.20", 7636);
// And to leave:
renzora::network::disconnect(ctx.world());
Shipping a multiplayer game
The exported game takes the same launch options: a player runs it normally (or with --connect address:port), and you run a copy with --server on a machine players can reach. A server also accepts --port, --address, --tick-rate and --max-clients, which override project.toml. It listens on every network interface of its machine.
The export turns multiplayer on by itself when your scenes use the networking components or your scripts use renzora::network. You can check it under Multiplayer (networking) in the export dialog. See Exporting.
Watching a session
Three editor panels help while testing. Open them from the + on any panel's tab bar, under Network:
| Panel | Shows |
|---|---|
| Network | Connection state, round-trip time, jitter, packet loss and the connected players. |
| Net Entities | Every networked entity and who owns it. |
| Net Settings | The [network] settings from project.toml. |
Good to know
- Every player is sent every shared entity, wherever it is in the world.
- Sharing your own components, custom transports and how the protocol works are covered in Networking (engine).