en:vci:script:reference:exportstate

Table of Contents

ExportState

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.

  • The variables will be synchronized among all users.
  • The variable will be empty when the item is called. (The variable will be initialized when the VCI is respawned.)
  • Depending on the type of space, the variable may be reset when everyone leaves the space where the item was spawned.
    • For Rooms: not reset
    • For Studios: reset

Example:

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

Set()

Arguments

  • name : string
  • value : number, string

Summary

Request to set a value (not yet set).

Types that can be set are

  • number
  • string

only. Not yet compatible with Tables.

Example:

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

Get()

Arguments

  • name : string

Return values

  • The value Set or Added by yourself or other users.
  • The default value is nil.

Summary

main.lua
local value = vci.state.Get('value' )

Add()

Arguments

  • name : string
  • value : number

Summary

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:

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

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

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

Page Tools