Network

A userdata object representing a network object.

Network is used for sending data between scripts running on server and client. This allows the server to call a function on the client with optional arguments, and vice versa.

Note:

A network object is accessable via `self.network` in scripted shapes (see ShapeClass).

Warning:

Network allows any Lua data to be sent between the host and other players in real-time. This may result in high latency and lag in multiplayer.

To avoid lag and minimize bandwidth usage, consider only sending data when necessary, when data has changed, and attempt to send as little amount of data as possible.

Functions:


sendToClient(network, player, callbackMethod, args=nil)

Server only

Sends a network event from the server to a client. This will run the callback method on the client with optional arguments.

Parameters:

TypeNameDescription
NetworknetworkThe network.
PlayerplayerThe client player (or the host).
stringcallbackMethodThe client function name.
anyargs=nilOptional arguments to be sent to the client.

sendToClients(network, callbackMethod, args=nil)

Server only

Sends a network event from the server to all clients. This will run the callback method on every client with optional arguments.

-- Example of calling client function over network
function MyHorn.server_onSledgehammer( self, position, player ) 
	self.network:sendToClients( 'client_hit', position )
end
 
function MyHorn.client_hit( self, position ) 
	-- Play sound
	sm.audio.play( 'Horn', position )
end

Parameters:

TypeNameDescription
NetworknetworkThe network.
stringcallbackMethodThe client function name.
anyargs=nilOptional arguments to be sent to the client.

sendToServer(network, callbackMethod, args=nil)

Client only

Sends a network event from the client to the server. This will run the callback method on the server with optional arguments.

-- Example of calling server function over network
function MySwitch.client_onInteract( self ) 
	self.network:sendToServer( 'server_toggle' )
end

function MySwitch.server_toggle( self ) 
	-- Toggle on and off
	self.interactable.active = not self.interactable.active
end

Parameters:

TypeNameDescription
NetworknetworkThe network.
stringcallbackMethodThe server function name.
anyargs=nilOptional arguments to be sent to the server.

setClientData(network, data, channel=1)

Server only

Sets a lua object that will automatically be synchronized to clients.

Scripts which use this feature needs to implement 'client_onClientDataUpdate'.

'client_onClientDataUpdate' will be called on the client whenever the data has changed,

including setting the data for the first time.

Channel 1 will be received before channel 2 if both are updated.

-- Example:
function MyEngine.server_onCreate( self )
	self.network:setClientData( { "gear" = 1 } )
end
 
function MyEngine.client_onClientDataUpdate( self, data )
	self.interactable:setPoseWeight( 0, data.gear / self.maxGears )
end

Parameters:

TypeNameDescription
NetworknetworkThe network.
anydataPersistent data to be synchronized with existing and new clients.
integerchannel=1Client data channel, 1 or 2 (Optional)