Item Sync Variable1). This function allows you to have variables associated with a specific item.
The amount of data that can be stored with vci.state is limited to a maximum of 16KB.
Example:
if vci.assets.IsMine then -- The user who spawned the item initializes the state vci.state.Set('switch', 0) end function updateAll() -- This is run by all users regardless of ownership --print(vci.state.Get('switch')) if vci.state.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 function onUse(use) print('use') vci.state.Set('switch', 1) end function onUnuse(use) print('unuse') vci.state.Set('switch', 0) end
Request to set a value (not yet set).
Types that can be set are
only. Not yet compatible with Tables.
Example:
vci.state.Set('value', 100) -- Request to set the value (Not yet set) local value = vci.state.Get('value' ) -- You get a value before setting the value above. The new value will be applied in the next frame.
Unlike normal variables, state variables don't change instantly.
local value = vci.state.Get('value' )
It's a convenient function. It is useful when multiple users change a value at the same time.
Things might not work properly when multiple users try to increment and reassign the value acquired through Get() at the same time.
Example:
vci.state.Set('value', vci.state.Get() + 1) -- The value from Get is incremented by 1
If two or more users execute the script above simultaneously the value only increments by one. (The last one takes precedence and overwrites the others) You can avoid this by using Add.
vci.state.Add('value', 1) -- A command to increment the value
When two or more users execute the script above simultaneously the value increments by the times it is run.