Networkable
Updated
A Networkable makes its entity a shared object: everyone in the same room sees it, and what one person does to it reaches the others. One peer owns the entity and simulates it; every other peer shows a copy that follows. Networkable Trait components choose what travels: the transform, the velocities, or the props of one of the entity’s components. Use them for anything people should see the same way: a door that opens, a ball that rolls, a lamp someone switches on, a shared scoreboard.
Before you start
Section titled “Before you start”- You know what a room, an owner and a proxy are. See the Multiplayer overview.
- You know how components send events to scripts through a Signal Emitter, and how
on(signal, callback)receives them. See Scripting and Events and signals. - Sharing happens in play only. In the editor nothing replicates.
The model
Section titled “The model”- One owner per object. The owner’s peer simulates the entity (its physics, its movement, the scripts that move it) and sends its traits. Every other peer holds a proxy.
- Proxies follow. A proxy is shown a short moment behind its owner and blended smoothly between updates, so it moves evenly even when updates arrive unevenly. A proxy’s rigid body turns kinematic and is driven to the incoming pose, so it still pushes local bodies. A NavMesh Agent is placed there, a Character Controller is led there, and player input and steering wait.
- Arena decides ownership, never state. Ownership claims are served in the order they arrive, and updates from a former owner are dropped.
- Taking an object. Selecting a proxy, grabbing it, or dragging its Pivotable gizmo or Handle asks for its ownership, so what you do to it reaches everyone. Scripts call
requestOwnership(). - When the owner leaves, its objects go to the oldest peer left that sees their scene, or are removed with it when
destroyWithOwneris on. With nobody in that scene, they wait, unowned, for the next peer that loads it. - Identity. An entity placed in the scene has the same id on every peer. The first peer to arrive sets its state; later ones take the live state. An entity spawned at runtime is sent whole, its children included, and built on every other peer.
- Nesting. A Networkable inside another Networkable’s subtree is its own object: its pose is relative to that parent, it is spawned with it, and it follows its ownership. Taking a cart takes its wheels. Moving it to another parent at runtime is replicated with its transform.
- Without a room, the entity is simply local and its owner is this peer.
Setting it up
Section titled “Setting it up”- Add a Networkable to the entity.
- Add one Networkable Trait per thing to replicate:
transformfor its pose,rigidbodyfor its velocities, a component’s trait name (such aspoint-light) for that component’s props. - Add a Signal Emitter on one Networkable if scripts should hear about ownership, the connection and other peers.
With no trait switched on, the entity is still in the room (its ownership, its events and any custom traits work) but sends no state.
Networkable
Section titled “Networkable”NetworkableComponent, type comp-networkable. One per entity.
| Prop | Type | Default | Description |
|---|---|---|---|
ownership |
'transfer' or 'locked' |
'transfer' |
transfer: anyone may take it. locked: only its first owner simulates it, as for a player avatar. |
persistent |
boolean | true |
Kept in the room after everyone leaves. |
destroy |
boolean | false |
Removed when its owner leaves, instead of handed to another peer. For avatars and projectiles. |
ttl |
number | 0 |
Removed this many seconds after it appears, up to 30 days. 0 keeps it. |
idle |
number | 86400 (a day) |
Removed after this many seconds in which nobody updates or uses it, up to 30 days. 0 means the default. |
These props are sent to the room when the object registers. Set them in the scene, or pass them to Network.replicate, before it does; changing them later does not change an object already in the room.
Members
Section titled “Members”| Member | Description |
|---|---|
isOwner |
Whether this peer simulates it: it owns it, or it is not shared yet. |
isProxy |
Whether it is a copy driven by another peer. |
ownerId |
The owning peer’s id; '' while it is not shared. |
request |
Ask to simulate it, together with what it is nested in. Resolves true when this peer owns it. |
release |
Hand it to another peer. |
destroy() |
Destroy the entity for everyone, as its owner. |
add |
Replicate one more trait: adds a Networkable Trait, or updates and switches on the one naming it. |
add |
Add a custom trait that carries a value of your own. It lives while the entity plays and is never saved. |
remove |
Stop replicating a trait: its Networkable Traits are removed. |
set |
Replicate exactly these traits, adding, changing or removing Networkable Traits to match. An entry is a trait name, or { type, frequency, include. Custom traits are kept. |
Networkable |
The traits an entity can replicate: transform, rigidbody when it has a rigid body, then its components’ trait names. |
Networkable |
Whether name is a component trait name. |
A custom trait’s get runs on the owner at the trait’s rate, and its value is sent only when it changed; set runs on every other peer. A value is a number, a string, a boolean or a small array of them. To send an object, send it as a string: get: () => JSON.stringify(state) and set: (text) => { state = JSON.parse(text); }.
Events
Section titled “Events”The Networkable sends its own ownership event, plus the events of the room connection, which every Networkable shares. One Signal Emitter on one Networkable is enough to hear the connection.
| Event | Data | When |
|---|---|---|
Networkable::ownership- |
{ is |
This peer started or stopped simulating the entity. |
Networkable::connect |
{ room, peer |
This peer joined room as peerId. |
Networkable::disconnect |
{ room, reason } |
This peer left ('left'), lost the connection and is reconnecting on its own ('lost'), or was removed by the room ('kicked', 'banned', 'room-closed'), in which case it stays out until Network.. |
Networkable::error |
{ room, message, code?, retrying } |
An attempt to join failed. retrying is false when the room refused it. code says why, for example 408 (timed out), 4030 (the room is full), 4031 (not allowed to join) or 4032 (the room is locked). |
Networkable::peer- |
{ peerId } |
Another peer arrived. |
Networkable::peer- |
{ peer |
Another peer left; reason is 'kicked', 'banned' or 'room-closed' when the room removed it. |
Compare with EventType.Networkable.OwnershipChange, Connect, Disconnect, Error, PeerJoin and PeerLeave.
Networkable Trait
Section titled “Networkable Trait”NetworkableTraitComponent, type comp-networkable-trait. Several per entity, one per trait. The entity’s Networkable sends every enabled trait on it. Switching one off (setEnabled(false)) stops sending it; adding, removing or changing one takes effect at once. When two name the same trait, the last one’s settings win.
| Prop | Type | Default | Description |
|---|---|---|---|
trait |
string | 'transform' |
What to replicate: transform, rigidbody, or a component’s trait name. |
frequency |
'auto', 'high', 'medium' or 'low' |
'auto' |
How often it is checked. |
includeScale |
boolean | true |
For transform: false shares position and rotation while each peer keeps its own scale. |
Traits
Section titled “Traits”| Trait | What travels |
|---|---|
transform |
Position, rotation and scale relative to the parent, and which parent it is under. |
rigidbody |
Linear and angular velocity, so whoever takes the object over carries on at its speed. |
| A component trait | The component’s props, and whether it is on, sent reliably whenever the owner changes them: setProps, setEnabled, or a UI control’s own change such as a click or a slider drag. |
The component traits, by name. Scripts name them with NetworkTraitType, the component’s class name without Component: NetworkTraitType.PointLight is 'point-light'.
| Area | Trait names |
|---|---|
| Entity state | enable, outline, silhouette |
| Materials | basic-, standard-, physical-, lambert-, phong-, normal-, glass-, matte-, reflector-, shader- |
| Lights | point-light, spot-light, directional-, hemisphere-, rect-, ambient-light, light-shadow |
| Models and meshes | gltf, fbx, obj, collada, usdz, kit-piece, geometry, csg, terrain, decal, fire |
| Media and text | audio, audio-, video, ui-video, media-stream, image, pdf, text, text-3d |
| Animation | animation- |
| UI | ui-slider, ui-checkbox, ui-switch, ui-, ui-progress, ui-label, ui- |
Some things are never traits: AI brains, cameras, controllers, wiring (Signal Emitter), structure (colliders, joints) and Behaviors. A Networkable Trait naming something that is not a trait plays without it, with a warning.
Frequencies
Section titled “Frequencies”| Frequency | Rate |
|---|---|
high |
20 times a second. |
medium |
10 times a second. |
low |
2 times a second. |
auto |
The trait’s own: high for transform, medium for rigidbody. Component and custom traits are checked at the high rate and sent only when they change. |
A trait is sent only when it changed past a small threshold. When it stops changing, its last value is sent once more, reliably, so a lost update never leaves a proxy slightly off.
Switching an entity on and off
Section titled “Switching an entity on and off”With enable among its traits, switching the entity off and on (setEnabled, or an Enable on Signal without targets) is replicated too. The entity stays in the room while it is off, owned as before.
Scenes and partitions
Section titled “Scenes and partitions”Each shared object belongs to a partition: the id of the scene it is in. A peer sees the partitions of the scenes it has loaded, and Arena only sends it their objects.
- Switching scenes. While the next scene loads, the peer sees both; once it is in, only the new one. The old scene’s objects are removed on this peer and handed to a peer still in that scene.
- Additive loads. With an additive load the peer sees both scenes, and each keeps its own objects.
- Unloading a scene stops the peer seeing it. Loading it again brings its objects back with their latest state; destroyed objects stay removed.
- Don’t Unload. Only a Networkable that is
lockedand owned by this peer travels to the next scene, such as the player’s avatar or what it holds. Any other shared Don’t Unload entity stays in its scene, with a warning.
Scripts can also give a plain object a partition of its own and subscribe to more partitions. See Network.
Spatial interest
Section titled “Spatial interest”When the project turns spatial interest on, a peer receives only the shared objects near it, within the partitions it sees.
- What it owns a peer always receives.
- Out of range, a scene object stays where it last was and stops receiving updates; it catches up when it is in range again. An object that is not part of the scene, such as another person’s avatar or a spawned ball, is removed and comes back whole.
- The area of interest is centred on the active camera, or on the object a script sets as
Network.interestCenter.
Examples
Section titled “Examples”1. A lamp anyone can switch
Section titled “1. A lamp anyone can switch”#pragma lifecycle(startup)
let lamp;let light;let net;
function startup() { lamp = findEntity('Lamp'); light = lamp.getComponent(PointLightComponent); net = lamp.getComponent(NetworkableComponent);
on('lamp/switch', async (event) => { if (event.eventType !== EventType.UIButton.Press) return; // Only the owner's changes travel: take the lamp first. if (!(await net.requestOwnership())) { log('Someone else holds the lamp'); return; } light.setProps({ intensity: light.props.intensity > 0 ? 0 : 2 }); });}A point light shared with a Networkable and a point-light trait: the switch takes ownership with requestOwnership, then turns the light on or off for everyone.
Press Lamp Switch to turn the lamp off and on. In a published world the change reaches every peer.
- The
point-lighttrait sends the light’s props whenever its owner changes them. requestOwnership()resolvestrueonce this peer owns the lamp. Without it, the change would happen on a proxy and stay local.- The handler is
asyncso it canawaitthe ownership request.
Snippets
Section titled “Snippets”1. Adding traits from a script
Section titled “1. Adding traits from a script”#pragma lifecycle(startup)
// Scene: a "Ball" with a dynamic rigid body, a Grabbable and a Networkable.function startup() { const ball = findEntity('Ball'); const net = ball.getComponent(NetworkableComponent);
log('Can replicate:', NetworkableComponent.traitsFor(ball).join(', '));
net.setTraits([ NetworkTraitType.Transform, { type: NetworkTraitType.RigidBody, frequency: 'medium' }, { type: NetworkTraitType.StandardMaterial, frequency: 'low' }, ]);}traitsForlists what this entity can share, which helps when you are not sure of a trait name.setTraitsadds Networkable Trait components to match the list; they are saved with the scene like ones you add in the editor.rigidbodykeeps a thrown ball moving at the right speed when another peer takes it over.
2. A shared scoreboard with a custom trait
Section titled “2. A shared scoreboard with a custom trait”#pragma lifecycle(startup)
// Scene: this Behavior's entity carries a Networkable and a Signal Emitter on// 'board/net'. A "Score" UI Label shows the value. Any script emits 'game/point'.let score = 0;let net;let label;
function show() { label.setProps({ text: `Score: ${score}` });}
function startup() { net = self.parent.getComponent(NetworkableComponent); label = findEntity('Score').getComponent(UILabelComponent);
// The owner reads `get`; every other peer receives the value in `set`. net.addTrait('score', { get: () => score, set: (value) => { score = value; show(); }, frequency: 'low', });
on('game/point', () => { if (!net.isOwner) return; // proxies never write score += 1; show(); });
on('board/net', (event) => { if (event.eventType === EventType.Networkable.OwnershipChange) { log(event.data.isOwner ? 'I keep the score' : `Score kept by ${event.data.ownerId}`); } });
show();}- A custom trait shares a plain value that no component holds. It is never saved with the scene.
- Only the owner increments the score; the others display what arrives in
set. - When the owner leaves, ownership passes to another peer, which carries on from the last value it received.
3. Hearing the room
Section titled “3. Hearing the room”#pragma lifecycle(startup)
// Scene: any Networkable entity has a Signal Emitter on 'net'.function startup() { log(Network.connected ? `Joined ${Network.room}` : 'Not in a room yet');
on('net', (event) => { switch (event.eventType) { case EventType.Networkable.Connect: log(`Connected to ${event.data.room} as ${event.data.peerId}`); break; case EventType.Networkable.Disconnect: log(`Disconnected: ${event.data.reason}`); break; case EventType.Networkable.Error: log(`Could not join: ${event.data.message}`, event.data.retrying ? '(retrying)' : ''); break; case EventType.Networkable.PeerJoin: log(`${event.data.peerId} arrived, ${Network.peerCount} others here`); break; case EventType.Networkable.PeerLeave: log(`${event.data.peerId} left`); break; } });}- Every Networkable shares one connection, so one Signal Emitter on one of them hears it all.
Network.connectedcovers the join that happened before the script started.
Common mistakes
Section titled “Common mistakes”- Changing a proxy. A
setPropsorsetPositionon an object this peer does not own stays local and is overwritten by the owner’s next update. CheckisOwner, orawait requestOwnership()first. - Forgetting the trait. A Networkable alone shares ownership, not state. Add
transformto share movement, and the component’s trait to share its props. - Using
lockedfor shared props. Alockedobject can only be simulated by its first owner. Use it for avatars, not for a door everyone opens. - No Signal Emitter. Without one, the Networkable’s events go nowhere, and
on(...)never fires. - Expecting the first
connectevent. It usually fires beforestartup. ReadNetwork.connectedinstead. - Sending objects in a custom trait. Values must be numbers, strings, booleans or small arrays of them. Use
JSON.stringifyandJSON.parsefor anything else.