Net¶
System · Net · server and client scripts
Net declares typed client-server messages and sends them between players and the server. Declare messages in a shared module with ToServer or ToClients, register handlers with On or Once, and send with Send, SendTo, SendAll, SendExcept, or SendNear.
ToServer¶
ToServer(name: string, fields: { [string]: any }?, options: ServerMessageOptions?) -> ServerMsg<any>
ToServer declares a message sent from clients to the server and returns an opaque ServerMsg handle. fields is an optional field table (using Field helpers or typed defaults), and options accepts Reliable (default true) and Timing ("Arrival" or "Input"). In Field.List(Field.Entity), any entity not replicated to the sender resolves to nil at its original index, so iterate with a numeric loop rather than ipairs.
Example
ToClients¶
ToClients(name: string, fields: { [string]: any }?, options: ClientMessageOptions?) -> ClientMsg<any>
ToClients declares a message sent from the server to clients and returns an opaque ClientMsg handle. fields is an optional field table, and options accepts Reliable (default true).
Example
On¶
On registers a handler for a declared message and returns a Connection. On the server, the handler receives (player, data?) for a ServerMsg. On a client, the handler receives (data?) for a ClientMsg.
Example
Once¶
Once registers a one-shot handler for a declared message, disconnects it after the next matching delivery, and returns a Connection.
Example
Send¶
Send sends a ServerMsg from the local client to the server and returns true if the message fit in this tick's network budget.
Example
Budget¶
Budget returns the remaining per-tick outbound message byte budget on the local client.
SendTo¶
SendTo sends a ClientMsg from the server to one player or a list of players and returns true when queued.
Example
SendAll¶
SendAll sends a ClientMsg from the server to every connected player and returns true when queued.
SendExcept¶
SendExcept sends a ClientMsg from the server to every connected player except one player or a list of players and returns true when queued.
Example
SendNear¶
SendNear sends a ClientMsg from the server to every player whose spatial interest set covers origin (a Vector3 or an Entity) and returns true when queued.