Skip to content
Stjepan Bakrac edited this page Sep 29, 2019 · 3 revisions

This library handles data sharing between packages.

local shared = require('shared')

✔️ Dependency Not Required

This library does not require an explicit dependency in the manifest.xml file for your package.

Tables

The shared table has the following entries:



shared.new

Creates a new shared object (also called the server since this holds the original data). Note that this value should not be local, or it may get garbage collected.

Definition

function shared.new(name : string) : shared_object

Parameters

name string

The name of the shared object. Will need this to access it from another package.

Return

shared_object

The shared object which contains the data and environment information.



shared.get

Retrieves a shared object from another package (also called the client since it reads the data from the server).

Definition

function shared.get(package : string, name : string) : shared_object

Parameters

package string

The name of the package where to look for the shared object.

name string

The name of the shared object within that package. The same name that was used to create it with shared.new.

Return

shared_object

The shared object which contains the data and environment information.



shared.read

Reads data from the provided shared object, based on the provided indices.

Definition

function shared.read(client : shared_object, ...indices : any) : any

Parameters

client shared_object

An object retrieved with shared.get.

...indices any

Any indices to access the shared data with. If, for example, the shared data contains the following table:

server.data = {
    recipes = {
        lasagna = {
            ingredients = {
                'lasagna',
            },
            steps = {
                'open packaging',
                'put in oven',
                'wait 3 hours',
                'enjoy',
            },
        },
    },
}

Then you would access it with shared.read(client, 'recipes', 'lasagna', 'steps', 4) and it would result in 'enjoy'.

Return

any

Returns the value inside the server's data store given by successively applying the provided indices.



shared.call

Calls the provided function with the server's data store as the first argument, followed by the given arguments. This function is executed with the server's env table as the environment, meaning that only functions defined inside env will be available to use inside the provided function.

Definition

function shared.call(client : shared_object, fn : function, ...args : any) : ...any

Parameters

client shared_object

An object retrieved with shared.get.

fn function

The function to call.

...args any

The arguments to provide to the function.

Return

...any

All results of the function called.



shared.pcall

Same as shared.call but calls the function in protected mode. The first result of the call is a boolean indicating success, followed by either the returned values of the provided function (if successful) or the error message (if not successful)

Definition

function shared.call(client : shared_object, fn : function, ...args : any) : boolean, ...any

Parameters

client shared_object

An object retrieved with shared.get.

fn function

The function to call.

...args any

The arguments to provide to the function.

Return

boolean

true if the pcall was successful, false if it failed.

...any

All results of the function called, if the pcall was successful, the error message if it failed.

Clone this wiki locally