Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Lua Networking

James Wilkinson edited this page Aug 11, 2017 · 2 revisions

A collection of helper Lua functions to allow mods to send networked data to other players who have the mod installed.
All data is sent as string data between clients, so any non-string data, such as numbers or Vector3's, must be converted into a string before transmission.

LuaNetworking:IsMultiplayer()

Checks if the game is in a multiplayer state, and has an active multiplayer session.
returns The active multiplayer session, or nil.

if LuaNetworking:IsMultiplayer() then
	log("Multiplayer!")
else
	log("Single-player!")
end

LuaNetworking:IsHost()

Checks if the local player is the host of the multiplayer game session.
returns True if the local player is the host of the game session, or false. Also returns false if not multiplayer session is running.

if LuaNetworking:IsHost() then
	log("I am the host!")
end

LuaNetworking:IsClient()

Checks if the local player is a client of the multiplayer game session.
returns True if the local player is not the host of the game session, or false. Also returns false if not multiplayer session is running.

if LuaNetworking:IsClient() then
	log("I am a client!")
end

LuaNetworking:LocalPeerID()

Returns the peer ID of the local player.
returns The peer ID of the local player in the current multiplayer session. Returns 0 if no session was found.

local peer_id = LuaNetworking:LocalPeerID()

LuaNetworking:GetNameFromPeerID( id )

Returns the name of the player associated with the specified peer ID id.
id The peer ID to lookup in the session players and get their name.
returns The name of the player with peer ID id. Returns No Name if a name could not be found the specified ID.

local my_id = LuaNetworking:LocalPeerID()
local my_name = LuaNetworking:GetNameFromPeerID( my_id )
log( "My name is: " .. my_name )

LuaNetworking:GetPeers()

An accessor for the session peers table.
returns The table of all connected peers in the current multiplayer session.

for id, ply in pairs( LuaNetworking:GetPeers() ) do
	log( id .. " = " .. LuaNetworking:GetNameFromPeerID( id ) )
end

LuaNetworking:GetNumberOfPeers()

Utility function for quickly and easily getting the number of players in the multiplayer session.
returns The number of connected players in the current session.

local num = LuaNetworking:GetNumberOfPeers()
log( "There are " .. tostring(num) .. " connected players." )

LuaNetworking:SendToPeers( id, data )

Sends networked data data with a message id of id to all connected players.
id The unique identifier of the data to send to connected players. This value shoud be unique to the data type you are sending, or to the remote function you wish to call on another players game.
data The data to send to all connected players. It should be in string format so that it will be transmitted to other players properly.

LuaNetworking:SendToPeers( "GoonModCustomLaserColor", "1.0000000,0.0000000,1.0000000" )

LuaNetworking:SendToPeer( peer, id, data )

Identical to Net:SendToPeers, except the first argument is the ID of the peer who should receive the data.
peer The peer ID of the player who should receive the networked data.
id The unique identifier of the data to send to the peer with id id.
data The data to send.

local message_text = "This is a private message to the player with Peer ID 1."
LuaNetworking:SendToPeer( 1, "PrivateMessage", message_text )

LuaNetworking:SendToPeersExcept( peer, id, data )

Identical to Net:SendToPeer, except the first argument is a peer ID, or table or peer ID's who should be excluded from receiving the networked data.
peer A peer ID, or a table of peer ID's, who should be excluded from receiving this networked data.
id The unique identifier of the data to send to the peer with id id.
data The data to send.

local message_text = "This message will be send to everybody who does not have a Peer ID of 4."
LuaNetworking:SendToPeersExcept( 4, "PrivateMessage", message_text )

local exclude = { 1, 2 }
local message_text = "This message will be send to everybody who's peer ID does not appear in the exlude table"
LuaNetworking:SendToPeersExcept( exclude, "PrivateMessage", message_text )

LuaNetworking:TableToString( tbl )

Converts a table into a string which is able to sent through the Networking functions. It will only perform a shallow conversion, meaning it should not be used with nested tables.
tbl The table to be converted into a string.
returns A string representation of the table passed into the function.

local tbl = { 1, 2, 3, 4, "hello world!" }
local str = LuaNetworking:TableToString( tbl )
LuaNetworking:SendToPeers( "NetworkTableStringTest", str )

LuaNetworking:StringToTable( str )

Converts a formatted string into a table. Should be used for turning a networked table sent as a string back into its original table.
str The formatted string to attempt to convert back into a table.
returns A table containing the formatted data from str.

local str = "key|value,key2|another value,another key|yet another value"
local tbl = LuaNetworking:StringToTable( str )

LuaNetworking:ColourToString( col )

Converts a colour into a formatted string, which is able to be sent through the Networking functions.
col The colour to be converted into a formatted string.
returns A formatted string containing the colour data from col.

local color = Color.red
local col_str = LuaNetworking:ColourToString( color )
LuaNetworking:SendToPeers( "NetworkColourStringTest", col_str )

LuaNetworking:StringToColour( str )

Converts a formatted string into a colour. Should be used for turning a networking colour sent as a string back to its original object.
str The formatted string to attempt to convert back into a Color object.
returns A Color object containing the original colour.

local col_str = "r:1.0000|g:0.2125|b:0.4568|a:1.0000"
local color = LuaNetworking:StringToColor( col_str )