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 the index of the component in the shared components list, a number from 1 to get_shared_count(). Throws an error if the component is not shared.

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) -> Component?
}
  • number: The number to decode, This is number from encode_component.
  • component: The decoded component, or nil if the number does not match a shared component.

Returns how many shared components there are. Every number returned by encode_component is between 1 and this value, so you can use it to size buffers or validate a number before calling decode_component.

type Client = {
get_shared_count: (self: Client) -> number
}
  • count: The number of shared components.

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.

Sets the serdes of a component or pair. This is the same as setting the replecs.serdes component. Serdes take part in the handshake, so the server must set a matching one.

type Client = {
set_serdes: <T>(self: Client, id: Component<T>, serdes: Serdes<T>) -> ()
}
  • id: The component or pair to set the serdes to.
  • serdes: The serdes table.

Removes the serdes of a component or pair.

type Client = {
remove_serdes: (self: Client, id: Component) -> ()
}
  • id: The component or pair to remove the serdes from.

Sets the preprocessor of a component or pair. The preprocessor runs after the serdes when reading a value. The handshake checks that the server also has a preprocessor for this component, but not what it does, so make sure it matches.

When set to a pair, the exact pair is used first, then jecs.pair(relation, jecs.Wildcard).

type Client = {
set_preprocessor: <B, A>(self: Client, id: Component<B>, preprocessor: Preprocessor<B, A>) -> ()
}
  • id: The component or pair to set the preprocessor to.
  • preprocessor: The preprocessor table.

Removes the preprocessor of a component or pair.

type Client = {
remove_preprocessor: (self: Client, id: Component) -> ()
}
  • id: The component or pair to remove the preprocessor from.