en:vci:script:reference:exportshared

ExportShared

Global Sync Variable1). A feature to store variables to the room without associating them to specific items.

main.lua
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.

main.lua
-- 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)
main.lua
-- 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

Set()

Arguments

  • name : string
  • value : number, string

Get()

Arguments

  • name : string

Return values

  • number, string

Bind()

Arguments

  • name : string
  • callback : function

Summary

It was implemented in the past when updateAll was still not available.

Add()

Arguments

  • name : string
  • value : usertype

Summary

Please refer to ExportState.Add.

The weakness of the global sync variable and alternate solution

The global sync variable has some weaknesses in its implementation.

  • There is no way to avoid the overlap of variable names in between items
  • We cannot clear the variables as we cannot know the owner of the variable

Example of using "vci.state + message" in place of "vci.studio.shared.Get"

In case of global sync variable

main.lua
local value = vci.studio.shared.Get('valueName')

The alternative

The one to Get
main.lua
-- Define the function to receive the value beforehand
message.On('response_valueName', function()
 
end)
-- Request the Value
message.Emit('request_valueName')
The one to be inquired
main.lua
-- 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

1)
Originally called “Shared Variable”, but the naming has been revised to “Global Sync Variable” in order to clearly indicate that it is one of the “Sync Variables” along with “Item Sync Variable”.
en/vci/script/reference/exportshared.txt · Last modified: 2021/11/18 18:10 by numa

Page Tools