Skip to content

API > SyncVar


SyncVar⚓︎

Defines a Synchronized variable. Synchronized Variables are variables that their value is synchronized on all clients of a multiplayer session. This property makes them ideal for storing game and object states that synched in all clients and persist even after change in master client. Type of variables Synchronized variables are flexible on type. They can adopt to the type of the input provided on SyncSet method. As a matter of fact, the type of the variable can be changed if needed. However, only the Serializable Types are properly synchronized. If a none-serializable type provided, it gets synchronized as a Nil. Check out Serializable Types. Size Limitation There is a size limitation on variables, but it is only relevant to strings and arrays. Please keep size of the string variables and arrays as low as possible. The value of the Synchronized Variables with size bigger than 700 bytes are most likely to not serialized properly. Late Joiners The Massive Loop guarantees that the all the synchronized variables in the session is received and ready to use before the Lua scripts start.

Members⚓︎

Properties⚓︎

Name Description
SyncVar.OnVariableChangePropertyReadonly Property Event handler that invokes when the value of the Synchronized variable changes by any clients.
SyncVar.OnVariableSetProperty Event handler called when the Sync variable set (even if still the same values set).

Constructor⚓︎

Name Description
SyncVar(script, Name, global=false)Constructor Creates or links the existing synchronized variable. If the synchronized variable does not exist in the multiplayer sessions, it creates and returns a new variable, otherwise it returns the existing variable.

Methods⚓︎

Name Description
SyncVar.Flush() Method Send the value of this variable to other clients again. You do not need to call this function to send the value of the variables to other.
SyncVar.SyncGet() Method Retrieves the (synchronized) value of the Synchronized variable.
SyncVar.SyncSet(Value) Method Sets the value of the variable and synchronizes it with other clients.
SyncVar.Update() Method Asks for latest value of the variable to sent to this client again. You do not need to call this function to receive the latest changes, the changes automatically received. However, in some rare cases it is needed to make sure the value is the latest.

Static Methods⚓︎

Name Description
SyncVar.Exists(script, name, gloabl=false) Static fieldMethod Checks if a synchronized variable with given name exists.
SyncVar.FlushAll() Static fieldMethod Flushes all the variables again.
SyncVar.UpdateAll() Static fieldMethod Updates all the variables again. Note that it will takes few frames for variables to be updated. Use OnVariableChange event handler.

Example⚓︎

do
    local script = LUA.script;

    local mySyncVar = SyncVar(script, "mySyncVar");

    local function OnVarChange(value)
        Debug.Log("mySyncVar's value changed by other client to: " .. value);
    end

    function script.Start
        mySyncVar.OnVariableChange.Add(OnVarChange);
        if mySyncVar.SyncGet() == nil then
            -- means that this is the master client 
            mySyncVar.SyncSet(10);
        else
            -- this is a late joiner
            Debug.Log(mySyncVar.SyncGet()); -- should print 10
        end
    end
end