Skip to content

Client Replicator


type Disconnect = () -> ()

Initializes the client replicator. World can be provided here if it wasn’t provided when creating the replicator.

type Client = {
init: (self: Client, world: jecs.World?) -> ()
}
  • world: The world to use for the replicators. Can also be provided when initing the replicator.

Destroys the client replicator. This will disconnect jecs hooks and connections.

type Client = {
destroy: (self: Client) -> ()
}

Applies the updates from the server.

type Client = {
apply_updates: (self: Client, buf: buffer, variants: {{any}}) -> ()
}
  • buf: The buffer containing the updates.
  • variants: The variants table containing non-serialized values.

Applies the unreliable values from the server.

type Client = {
apply_unreliable: (self: Client, buf: buffer, variants: {{any}}) -> ()
}
  • buf: The buffer containing the updates.
  • variants: The variants table containing non-serialized values.

Applies the full state of the world from the server.

type Client = {
apply_full: (self: Client, buf: buffer, variants: {{any}}) -> ()
}
  • buf: The buffer containing the updates.
  • variants: The variants table containing non-serialized values.

Hooks to a networking process. The arguments depend on the action type

type Client = {
hook: (self: Client, action: "changed", pair: Pair, callback: (entity: Entity, id: Id, value: any) -> ()) -> Disconnect,
hook: (self: Client, action: "removed", pair: Pair, callback: (entity: Entity, id: Id) -> ()) -> Disconnect,
hook: (self: Client, action: "deleted", entity: Entity, callback: (entity: Entity) -> ()) -> Disconnect,
}
  • action: The action to hook to.
  • : Hook arguments.
  • disconnect: A function that disconnects the hook when called.

Special hook that overrides the networking process entirely, allowing you to customize or abort actions. The usage is identical to client:hook().

type Client = {
override: (self: Client, action: "changed", pair: Pair, callback: (entity: Entity, id: Id, value: any) -> ()) -> Disconnect,
override: (self: Client, action: "removed", pair: Pair, callback: (entity: Entity, id: Id) -> ()) -> Disconnect,
override: (self: Client, action: "deleted", entity: Entity, callback: (entity: Entity) -> ()) -> Disconnect,
}

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.

type Client = {
added: (self: Client, callback: (entity: Entity) -> ()) -> Disconnect,
}
  • callback: Function that gets called when the entity is created.
  • disconnect: A function that disconnects the hook when called.

Gets the equivalent server entity for a client entity.

type Client = {
get_server_entity: (self: Client, client_entity: Entity) -> number?
}
  • entity: The client entity.
  • server_entity: The server entity, This is typed a number to avoid using this in the client.

Gets the equivalent client entity from a server entity.

type Client = {
get_client_entity: (self: Client, server_entity: number) -> Entity?
}
  • server_entity: The server entity.
  • entity: The client entity.

Binds a client entity to a server id. This would modify what get_server_entity and get_client_entity return.

type Client = {
register_entity: (self: Client, entity: Entity, server_entity: number) -> ()
}
  • entity: The client entity to bind.
  • server_entity: The server entity to bind.

Unbinds a client entity from a server id. This would also cause replecs to recreate a client entity for the server entity, and can possibly be used for force replecs to re-run custom ids.

type Client = {
unregister_entity: (self: Client, entity: Entity) -> ()
}
  • entity: The client entity to unbind.

Encodes a component for sending through the network. This returns a number between 1 to 255.

type Client = {
encode_component: (self: Client, component: Entity) -> number
}
  • component: The component to encode.
  • number: The encoded component.

Decodes a component from a number.

type Client = {
decode_component: (self: Client, encoded: number) -> Entity
}
  • number: The number to decode, This is number from encode_component.
  • component: The decoded component.

Registers a custom id handler. Custom ids can only be used if they are registered.

type Client = {
register_custom_id: (self: Client, custom_id: CustomId) -> ()
}

Generates a handshake info. This can be used to verify that the server and client are setup correctly. Relevant for components with shared,serdes and custom_ids, as a mistmatch in these between server and client would cause hard to debug issues.

type Client = {
generate_handshake: (self: Client) -> HandshakeInfo
}
  • handshake: The handshake info. This can be passed to server:verify_handshake()

Verifies a handshake info from server:generate_handshake().

type Client = {
verify_handshake: (self: Client, handshake: HandshakeInfo) -> (success: boolean, err: string?)
}
  • handshake: The handshake info to verify.
  • success: Whether the handshake info is valid.
  • err: The error message if the handshake was not successful.