Skip to content
Person8880 edited this page Jul 8, 2013 · 1 revision

Overview

Datatables provide an easy way to network values between the server and client states of a plugin. They work on the principle that the server can read and write values, whilst the client can only read values.

Setting up a datatable

To set up a datatable for a plugin, first make sure the plugin is in its own folder and has a shared.lua file. Inside this shared.lua file, you should add the following:

local Plugin = {}

function Plugin:SetupDataTable()
    self:AddDTVar( "boolean", "SomeBoolean", false )
    self:AddDTVar( "integer (0 to 10)", "SomeInt", 0 )
end

Shine:RegisterExtension( "myplugin", Plugin )

The function Plugin:SetupDataTable() is run in the load process, and inside it should be where you setup all the values you want in your datatable. The function Plugin:AddDTVar() takes arguments as follows:

Plugin:AddDTVar( String Type, String Name, Type DefaultValue[, String AccessCommand ] )

The type string should be the string you would use in an ordinary network message. The name is the key on the datatable that this value will be assigned to. The default value is the value to set initially. Lastly you can optionally set a command that a player must have access to in order to receive the value.

Using a datatable

Once you've set up the datatable, using it is easy. On the server, to change a value, just use:

self.dt.SomeBoolean = true

this will immediately propagate to all connected clients, and new connecting clients will receive the up to date datatable.

To read a value, just read the table value.

if self.dt.SomeBoolean then
    Print( "SomeBoolean is true!" )
end

Detecting changes

To detect changes in a plugin's network values, create the function:

Plugin:NetworkUpdate( Key, OldValue, NewValue )

in the shared.lua file (it must be here for loading reasons). This is run whenever a datatable value is changed, passing the key (e.g "SomeBoolean"), the old value (e.g false) and finally the new value it has taken (e.g true).

Clone this wiki locally