Networked props ​
A networked prop is one OneSync object that every player shares. Use it when physics or position must be the same for everyone, for example a crate players push around. Everything else should be a streamed prop: it costs no network entity.
-- server
exports.np_prophandler:registerProp({
id = 'np_drugs:crate_7',
model = 'prop_box_wood02a',
coords = vector3(1391.2, 3605.8, 38.9),
networked = true,
frozen = false,
spawnDistance = 60.0,
})
Lifecycle ​
The server owns the entity. It runs a pass every server.networked.pollIntervalMs (1 s):
- Player positions are read into a grid (
server.networked.cellSize, 128 m). - A prop whose routing bucket has a player within
spawnDistancegets its entity:CreateObjectNoOffset(model, x, y, z, true, true, false), then the rotation,FreezeEntityPosition,SetEntityRoutingBucket(bucket 0 whenroutingBucketis unset) andSetEntityOrphanMode(entity, 2). The entity carries the state bagnp_prophandler:idwith the prop id. - An existing entity counts players up to
spawnDistance + server.networked.despawnMargin(15 m). Once nobody has been in range forserver.networked.despawnDelayMs(15 s), it is deleted. A disabled prop is deleted at once. - An entity deleted by something else (
entityRemoved) is created again afterserver.networked.recreateDelayMs(2 s) if a player is still near. - A changed definition (
updateProp) deletes the entity. The next pass creates the new one. Adynamicprop is followed instead: it is created again where it was left.
Net id changes go to every client on the sync channel, in one message per pass.
spawnProp(id) creates the entity right away instead of on the next pass (the maxActive cap
still applies) and returns entity, netId, for when a client needs the net id at once.
Limits: server.networked.maxRegistered (256) networked props registered, server.networked.maxActive (64)
entities at the same time, server.networked.maxCreatesPerTick (8) created per pass. At the maxActive cap,
creation order is registry order, not nearest first.
Props that move (dynamic) ​
A plain networked prop is created at, and streamed around, the coords it was registered with. With dynamic = true
(networked only) the server follows the entity instead: each pass reads its position and rotation, decides creation
and deletion around that point, and when the entity is deleted (nobody near, or by something else) it is created again
where it was left, with that rotation.
-- server: an ATM players rip out and drag away
exports.np_prophandler:registerProp({
id = 'np_banking:ripped_' .. id, model = 'prop_atm_01', coords = atm.coords,
networked = true, dynamic = true, frozen = false, spawnDistance = 150.0,
})
local entity, netId = exports.np_prophandler:spawnProp('np_banking:ripped_' .. id) -- now, not on the next pass
local coords, rotation = exports.np_prophandler:getPropCoords('np_banking:ripped_' .. id)
exports.np_prophandler:setPropCoords('np_banking:ripped_' .. id, vector3(...), vector3(0.0, 0.0, 90.0)) -- rotation optional
- Moving it is up to you: physics (
frozen = false), a client with network control (SetEntityCoords,AttachEntityToEntity, ropes), or the server withsetPropCoords, which moves the entity and the stored position, also while it is despawned. updatePropkeeps a dynamic prop where it is unless the registeredcoordsorrotationchange.- The entity handle changes whenever the entity is created again. Look it up each time with
getPropEntity/getPropNetId(server) orgetPropEntity/np_prophandler:spawned(client) instead of keeping one. getPropCoords(id)answers for every prop: the live entity of a networked prop, else where it will be created, else the registered coords.setPropCoordsworks on every networked prop of your resource. A non-dynamic one keeps the new place until its nextupdateProporsetPropEnabled. Streamed props move withupdateProp.- The position is read once per pass (1 s). An entity moved and deleted within one pass comes back where the previous pass saw it.
On the client ​
Clients get the entity through normal entity streaming. np_prophandler matches it to the prop by its net id and the
entity state bag np_prophandler:id (net ids are reused, so the state bag decides). When it streams in for a client:
alpha,lodDistance,textureVariationandinvisibleare applied locally, and collision is turned off whencollision = false.- The
interactoption is added. np_prophandler:spawnedfires with(id, entity, data). When it streams out,np_prophandler:despawnedfires.
getPropEntity returns the entity on the client while it is streamed in. On the server,
getPropEntity and getPropNetId return it while it exists.
Differences from streamed props ​
| Streamed | Networked | |
|---|---|---|
| Who sees it | Players for whom it is available | Decided by entity scope and the routing bucket. jobs / gangs / hours only gate the interact option |
routingBucket unset |
All buckets | Bucket 0 |
placeOnGround |
Allowed | Refused with 'networked_place_on_ground': the server has no ground probe |
renderDistance, despawnDelayMs |
Per client | Not used. The server's despawnMargin and despawnDelayMs apply |
interact with server = true |
Sends net id 0 |
Sends the entity's net id |
| Missing model | Detected on the client, never retried | The server cannot check it. A bad model creates an entity clients cannot draw |
| Position | Fixed | Fixed, or follows the entity with dynamic = true |
| Who registers it | The server, or a client with addLocalProp |
The server only. addLocalProp refuses it |
When np_prophandler stops
It deletes every networked entity it created. Clients forget the props without deleting anything, because the server owns those entities.