Skip to main content

SharedTable

SharedTable is a new way of networking, it prevents the hassle of syncing data from the server to the client. Note that shared tables are a lot more costly in network usage.

Example usage:

MyClass = class()

-- SERVER --

function MyClass:server_onCreate()
sm.scrapcomputers.sharedTable:init(self)

self.sv = {}

-- Shared tables automaticly sync variables at the root level, if you
-- are modifing a data inside of a table (eg self.sv.sharedData.myTable.myValue),
-- you will have to manually sync it with sm.scrapcomputers.sharedTable:forceSync

self.sv.sharedTable = sm.scrapcomputers.sharedTable:new(self, "self.cl.sharedTable")
self.sv.sharedTable.myCounter = 0
end

function MyClass:server_onFixedUpdate()
sm.scrapcomputers.sharedTable:runTick(self)

print("Server Current counter", self.sv.sharedTable.myCounter)
end

-- This event gets called when a client is updating a shared table, it can be used to allow-deny the client.
-- To block data, return false. Note that you are able to cause corruption with this so handle it with care!
--
-- This event is not required to be defined, by default it allows anything
---@param data {id: integer, data: any?, isEnd: boolean}
---@param player Player
function MyClass:server_onSharedTableEventReceived(data, player)
return true
end

-- This event gets called when theres a variable change in the shared table. It can be used to rewrap objects
-- to have their functions back. You can also return a boolean to allow the change or not.
--
-- comesFromSelf is true when the server edits the variable. if false then it was done by a client.
function MyClass:server_onSharedTableChange(id, key, value, comesFromSelf, player)
return true
end

-- CLIENT --

function MyClass:client_onCreate()
sm.scrapcomputers.sharedTable:init(self)

self.cl = {}
end

function MyClass:client_onFixedUpdate()
sm.scrapcomputers.sharedTable:runTick(self)

print("Server Current counter", self.cl.sharedTable.myCounter)
end

function MyClass:client_onInteract()
self.cl.sharedTable.myCounter = 0
print("Resetted counter!")
end

-- This event gets called when theres a variable change in the shared table. It can be used to rewrap objects
-- to have their functions back. You can also return a boolean to allow the change or not.
--
-- comesFromSelf is true when the server edits the variable. if false then it was done by the client.
function MyClass:client_onSharedTableChange(id, key, value, comesFromSelf)
return true
end

Functions

new

sm.scrapcomputers.sharedTable:new( classInstance, clientPath )

Creates a new shared table (server-side).

Arguments:

  • classInstance [ SharedTable.ShapeClass ] The class instance (i.e., self for the class).
  • clientPath [ string ] The path on the client to store the shared table.

Returns:


runTick

sm.scrapcomputers.sharedTable:runTick( classInstance )

Runs a tick for SharedTables.

Arguments:

  • classInstance [ ShapeClass ] The class instance (i.e., self for the class).

init

sm.scrapcomputers.sharedTable:init( classInstance )

Initializes for shared tables to be used.

Arguments:

  • classInstance [ ShapeClass ] The class instance (i.e., self for the class).

forceSync

sm.scrapcomputers.sharedTable:forceSync( sharedTable )

Forcefully syncs a SharedTable.

This is necessary when the SharedTable contains inner tables.
Can be network-expensive as it sends all values over the network.

Arguments:


forceSyncProperty

sm.scrapcomputers.sharedTable:forceSyncProperty( sharedTable, propertyIndex )

Forcefully syncs a specific property in a SharedTable.
This is necessary if the SharedTable contains inner tables.

Arguments:

  • sharedTable [ SharedTable ] The shared table to sync
  • propertyIndex [ string ] The property to sync.

getSharedTableId

sm.scrapcomputers.sharedTable:getSharedTableId( sharedTable )

Gets the ID of the SharedTable.

Arguments:

  • sharedTable [ SharedTable ] The shared table to query

Returns:

  • [ integer ] The ID of the shared table.

getRawContents

sm.scrapcomputers.sharedTable:getRawContents( sharedTable )

Gets the raw contents of a SharedTable (without metamethods).
Any modifications applied to the raw contents will NOT be reflected in the shared table itself.

Arguments:

  • sharedTable [ SharedTable ] The shared table to query

Returns:

  • [ table ] The raw contents of the shared table.

disableSync

sm.scrapcomputers.sharedTable:disableSync( sharedTable )

Disables syncing for a SharedTable.

Arguments:

  • sharedTable [ SharedTable ] The shared table to disable syncing for.

enableSync

sm.scrapcomputers.sharedTable:enableSync( sharedTable )

Enables syncing for a SharedTable.

Arguments:

  • sharedTable [ SharedTable ] The shared table to enable syncing for.

isSyncingEnabled

sm.scrapcomputers.sharedTable:isSyncingEnabled( sharedTable )

Returns whether syncing is enabled for a SharedTable.

Arguments:

  • sharedTable [ SharedTable ] The shared table to check.

Returns:

  • [ boolean ] Whether syncing is enabled for the table.