Skip to content

Plugin network messages

Person8880 edited this page Sep 15, 2013 · 8 revisions

Overview

Plugin network messages provide an easier way to setup and deal with network messages in plugins.

Setting up network messages

To register network messages for a plugin, you need to use the function Plugin:AddNetworkMessage(), in the Plugin:SetupDataTable() method. This method is also used to set up data table variables. Make sure this is in the shared.lua file of the plugin.

There is a significant difference between data table variables and network messages. Data table variables are automatically sent to all players and synced when changed. This is useful for networking things like gamestates, settings flags, but not useful for initiating a one time action. Network messages are sent only once, and only to the players that they are set to send to at the time of sending.

function Plugin:SetupDataTable()
    self:AddNetworkMessage( "ServerTest", { Number = "integer (0 to 10)" }, "Client" )
    self:AddNetworkMessage( "ClientTest", { String = "string (10)" }, "Server" )
end

The arguments to Plugin:AddNetworkMessage() are:

  1. The name to give this network message, this is used later to send it, and to set up the receiving function.
  2. The table of data the network message will contain, this table is identical to those used in Shared.RegisterNetworkMessage().
  3. The environment that's going to receive this message, either "Server" or "Client".

Sending a message

To send a network message you have set up, use the Plugin:SendNetworkMessage() method.

On the server

function Plugin:UpdateNumber( Num )
    self:SendNetworkMessage( nil, "ServerTest", { Number = Num }, true )
end

The arguments to Plugin:SendNetworkMessage() on the server side are:

  1. The client(s) to send the message to. Either a ServerClient object, an array of ServerClients, or nil to send to everyone.
  2. The name of the network message, the same one used when adding it.
  3. The table of data to send.
  4. Whether to send the data reliably.

On the client

function Plugin:UpdateString( String )
    self:SendNetworkMessage( "ClientTest", { String = String }, true )
end

The arguments to Plugin:SendNetworkMessage() on the client side are:

  1. The name of the network message, the same one used when adding it.
  2. The table of data to send.
  3. Whether to send the data reliably.

Receiving a message

To receive a network message, make a function on the plugin's table, in the environment you set it to be received, called Receive<Name>. <Name> is the name of the message set when you added it.

if Server then
    function Plugin:ReceiveClientTest( Client, Data )
         self.String = Data.String
    end
elseif Client then
    function Plugin:ReceiveServerTest( Data )
         self.Number = Data.Number
    end
end

When receiving on the server, there are two arguments, the client who sent the message, and the data. When receiving on the client, the only argument is the table of data from the network message.

Clone this wiki locally