Skip to content

Sending Updates

Once players have received the full state of the world, you will need to constantly send changes to your players.

Changes should be sent inside a system that will run every frame. Its recommended that you throttle the updates to a lower interval, a good rate is somewhere around 10-30 times a second.

In the server, you will call server_replicator:collect_updates(). This will return an iterator that will be executed for every player.

The iterator will give you the following values:

  • player: Player - the player to send the updates to
  • buf: buffer - the buffer containing the updates
  • variants: {any} - the variants table containing non-serialized values

Updates can then be applied in the client using client_replicator:apply_updates(buf, variants).

replecs system
local replicator = require("@server/replicator")
local interval = require("@utils/interval")
local system = require("@utils/scheduler/system")
-- interval throttles systems to a specific interval
local updates_interval = interval(1 / 20)
function replecs_server()
-- will send updates 20 times a second
if updates_interval() then
for player, buf, variants in replicator:collect_updates() do
remotes_server.send_updates:fire(player, buf, variants)
end
end
end
system(replecs_server)

Unreliable updates are sent similarly to normal updates. You can call server_replicator:collect_unreliable(). This will return an iterator that will be executed for every player.

The iterator will give you the same values as normal updates.

Updates can then be applied in the client using client_replicator:apply_unreliable(buf, variants).

Because unreliable values are always sent, deciding how often you send them is crucial, making it slower to not overload the network or faster for smoother replication.

Combining both update types your client/server systems should look something like this:

replecs system
local replicator = require("@server/replicator")
local interval = require("@utils/interval")
local system = require("@utils/scheduler/system")
local updates_interval = interval(1 / 20)
local unreliables_interval = interval(1 / 30)
function replecs_server()
-- will send updates 20 times a second
if updates_interval() then
for player, buf, variants in replicator:collect_updates() do
remotes_server.send_updates:fire(player, buf, variants)
end
end
-- will send unreliables 30 times a second
if unreliables_interval() then
for player, buf, variants in replicator:collect_unreliable() do
remotes_server.send_unreliables:fire(player, buf, variants)
end
end
end
system(replecs_server)