Sending Updates
Sending Changes
Section titled “Sending Changes”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)
.
Example:
Section titled “Example:”local replicator = require("@server/replicator")local interval = require("@utils/interval")local system = require("@utils/scheduler/system")
-- interval throttles systems to a specific intervallocal 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 endend
system(replecs_server)
local replicator = require("@client/replicator")local collect = require("@utils/collect")local system = require("@utils/scheduler/system")
-- collect converts events into iteratorslocal updates = collect(remotes_client.send_updates)
-- system running every framefunction replecs_client() for buf, variants in updates do replicator:apply_updates(buf, variants) endend
system(replecs_client)
Sending Unreliable Values
Section titled “Sending Unreliable Values”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.
Summary
Section titled “Summary”Combining both update types your client/server systems should look something like this:
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 endend
system(replecs_server)
local replicator = require("@client/replicator")local collect = require("@utils/collect")local system = require("@utils/scheduler/system")
local updates = collect(remotes_client.send_updates)local unreliables = collect(remotes_client.send_unreliables)
function replecs_client() for buf, variants in updates do replicator:apply_updates(buf, variants) end for buf, variants in unreliables do replicator:apply_unreliable(buf, variants) endend
system(replecs_client)