Skip to content

Setting Up

To start you need to create a Replicator for the server and the client.

Replecs exports replecs.create. This creates a holder that will have both client and server entries. These entries will only be present on the client and server respectively.

You can then export each replicator type from their sides.

@shared/replicator.luau
local replecs = require("@pkg/replecs")
local world = require("world")
return replecs.create(world)

Having a holder that has both client and server is useful for methods that would be used in a shared file where you cant require the client or server replicators directly.

An example of this is the method for hooking after replication which most of the time will be called inside signals for shared components, but it’s exclusive to the client.


Additionally, you can create the client and server replicators separately using:

  • replecs.create_client
  • replecs.create_server.
local replecs = require("@pkg/replecs")
return replecs.create_server(world)

Your world can be provided to the replicator to be used in two ways:

  • Directly to the replicator constructor, as seen in the previous examples.
  • When initialiazing the replicator calling replicator:init(world)

After creating your replicators. You must add replecs.shared to every single component/tag that can be replicated. These components also need to be named with jecs.Name.

They need to be equally marked in both the client and server, This means that a marked component can’t be missing from one of the two.

Easiest way to do this is to save your components in a table and then iterate over them.

local replecs = require("@pkg/replecs")
local world = require("world")
-- create a table with all your components
local shared_components = {
foo = world:component(),
baz = world:entity(),
}
for name, component in shared_components do
world:set(component, jecs.Name, name)
world:add(component, replecs.shared)
end

After creating your replicators. You need to initialize them by calling replicator:init() before using them.

You can also provide your world here if you did not provide it in the constructor. This is useful in workflows where the world is not exported as a singleton.