Client Replicator
Disconnect
Section titled “Disconnect”type Disconnect = () -> ()Methods
Section titled “Methods”client:init()
Section titled “client:init()”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?) -> ()}Arguments
Section titled “Arguments”- world: The world to use for the replicators. Can also be provided when initing the replicator.
client:destroy()
Section titled “client:destroy()”Destroys the client replicator. This will disconnect jecs hooks and connections.
type Client = { destroy: (self: Client) -> ()}client:apply_updates()
Section titled “client:apply_updates()”Applies the updates from the server.
type Client = { apply_updates: (self: Client, buf: buffer, variants: {{any}}) -> ()}Arguments
Section titled “Arguments”- buf: The buffer containing the updates.
- variants: The variants table containing non-serialized values.
client:apply_unreliable()
Section titled “client:apply_unreliable()”Applies the unreliable values from the server.
type Client = { apply_unreliable: (self: Client, buf: buffer, variants: {{any}}) -> ()}Arguments
Section titled “Arguments”- buf: The buffer containing the updates.
- variants: The variants table containing non-serialized values.
client:apply_full()
Section titled “client:apply_full()”Applies the full state of the world from the server.
type Client = { apply_full: (self: Client, buf: buffer, variants: {any}) -> ()}Arguments
Section titled “Arguments”- buf: The buffer containing the updates.
- variants: The variants table containing non-serialized values.
client:hook()
Section titled “client:hook()”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,}Arguments
Section titled “Arguments”- action: The action to hook to.
- …: Hook arguments.
Returns
Section titled “Returns”- disconnect: A function that disconnects the hook when called.
client:override()
Section titled “client:override()”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,}client:added()
Section titled “client:added()”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,}Arguments
Section titled “Arguments”- callback: Function that gets called when the entity is created.
Returns
Section titled “Returns”- disconnect: A function that disconnects the hook when called.
client:get_server_entity()
Section titled “client:get_server_entity()”Gets the equivalent server entity for a client entity.
type Client = { get_server_entity: (self: Client, client_entity: Entity) -> number?}Arguments
Section titled “Arguments”- entity: The client entity.
Returns
Section titled “Returns”- server_entity: The server entity, This is typed a number to avoid using this in the client.
client:get_client_entity()
Section titled “client:get_client_entity()”Gets the equivalent client entity from a server entity.
type Client = { get_client_entity: (self: Client, server_entity: number) -> Entity?}Arguments
Section titled “Arguments”- server_entity: The server entity.
Returns
Section titled “Returns”- entity: The client entity.
client:register_entity()
Section titled “client:register_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) -> ()}Arguments
Section titled “Arguments”- entity: The client entity to bind.
- server_entity: The server entity to bind.
client:unregister_entity()
Section titled “client:unregister_entity()”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) -> ()}Arguments
Section titled “Arguments”- entity: The client entity to unbind.
client:encode_component()
Section titled “client:encode_component()”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}Arguments
Section titled “Arguments”- component: The component to encode.
Returns
Section titled “Returns”- number: The encoded component.
client:decode_component()
Section titled “client:decode_component()”Decodes a component from a number.
type Client = { decode_component: (self: Client, encoded: number) -> Component?}Arguments
Section titled “Arguments”- number: The number to decode, This is number from
encode_component.
Returns
Section titled “Returns”- component: The decoded component, or
nilif the number does not match a shared component.
client:get_shared_count()
Section titled “client:get_shared_count()”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}Returns
Section titled “Returns”- count: The number of shared components.
client:register_custom_id()
Section titled “client:register_custom_id()”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) -> ()}client:generate_handshake()
Section titled “client:generate_handshake()”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}Returns
Section titled “Returns”- handshake: The handshake info. This can be passed to
server:verify_handshake()
client:verify_handshake()
Section titled “client:verify_handshake()”Verifies a handshake info from server:generate_handshake().
type Client = { verify_handshake: (self: Client, handshake: HandshakeInfo) -> (success: boolean, err: string?)}Arguments
Section titled “Arguments”- handshake: The handshake info to verify.
Returns
Section titled “Returns”- success: Whether the handshake info is valid.
- err: The error message if the handshake was not successful.
client:set_serdes()
Section titled “client:set_serdes()”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>) -> ()}Arguments
Section titled “Arguments”- id: The component or pair to set the serdes to.
- serdes: The serdes table.
client:remove_serdes()
Section titled “client:remove_serdes()”Removes the serdes of a component or pair.
type Client = { remove_serdes: (self: Client, id: Component) -> ()}Arguments
Section titled “Arguments”- id: The component or pair to remove the serdes from.
client:set_preprocessor()
Section titled “client:set_preprocessor()”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>) -> ()}Arguments
Section titled “Arguments”- id: The component or pair to set the preprocessor to.
- preprocessor: The preprocessor table.
client:remove_preprocessor()
Section titled “client:remove_preprocessor()”Removes the preprocessor of a component or pair.
type Client = { remove_preprocessor: (self: Client, id: Component) -> ()}Arguments
Section titled “Arguments”- id: The component or pair to remove the preprocessor from.