### Start Local Development Server Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/README.md Starts a local development server, typically at `localhost:4321`, allowing you to preview your site in real-time as you make changes. This command is essential during the development phase. ```bash npm run dev ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/README.md Installs all the necessary packages listed in the project's `package.json` file. This is a standard Node.js/npm command required before running other project commands. ```bash npm install ``` -------------------------------- ### Get and Apply Full Game State - Luau Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/starting-replication.mdx This snippet demonstrates how to retrieve the full game state on the server and apply it on the client. On the server, `server_replicator:get_full(player)` is called to get the data buffer and variants. On the client, `client_replicator:apply_full(buffer, variants)` is used to initialize the client's world state. This is typically done when a player joins the game. ```Luau local replicator = require("@server/replicator") -- remote function in the server remotes_server.receive_full:set_callback(function(player) return replicator:get_full(player) end) ``` ```Luau local replicator = require("@client/replicator") -- remote function in the client local buf, variants = remotes_client.receive_full:invoke_server() replicator:apply_full(buf, variants) ``` -------------------------------- ### Initialize Replicators and Get Full World State (Luau) Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/summary.mdx Initializes the replicator on both the server and client. The server sets up a callback to receive full state requests and mark players as ready. The client invokes the server to get the full state and applies it. ```luau local replicator = require("@server/replicator") local remotes_server = require("@shared/remotes").server replicator:init() remotes_server.receive_full:set_callback(function(player) replicator:mark_player_ready(player) return replicator:get_full(player) end) ``` ```luau local replicator = require("@client/replicator") local remotes_client = require("@shared/remotes").client replicator:init() local buf, variants = remotes_client.receive_full:invoke_server() replicator:apply_full(buf, variants) ``` -------------------------------- ### Get Help with Astro CLI Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/README.md Displays the help documentation for the Astro CLI, listing available commands and their options. This is useful for understanding the full capabilities of the Astro command-line interface. ```bash npm run astro -- --help ``` -------------------------------- ### Mark Player Ready for Replication - Luau Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/starting-replication.mdx This Luau code snippet shows how to mark a player as ready for replication on the server. After a player has received the full state of the world, `server_replicator:mark_player_ready(player)` should be called. This ensures that the player will start receiving subsequent replication updates. The function also returns the full state data for the player. ```Luau local replicator = require("@server/replicator") -- remote function in the server remotes_server.receive_full:set_callback(function(player: Player) replicator:mark_player_ready(player) return replicator:get_full(player) end) ``` -------------------------------- ### Create Astro Project with Starlight Template Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/README.md This command initializes a new Astro project using the official Starlight template. It requires Node.js and npm to be installed. The command prompts for project name and other configurations. ```bash npm create astro@latest -- --template starlight ``` -------------------------------- ### Create World and Require Replecs (Luau) Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/summary.mdx Initializes the game world using jecs and requires the replecs library. This step is crucial to ensure the world is set up before replecs is used, preventing potential errors. ```luau local jecs = require("@pkg/jecs") require("@pkg/replecs") return jecs.world() ``` -------------------------------- ### Create Replicator and Export Client/Server Entries (Luau) Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/summary.mdx Sets up the Replicator in the shared module and then re-exports the client and server specific implementations. This allows for distinct replication logic on the client and server sides. ```luau local replecs = require("@pkg/replecs") local world = require("@shared/world") return replecs.create(world) ``` ```luau local replicator = require("@shared/replicator") return replicator.server ``` ```luau local replicator = require("@shared/replicator") return replicator.client ``` -------------------------------- ### Server Handshake Generation Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/server-replicator.mdx API to generate handshake information for verifying server and client setup. ```APIDOC ## GET /server:generate_handshake() ### Description Generates a handshake info. This can be used to verify that the server and client are setup correctly. ### Method GET ### Endpoint /server:generate_handshake ### Parameters None ### Response #### Success Response (200) - **handshake_info** (HandshakeInfo) - The handshake info. This can be passed to `client:verify_handshake()`. ### Response Example { "handshake_info": { "token": "some_token", "timestamp": 1678886400 } } ``` -------------------------------- ### Mark Shared Components for Replication (Luau) Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/setting-up.mdx Illustrates the process of marking components and entities as 'shared' using `replecs.shared` so they can be replicated across client and server. Components must also be named using `jecs.Name`. This example iterates over a table of shared components to apply the necessary attributes. ```luau 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 pairs(shared_components) do world:set(component, jecs.Name, name) world:add(component, replecs.shared) end ``` -------------------------------- ### Server Full State Retrieval Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/server-replicator.mdx API to get the complete world state for a specific player. ```APIDOC ## GET /server:get_full(player: Player) ### Description Gets the full state of the world for the provided player. ### Method GET ### Endpoint /server:get_full ### Parameters #### Query Parameters - **player** (Player) - Required - The player to get the full state of the world for. ### Response #### Success Response (200) - **buf** (buffer) - The buffer containing the full state of the world. - **variants** ({{any}}) - The variants table containing non-serialized values. ### Response Example { "buf": "...binary data...", "variants": { "some_key": "some_value" } } ``` -------------------------------- ### Create Systems for Updates and Unreliable Values (Luau) Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/summary.mdx Sets up server and client systems for sending updates and unreliable values. It utilizes utility functions for managing intervals to control the frequency of these updates. ```luau local replicator = require("@server/replicator") local interval = require("@utils/interval") local updates_interval = interval(1 / 20) local unreliables_interval = interval(1 / 30) function replecs_server() ``` -------------------------------- ### Define Shared Components and Mark for Replication (Luau) Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/summary.mdx Defines various components and tags for the ECS, such as position, velocity, and player status. It then marks these components as shared for replication and assigns them names. ```luau 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 ``` -------------------------------- ### Server-Side Replication: Sending Updates and Unreliables Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/summary.mdx Handles the server-side logic for sending updates and unreliable data. It checks intervals before collecting and firing updates to players. Dependencies include interval functions and the replicator module. ```lua if updates_interval() then 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 ``` -------------------------------- ### Client-Side Replication: Applying Updates and Unreliables Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/summary.mdx Manages the client-side replication process by applying received updates and unreliable data. It utilizes a collect utility to process event data. Dependencies include the replicator and collect modules. ```luau local replicator = require("@client/replicator") local collect = require("@utils/collect") 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) end end return replecs_client ``` -------------------------------- ### client:generate_handshake() Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/client-replicator.mdx Generates handshake information to verify correct server and client setup. This is crucial for components involving `shared`, `serdes`, and `custom_ids` to prevent hard-to-debug issues caused by mismatches. ```APIDOC ## client:generate_handshake() ### Description Generates handshake information to verify correct server and client setup. This is crucial for components involving `shared`, `serdes`, and `custom_ids` to prevent hard-to-debug issues caused by mismatches. ### Method Not applicable (method within a type definition) ### Endpoint Not applicable (method within a type definition) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```lua -- Assuming 'client' is an instance of the Client type local handshake_info = client:generate_handshake() ``` ### Response #### Success Response (200) - **handshake** (HandshakeInfo) - The generated handshake information. This can be passed to `server:verify_handshake()`. #### Response Example ```json { "token": "some_secure_token", "version": "1.0.0" } ``` ``` -------------------------------- ### Create Client Replicator with TypeScript Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/replecs.mdx Generates a new client replicator instance. Similar to the server, this function takes an optional world object that can be specified during creation or at a later time during replicator setup. ```typescript function Replecs.create_client(world: jecs.World?): Replecs.Client ``` -------------------------------- ### Luau Networked Game Setup: Server and Client Source: https://context7.com/pepeeltoro41/replecs/llms.txt This Luau code sets up a networked game environment for both server and client. It defines components, configures replication settings, manages player connections, and handles data synchronization through various remote events. It requires the 'jecs' and 'replecs' libraries. ```luau -- Shared code (both server and client) local jecs = require("@jecs") local replecs = require("@replecs") local world = jecs.world() -- Define components local Position = world:component() local Velocity = world:component() local Health = world:component() local Model = world:component() local Owner = world:component() -- Setup serdes for Position world:set(Position, replecs.serdes, { bytespan = 12, serialize = function(v: Vector3) local buf = buffer.create(12) buffer.writef32(buf, 0, v.X) buffer.writef32(buf, 4, v.Y) buffer.writef32(buf, 8, v.Z) return buf end, deserialize = function(buf: buffer) return Vector3.new( buffer.readf32(buf, 0), buffer.readf32(buf, 4), buffer.readf32(buf, 8) ) end, }) world:set(Velocity, replecs.serdes, { bytespan = 12, serialize = function(v: Vector3) local buf = buffer.create(12) buffer.writef32(buf, 0, v.X) buffer.writef32(buf, 4, v.Y) buffer.writef32(buf, 8, v.Z) return buf end, deserialize = function(buf: buffer) return Vector3.new( buffer.readf32(buf, 0), buffer.readf32(buf, 4), buffer.readf32(buf, 8) ) end, }) -- Create replicator local lib = replecs.create(world) -- SERVER CODE if lib.server then local server = lib.server local Players = game:GetService("Players") local RunService = game:GetService("RunService") -- Create remotes local ReliableRemote = Instance.new("RemoteEvent") ReliableRemote.Name = "Reliable" ReliableRemote.Parent = game.ReplicatedStorage local UnreliableRemote = Instance.new("UnreliableRemoteEvent") UnreliableRemote.Name = "Unreliable" UnreliableRemote.Parent = game.ReplicatedStorage local FullStateRemote = Instance.new("RemoteEvent") FullStateRemote.Name = "FullState" FullStateRemote.Parent = game.ReplicatedStorage local ClientReadyRemote = Instance.new("RemoteEvent") ClientReadyRemote.Name = "ClientReady" ClientReadyRemote.Parent = game.ReplicatedStorage local HandshakeRemote = Instance.new("RemoteFunction") HandshakeRemote.Name = "Handshake" HandshakeRemote.Parent = game.ReplicatedStorage -- Generate handshake local serverHandshake = server:generate_handshake() HandshakeRemote.OnServerInvoke = function() return serverHandshake end -- Player management local playerEntities = {} Players.PlayerAdded:Connect(function(player) print("Player joined:", player.Name) end) ClientReadyRemote.OnServerEvent:Connect(function(player) server:mark_player_ready(player) print("Player ready:", player.Name) -- Send full state local buffer, variants = server:get_full(player) FullStateRemote:FireClient(player, buffer, variants) end) Players.PlayerRemoving:Connect(function(player) local playerEntity = playerEntities[player] if playerEntity then world:delete(playerEntity) playerEntities[player] = nil end end) -- Replication loop RunService.Heartbeat:Connect(function() -- Reliable updates for player, buffer, variants in server:collect_updates() do pcall(ReliableRemote.FireClient, ReliableRemote, player, buffer, variants) end -- Unreliable updates for player, buffer, variants in server:collect_unreliable() do pcall(UnreliableRemote.FireClient, UnreliableRemote, player, buffer, variants) end end) -- Example: Create networked entity local entity = world:entity() world:add(entity, replecs.networked) world:set(entity, Position, Vector3.new(0, 5, 0)) world:add(entity, jecs.pair(replecs.reliable, Position)) world:set(entity, Velocity, Vector3.new(1, 0, 1)) world:add(entity, jecs.pair(replecs.unreliable, Velocity)) world:set(entity, Health, 100) world:add(entity, jecs.pair(replecs.reliable, Health)) end -- CLIENT CODE if lib.client then local client = lib.client local ReliableRemote = game.ReplicatedStorage:WaitForChild("Reliable") local UnreliableRemote = game.ReplicatedStorage:WaitForChild("Unreliable") local FullStateRemote = game.ReplicatedStorage:WaitForChild("FullState") local ClientReadyRemote = game.ReplicatedStorage:WaitForChild("ClientReady") local HandshakeRemote = game.ReplicatedStorage:WaitForChild("Handshake") -- Verify handshake local serverHandshake = HandshakeRemote:InvokeServer() local success, err = client:verify_handshake(serverHandshake) if not success then error("Handshake failed: " .. err) end -- Setup hooks client:added(function(entity) ``` -------------------------------- ### Utility: Interval Throttling Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/summary.mdx Provides a throttled interval function that returns true only after a specified number of seconds has elapsed since the last call. This is useful for rate-limiting operations. It uses `os.clock()` for timing. ```lua local function interval(s: number) local pin: number = nil :: any local function throttle() if not pin then pin = os.clock() end local elapsed = os.clock() - pin > s if elapsed then pin = os.clock() end return elapsed end return throttle end return interval ``` -------------------------------- ### Utility: Collecting Event Data Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/summary.mdx A utility function to collect data emitted from events, whether they are functions or Roblox event connections. It returns an iterator for the collected data. This is crucial for processing batched or real-time data streams. ```lua local function collect(event) local storage = {} local mt = {} local iter = function() local n = #storage return function() if n <= 0 then mt.__iter = nil return nil end n -= 1 return n + 1, unpack(table.remove(storage, 1) :: any) end end if type(event) == "function" then event(function(...) table.insert(storage, { ... }) mt.__iter = iter end) else event:Connect(function(...) table.insert(storage, { ... }) mt.__iter = iter end) end setmetatable(storage, mt) return (storage :: any) :: () -> (number, ...any) end return collect ``` -------------------------------- ### Handle New Entity Creations - Luau Source: https://context7.com/pepeeltoro41/replecs/llms.txt Registers a callback that executes whenever a new entity is created on the client during replication. This function is useful for performing custom setup actions for newly replicated entities, such as creating visual representations, initializing custom components, or linking them to other client-side systems. The callback receives the newly created entity as an argument. It's also common to hook into entity deletions to clean up any resources created by this callback. ```luau local world = jecs.world() local lib = replecs.create(world) local client = lib.client local Model = world:component() local Position = world:component() local entityModels = {} -- Hook into all entity creations local disconnect = client:added(function(entity) print("New entity created:", entity) -- Wait for components to be added task.wait() -- Check what components it has if world:has(entity, Model) then local modelData = world:get(entity, Model) local pos = world:get(entity, Position) -- Create visual representation local instance = createModel(modelData) instance.Position = pos entityModels[entity] = instance end end) -- Also listen for deletions to cleanup client:hook("deleted", jecs.Wildcard, function(entity) local instance = entityModels[entity] if instance then instance:Destroy() entityModels[entity] = nil end end) -- Disconnect when done disconnect() ``` -------------------------------- ### Generate Handshake Info for Replecs Client-Server Verification Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/client-replicator.mdx Generates handshake information to verify correct setup between the Replecs server and client. This is particularly important for components with `shared`, `serdes`, and `custom_ids` to prevent hard-to-debug issues arising from mismatches. It returns handshake information. ```typescript type Client = { generate_handshake: (self: Client) -> HandshakeInfo } ``` -------------------------------- ### Build Production Site Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/README.md Compiles your Astro project into a static production-ready site. The output is generated in the `./dist/` directory. This command is used before deploying your website. ```bash npm run build ``` -------------------------------- ### Preview Production Build Locally Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/README.md Serves the production build locally, allowing you to test the final output before deploying. This command uses the output generated by `npm run build`. ```bash npm run preview ``` -------------------------------- ### Create Replicators (Luau) Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/setting-up.mdx Demonstrates how to create a main Replicator holder that contains both client and server entries. This allows for shared methods and exporting specific replicators for each side. It requires the 'replecs' library. ```luau local replecs = require("@pkg/replecs") -- Shared Replicator local sharedReplicator = replecs.create() -- Server-specific Replicator (only available on server) local serverReplicator = sharedReplicator.server -- Client-specific Replicator (only available on client) local clientReplicator = sharedReplicator.client -- Example of exporting from respective sides: -- Server: return serverReplicator -- Client: return clientReplicator ``` -------------------------------- ### Create Separate Client/Server Replicators (Luau) Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/setting-up.mdx Shows how to create client and server replicators independently using `replecs.create_client` and `replecs.create_server`. This is an alternative to the combined `replecs.create` method. It requires the 'replecs' library and a 'world' object. ```luau local replecs = require("@pkg/replecs") -- Assuming 'world' is defined elsewhere local serverReplicator = replecs.create_server(world) local clientReplicator = replecs.create_client(world) ``` -------------------------------- ### Get Full World State for Player Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/server-replicator.mdx Retrieves the complete world state for a specified player. This method returns a buffer containing the state data and a variants table for non-serialized values. ```typescript type Server = { get_full: (self: Server, player: Player) -> (buffer, {{any}}) } ``` -------------------------------- ### Player Readiness Management Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/server-replicator.mdx APIs for marking players as ready and checking their readiness status. ```APIDOC ## POST /server:mark_player_ready(player: Player) ### Description Marks a player as ready to receive updates. Non-ready players will not receive updates until this is called. ### Method POST ### Endpoint /server:mark_player_ready ### Parameters #### Request Body - **player** (Player) - Required - The player to mark as ready. ### Response #### Success Response (200) - **status** (string) - Indicates the player has been marked as ready. ### Response Example { "status": "Player marked as ready." } ## GET /server:is_player_ready(player: Player) ### Description Checks if a player has been marked as ready to receive updates. ### Method GET ### Endpoint /server:is_player_ready ### Parameters #### Query Parameters - **player** (Player) - Required - The player to check. ### Response #### Success Response (200) - **is_ready** (boolean) - Whether the player is ready. ### Response Example { "is_ready": true } ``` -------------------------------- ### Run Astro CLI Commands Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/README.md Executes various commands provided by the Astro CLI, such as `astro add` for integrating tools or `astro check` for verifying your project's integrity. This is a versatile command for managing your Astro project. ```bash npm run astro ... ``` -------------------------------- ### Replecs.create_server Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/replecs.mdx Initializes and returns a new server replicator instance. ```APIDOC ## Replecs.create_server ### Description Creates a new server replicator. ### Method POST ### Endpoint /replecs/create_server ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **world** (jecs.World?) - Optional - The world to use for the replicators. Can also be provided when initing the replicator. ### Request Example ```json { "world": "" } ``` ### Response #### Success Response (200) - **Replecs.Server** (object) - The created server replicator instance. #### Response Example ```json { "instance": "" } ``` ``` -------------------------------- ### Get Client Entity Mapping in TypeScript Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/client-replicator.mdx Defines the `get_client_entity` method for the `Client` type. This function retrieves the client entity corresponding to a given server entity ID. It returns an `Entity` or null if no mapping exists. ```typescript type Client = { get_client_entity: (self: Client, server_entity: number) => Entity? } ``` -------------------------------- ### Server Initialization and Destruction Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/server-replicator.mdx APIs for initializing and destroying the server replicator, including managing world data and disconnecting hooks. ```APIDOC ## POST /server:init() ### Description Initializes the server replicator. The world can be provided here if it wasn't provided when creating the replicator. ### Method POST ### Endpoint /server:init ### Parameters #### Query Parameters - **world** (jecs.World?) - Optional - The world to use for the replicators. Can also be provided when initing the replicator. ### Response #### Success Response (200) - **status** (string) - Indicates successful initialization. ### Response Example { "status": "Server replicator initialized successfully." } ## POST /server:destroy() ### Description Destroys the server replicator. This will disconnect jecs hooks and connections. ### Method POST ### Endpoint /server:destroy ### Response #### Success Response (200) - **status** (string) - Indicates successful destruction. ### Response Example { "status": "Server replicator destroyed successfully." } ``` -------------------------------- ### Initialize Server Replicator Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/server-replicator.mdx Initializes the server replicator. The `world` argument is optional and can be provided either here or during the replicator's creation. This method sets up the necessary components for replication. ```typescript type Server = { init: (self: Server, world: jecs.World?) -> () } ``` -------------------------------- ### Get Server Entity Mapping in TypeScript Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/client-replicator.mdx Defines the `get_server_entity` method for the `Client` type. This function retrieves the corresponding server entity ID for a given client entity. It returns a number or null if no mapping is found. ```typescript type Client = { get_server_entity: (self: Client, client_entity: Entity) => number? } ``` -------------------------------- ### Replecs.create_client Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/replecs.mdx Initializes and returns a new client replicator instance. ```APIDOC ## Replecs.create_client ### Description Creates a new client replicator. ### Method POST ### Endpoint /replecs/create_client ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **world** (jecs.World?) - Optional - The world to use for the replicators. Can also be provided when initing the replicator. ### Request Example ```json { "world": "" } ``` ### Response #### Success Response (200) - **Replecs.Client** (object) - The created client replicator instance. #### Response Example ```json { "instance": "" } ``` ``` -------------------------------- ### Client Replicator Initialization and Destruction Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/client-replicator.mdx Methods for initializing and destroying the client replicator, managing its lifecycle and connections. ```APIDOC ## POST /client:init ### Description Initializes the client replicator. The world can be provided here if it wasn't provided when creating the replicator. ### Method POST ### Endpoint /client:init ### Parameters #### Query Parameters - **world** (jecs.World?) - Optional - The world to use for the replicators. Can also be provided when initing the replicator. ### Request Example ```json { "world": { ... } } ``` ### Response #### Success Response (200) - **status** (string) - Indicates successful initialization. #### Response Example ```json { "status": "initialized" } ``` --- ## DELETE /client:destroy ### Description Destroys the client replicator. This will disconnect jecs hooks and connections. ### Method DELETE ### Endpoint /client:destroy ### Parameters None ### Request Example ### Response #### Success Response (200) - **status** (string) - Indicates successful destruction. #### Response Example ```json { "status": "destroyed" } ``` ``` -------------------------------- ### Modifying Replication Filters (Luau) Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/guides/player-filtering.mdx This example shows how to change the replication filter for an entity after it has been initially set. To apply modifications, you must call `world:set` again with the updated filter. This allows dynamic adjustment of replication targets. ```luau local entity = world:entity() world:set(entity, replecs.networked, { [Player1] = true, [Player2] = true, }) -- now it only replicates to player1 world:set(entity, replecs.networked, { [Player1] = true, }) ``` -------------------------------- ### Replecs Initialization API Source: https://context7.com/pepeeltoro41/replecs/llms.txt Creates both server and client replicators in a single library instance, automatically detecting the execution environment and initializing the appropriate replicator. ```APIDOC ## replecs.create ### Description Creates both server and client replicators in a single library instance, automatically detecting the execution environment and initializing the appropriate replicator. ### Method `replecs.create(world)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **world** (table) - Required - The JECS world instance. ### Request Example ```luau local jecs = require("@jecs") local replecs = require("@replecs") -- Create replicator (auto-detects server vs client) local world = jecs.world() local lib = replecs.create(world) -- On server side if lib.server then local server = lib.server server:mark_player_ready(player) -- Collect updates each frame game:GetService("RunService").Heartbeat:Connect(function() for player, buffer, variants in server:collect_updates() do ReliableRemote:FireClient(player, buffer, variants) end for player, buffer, variants in server:collect_unreliable() do UnreliableRemote:FireClient(player, buffer, variants) end end) end -- On client side if lib.client then local client = lib.client ReliableRemote.OnClientEvent:Connect(function(buffer, variants) client:apply_updates(buffer, variants) end) UnreliableRemote.OnClientEvent:Connect(function(buffer, variants) client:apply_unreliable(buffer, variants) end) end ``` ### Response #### Success Response (200) - **lib** (table) - An object containing `server` and/or `client` properties depending on the execution environment. #### Response Example ```lua { server = or nil, client = or nil } ``` ``` -------------------------------- ### Replecs.create Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/replecs.mdx Creates a new ReplecsLib, which encapsulates both server and client replicators, providing methods for simultaneous interaction. ```APIDOC ## Replecs.create ### Description Creates a new `ReplecsLib`. This contains both the server and client replicators in one object and provides methods to interact with both of them at the same time. ### Method POST ### Endpoint /replecs/create ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **world** (jecs.World?) - Optional - The world to use for the replicators. Can also be provided when initing the replicator. ### Request Example ```json { "world": "" } ``` ### Response #### Success Response (200) - **ReplecsLib** (object) - An object containing server and client replicator instances. #### Response Example ```json { "server": "", "client": "", "after_replication": "", "register_custom_id": "" } ``` ``` -------------------------------- ### Initialize Client Replicator in TypeScript Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/client-replicator.mdx Defines the `init` method for the `Client` type. This method initializes the client replicator and optionally accepts a `jecs.World` object. If the world is not provided during replicator creation, it can be supplied here. ```typescript type Client = { init: (self: Client, world?: jecs.World) => () } ``` -------------------------------- ### Component Encoding and Decoding Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/server-replicator.mdx APIs for encoding and decoding components for network transmission. ```APIDOC ## POST /server:encode_component(component: Entity) ### Description Encodes a component for sending through the network. This returns a number between 1 to 255. ### Method POST ### Endpoint /server:encode_component ### Parameters #### Request Body - **component** (Entity) - Required - The component to encode. ### Response #### Success Response (200) - **encoded_value** (number) - The encoded component (1-255). ### Response Example { "encoded_value": 123 } ## POST /server:decode_component(encoded: number) ### Description Decodes a component from a number. This number should be from `encode_component`. ### Method POST ### Endpoint /server:decode_component ### Parameters #### Request Body - **encoded** (number) - Required - The number to decode, obtained from `encode_component`. ### Response #### Success Response (200) - **component** (Entity) - The decoded component. ### Response Example { "component": "...entity data..." } ``` -------------------------------- ### Create Server and Client Replicators with Replecs Source: https://context7.com/pepeeltoro41/replecs/llms.txt Initializes both server and client replicators within a single Replecs instance, automatically detecting the execution environment. It demonstrates how to set up the server to collect and send updates, and how clients receive and apply these updates via remote events. Dependencies include 'jecs' and 'replecs'. ```luau local jecs = require("@jecs") local replecs = require("@replecs") -- Create replicator (auto-detects server vs client) local world = jecs.world() local lib = replecs.create(world) -- On server side if lib.server then local server = lib.server server:mark_player_ready(player) -- Collect updates each frame game:GetService("RunService").Heartbeat:Connect(function() for player, buffer, variants in server:collect_updates() do ReliableRemote:FireClient(player, buffer, variants) end for player, buffer, variants in server:collect_unreliable() do UnreliableRemote:FireClient(player, buffer, variants) end end) end -- On client side if lib.client then local client = lib.client ReliableRemote.OnClientEvent:Connect(function(buffer, variants) client:apply_updates(buffer, variants) end) UnreliableRemote.OnClientEvent:Connect(function(buffer, variants) client:apply_unreliable(buffer, variants) end) end ``` -------------------------------- ### Mark Player as Ready for Updates Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/server-replicator.mdx Marks a specific player as ready to receive updates. Players who are not marked as ready will not receive any replication updates until this method is called for them. ```typescript type Server = { mark_player_ready: (self: Server, player: Player) -> () } ``` -------------------------------- ### Create Server Replicator with TypeScript Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/replecs.mdx Initializes a new server replicator instance. This function accepts an optional world object, which can also be provided during the replicator's initialization phase. ```typescript function Replecs.create_server(world: jecs.World?): Replecs.Server ``` -------------------------------- ### Client Replicator State Application Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/client-replicator.mdx Methods for applying updates, unreliable values, and the full world state from the server. ```APIDOC ## POST /client:apply_updates ### Description Applies the updates from the server. ### Method POST ### Endpoint /client:apply_updates ### Parameters #### Request Body - **buf** (buffer) - Required - The buffer containing the updates. - **variants** ({any}) - Required - The variants table containing non-serialized values. ### Request Example ```json { "buf": "...", "variants": { ... } } ``` ### Response #### Success Response (200) - **status** (string) - Indicates successful application of updates. #### Response Example ```json { "status": "updates_applied" } ``` --- ## POST /client:apply_unreliable ### Description Applies the unreliable values from the server. ### Method POST ### Endpoint /client:apply_unreliable ### Parameters #### Request Body - **buf** (buffer) - Required - The buffer containing the updates. - **variants** ({any}) - Required - The variants table containing non-serialized values. ### Request Example ```json { "buf": "...", "variants": { ... } } ``` ### Response #### Success Response (200) - **status** (string) - Indicates successful application of unreliable values. #### Response Example ```json { "status": "unreliable_applied" } ``` --- ## POST /client:apply_full ### Description Applies the full state of the world from the server. ### Method POST ### Endpoint /client:apply_full ### Parameters #### Request Body - **buf** (buffer) - Required - The buffer containing the updates. - **variants** ({any}) - Required - The variants table containing non-serialized values. ### Request Example ```json { "buf": "...", "variants": { ... } } ``` ### Response #### Success Response (200) - **status** (string) - Indicates successful application of the full world state. #### Response Example ```json { "status": "full_state_applied" } ``` ``` -------------------------------- ### Mark Player Ready for Replication (Luau) Source: https://context7.com/pepeeltoro41/replecs/llms.txt Marks a player as ready to receive replication data. This prevents updates from being sent before the client is fully initialized. It listens for a 'ClientReady' RemoteEvent and then fires the initial full state to the player. ```Luau local Players = game:GetService("Players") local world = jecs.world() local lib = replecs.create(world) local server = lib.server local ClientReadyRemote = Instance.new("RemoteEvent") ClientReadyRemote.Name = "ClientReady" ClientReadyRemote.Parent = game.ReplicatedStorage -- When player joins, don't immediately send data Players.PlayerAdded:Connect(function(player) print("Player joined:", player.Name, "- Waiting for client ready signal") end) -- Wait for client to signal it's ready ClientReadyRemote.OnServerEvent:Connect(function(player) if server:is_player_ready(player) then warn("Player already marked ready:", player.Name) return end -- Mark player as ready server:mark_player_ready(player) print("Player ready:", player.Name) -- Send initial full state local buffer, variants = server:get_full(player) game.ReplicatedStorage.FullState:FireClient(player, buffer, variants) -- Player now receives updates from collect_updates() and collect_unreliable() end) -- Client signals when ready: -- ClientReadyRemote:FireServer() ``` -------------------------------- ### Create ReplecsLib with TypeScript Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/replecs.mdx Creates a new ReplecsLib instance, which encapsulates both server and client replicators. It allows simultaneous interaction with both. The world object can be provided during creation or later during replicator initialization. ```typescript function Replecs.create(world: jecs.World?): ReplecsLib ``` -------------------------------- ### Apply Full Server State in TypeScript Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/client-replicator.mdx Defines the `apply_full` method for the `Client` type. This function is used to apply the complete world state from the server. It requires a buffer containing the full state and a variants table. ```typescript type Client = { apply_full: (self: Client, buf: buffer, variants: {{any}}) => () } ``` -------------------------------- ### Collect and Apply Updates (Luau) Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/started/sending-updates.mdx Demonstrates how to collect and apply reliable state updates for players. The server collects updates using `server_replicator:collect_updates()` and the client applies them with `client_replicator:apply_updates(buf, variants)`. This is crucial for synchronizing game states. ```luau local server_replicator = require(game.ServerStorage.Replicator) local client_replicator = require(game.ReplicatedStorage.Replicator) -- Server-side system to collect updates local function collect_updates() for player, buf, variants in server_replicator:collect_updates() do -- Send buf and variants to the player end end -- Client-side system to apply updates local function apply_updates(buf, variants) client_replicator:apply_updates(buf, variants) end ``` ```luau local server_replicator = require(game.ServerStorage.Replicator) local client_replicator = require(game.ReplicatedStorage.Replicator) -- Server-side system to collect updates local function collect_updates_server() for player, buf, variants in server_replicator:collect_updates() do -- Send buf and variants to the player end end -- Client-side system to apply updates local function apply_updates_client(buf, variants) client_replicator:apply_updates(buf, variants) end ``` -------------------------------- ### Client Replicator Entity Management Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/client-replicator.mdx Methods for managing entity lifecycle and mapping between client and server entities. ```APIDOC ## POST /client:added ### Description Hook that gets called when replecs creates a new entity. This is called right after the entity is created so it will be empty. Use `client:after_replication()` to wait for any useful info from it. This hook is not called if the entity was created with a custom id. ### Method POST ### Endpoint /client:added ### Parameters #### Request Body - **callback** ((entity: Entity) -> ()) - Required - Function that gets called when the entity is created. ### Request Example ```json { "callback": "function reference" } ``` ### Response #### Success Response (200) - **disconnect** (Disconnect) - A function that disconnects the hook when called. #### Response Example ```json { "disconnect": "function reference" } ``` --- ## GET /client:get_server_entity ### Description Gets the equivalent server entity for a client entity. ### Method GET ### Endpoint /client:get_server_entity ### Parameters #### Query Parameters - **client_entity** (Entity) - Required - The client entity. ### Request Example ```json { "client_entity": "..." } ``` ### Response #### Success Response (200) - **server_entity** (number?) - The server entity. This is typed as a number to avoid using this in the client. #### Response Example ```json { "server_entity": 123 } ``` --- ## GET /client:get_client_entity ### Description Gets the equivalent client entity from a server entity. ### Method GET ### Endpoint /client:get_client_entity ### Parameters #### Query Parameters - **server_entity** (number) - Required - The server entity. ### Request Example ```json { "server_entity": 123 } ``` ### Response #### Success Response (200) - **entity** (Entity?) - The client entity. #### Response Example ```json { "entity": "..." } ``` --- ## POST /client:register_entity ### Description Binds a client entity to a server id. This would modify what `get_server_entity` and `get_client_entity` return. ### Method POST ### Endpoint /client:register_entity ### Parameters #### Request Body - **entity** (Entity) - Required - The client entity to bind. - **server_entity** (number) - Required - The server entity to bind. ### Request Example ```json { "entity": "...", "server_entity": 123 } ``` ### Response #### Success Response (200) - **status** (string) - Indicates successful registration. #### Response Example ```json { "status": "entity_registered" } ``` ``` -------------------------------- ### Check if Player is Ready Source: https://github.com/pepeeltoro41/replecs/blob/main/docs/src/content/docs/reference/server-replicator.mdx Checks whether a given player has been marked as ready to receive updates. Returns a boolean indicating the player's readiness status. ```typescript type Server = { is_player_ready: (self: Server, player: Player) -> boolean } ```