Skip to content

Summary

This page will summarize the previous guides and create a basic project setup for replecs.

  1. Create your world and require replecs. This is to avoid creating a world before requiring replecs.

    • Directorysrc
      • Directoryshared
        • world.lua
    local jecs = require("@pkg/jecs")
    require("@pkg/replecs")
    return jecs.world()

  2. Create a Replicator in shared and re-export the client and server entries:

    • Directorysrc
      • Directoryclient
        • replicator.lua
      • Directoryserver
        • replicator.lua
      • Directoryshared
        • replicator.lua
        • world.lua
    local replecs = require("@pkg/replecs")
    local world = require("@shared/world")
    return replecs.create(world)

  3. Create your components and mark them as shared:

    • Directorysrc
      • Directoryclient
        • replicator.lua
      • Directoryserver
        • replicator.lua
      • Directoryshared
        • components.lua
        • replicator.lua
        • world.lua
    local jecs = require("@pkg/jecs")
    local replecs = require("@pkg/replecs")
    local world = require("@shared/world")
    local components = {
    position = world:component(),
    velocity = world:component(),
    player = world:component(),
    health = world:component(),
    alive = world:tag(),
    }
    for name, component in components do
    world:add(component, replecs.shared)
    world:set(component, jecs.Name, name)
    end

  4. Initialize replicators. The client requests the full state of the world, the server queues the player and activates it inside the replication system in the next step:

    • Directorysrc
      • Directoryclient
        • replicator.lua
        • init.client.lua
      • Directoryserver
        • pending-players.lua
        • replicator.lua
        • init.server.lua
      • Directoryshared
        • components.lua
        • replicator.lua
        • world.lua
    local replicator = require("@server/replicator")
    local remotes_server = require("@shared/remotes").server
    local pending_players = require("@server/pending-players")
    replicator:init()
    -- players are activated by the replecs-server system, not here
    remotes_server.request_full:on(function(player)
    table.insert(pending_players, player)
    end)
  5. Create systems to send updates and unreliable values. Pending players are activated right before collect_updates, any change made between get_full and collect_updates would be silently dropped for that player:

    • Directorysrc
      • Directoryclient
        • Directorysystems
          • replecs-client.lua
        • replicator.lua
      • Directoryserver
        • Directorysystems
          • replecs-server.lua
        • pending-players.lua
        • replicator.lua
      • Directoryshared
        • Directoryutils
          • collect.lua
          • interval.lua
        • components.lua
        • replicator.lua
        • world.lua
    local replicator = require("@server/replicator")
    local pending_players = require("@server/pending-players")
    local interval = require("@utils/interval")
    local updates_interval = interval(1 / 20)
    local unreliables_interval = interval(1 / 30)
    function replecs_server()
    if updates_interval() then
    -- activate new players right before collecting updates
    for _, player in pending_players do
    if not player.Parent then
    continue
    end
    replicator:mark_player_ready(player)
    local buf, variants = replicator:get_full(player)
    remotes_server.receive_full:fire(player, buf, variants)
    end
    table.clear(pending_players)
    for player, buf, variants in replicator:collect_updates() do
    remotes_server.send_updates:fire(player, buf, variants)
    end
    end
    if unreliables_interval() then
    for player, buf, variants in replicator:collect_unreliable() do
    remotes_server.send_unreliables:fire(player, buf, variants)
    end
    end
    end
    return replecs_server