Global Sync Variable1). A feature to store variables to the room without associating them to specific items.
if vci.assets.IsMine then -- The user who spawned the item initializes the state vci.studio.shared.Set('switch', 0) end function onUse(use) print('use') vci.studio.shared.Set('switch', 1) end function onUnuse(use) print('unuse') vci.studio.shared.Set('switch', 0) end
Bind is somewhat similar to updateAll, but it is a little different.
-- Bind. Called only when the variable is changed vci.studio.shared.Bind('switch', function(value) if value==1 then vci.assets.SetMaterialColorFromIndex(4, Color.__new(1, 0, 0, 1)) else vci.assets.SetMaterialColorFromIndex(4, Color.__new(0.5, 0.5, 0.5, 1)) end end)
-- Bind. Called every frame even the value is not changed function updateAll() -- This is run by all users regardless of ownership --print(vci.state.Get('switch')) if vci.studio.shared.Get('switch')==1 then vci.assets.SetMaterialColorFromIndex(4, Color.__new(1, 0, 0, 1)) else vci.assets.SetMaterialColorFromIndex(4, Color.__new(0.5, 0.5, 0.5, 1)) end end
It was implemented in the past when updateAll was still not available.
Please refer to ExportState.Add.
The global sync variable has some weaknesses in its implementation.
local value = vci.studio.shared.Get('valueName')
-- Define the function to receive the value beforehand message.On('response_valueName', function() end) -- Request the Value message.Emit('request_valueName')
-- When the value is requested message.On('request_valueName', function() -- Acquire a value from the item sync variable and return vci.Emit('respone_valueName', vci.state.Get('valueName')) end)
It is a little clumsy