shared_object - Windower/packages GitHub Wiki

An object representing shared data. It is created by calling shared.new and lives as a data server.

Clients can retrieve it by calling shared.get with the server's package name and the name used to create the server.

The client is limited in what it can do by what the server provides. The server can define two tables on a shared object:

local shared = require('shared')

server = shared.new('some_object')

server.data = {}
server.env = {}

The data table is used to store data available to the client. It can be accessed by shared.read. It is also passed as the first argument to the calling functions (shared.call and shared.pcall).

The env table defines the environment available to functions executed in the calling functions. If the env table is empty there's nothing that the calling functions can do other than directly access data (and there's shared.read for that). For example, this would not work:

shared.call(client, function(data) print(#data) end)

This will actually error because it cannot find print. To enable that, the server has to define env like this:

server.env = {
    print = print,
}

Now print would be accessible. Similarly it can define its own functions which clients should be able to use.

Furthermore each shared_object has the shared table as its __index metatable, meaning you can do this for convenience:

client:call(function(data) print(#data) end)

And the same with shared.pcall and shared.read.

⚠️ **GitHub.com Fallback** ⚠️