This translation is older than the original page and might be outdated. See what has changed.
Translations of this page:



Synchronizing the state of items

The position, rotation and scale of items are automatically synchronized by SubItem. In this page, we will explain how to synchronize other elements, such as on/off of a switch or a counter.

As an example, we will switch the on/off state of a switch with onUse (pressing on the grip button while grabbing a SubItem). When the switch is off, change the color of the material 0 to white, when on, change the color of the material 0 to red.

Method 1: use _ALL_ functions

Use _ALL_ functions to run commands on all users simultaneously.

isRed = false -- not local
function onUse()
    isRed = not isRed -- flip the true, false
    if isRed then
        vci.assets._ALL_SetMaterialColorFromIndex(0, Color.__new(1, 0, 0, 1)) -- Red
        vci.assets._ALL_SetMaterialColorFromIndex(0, Color.__new(1, 1, 1, 1)) -- white

It seems simple, but it doesn't work in the following situations

  • The color doesn't change on the user who joined the room in the middle of the session.
  • When another user grabbed the item and pressed the grip button (The variable holding the state of the color is in the original user; thus it won't be transferred with the ownership)

Method 2: Run the command on all users though Item Sync Variable

Use the vci.state.

if vci.assets.isMine then
    -- Initialize with the item spawn, only in the user who spawned it
    vci.state.Set('isRed', false) -- Initial value. `nil` is set if nothing is done to it
function onUse()
  local isRed = vci.state.Get('isRed') -- Acquire true or false local
  vci.state.Set('isRed', not isRed) -- Just change the state, not the actual color
function updateAll() -- Use updateAll because this needs to be run on every users.
    local isRed = vci.state.Get('isRed') -- Acquire the state. The item sync variable will be the same in all users (synced) local
    if isRed then
        vci.assets.SetMaterialColorFromIndex(0, Color.__new(1, 0, 0, 1)) -- Red
        vci.assets.SetMaterialColorFromIndex(0, Color.__new(1, 1, 1, 1)) -- white

Which method to use

  • _ALL_ can be used for one-shot, one-way usages like playing a short sound.
  • The item sync variable can be used for everything else
en/vci/sync/tips1.txt · Last modified: 2021/11/18 18:18 by numa

Page Tools