SmartFoxServer Silverlight API
SetRoomVariables Method (varList, roomId, setOwnership)
Namespaces ► SmartFoxClientAPI ► SmartFoxClient ► SetRoomVariables(List<(Of <(RoomVariable>)>), Int32, Boolean)
Set one or more Room Variables.
Room Variables are a useful feature to share data across the clients, keeping it in a centralized place on the server. When a user sets/updates/deletes one or more Room Variables, all the other users in the same room are notified.
Allowed data types for Room Variables are Numbers, Strings and Booleans; in order save bandwidth, Arrays and Objects are not supported. Nevertheless, an array of values can be simulated, for example, by using an index in front of the name of each variable (check one of the following examples).
If a Room Variable is set to null, it is deleted from the server.
Declaration Syntax

C# | Visual Basic | Visual C++

    public void SetRoomVariables(
    	List<RoomVariable> varList,
    	int roomId,
    	bool setOwnership
    )

    Public Sub SetRoomVariables ( _
    	varList As List(Of RoomVariable), _
    	roomId As Integer, _
    	setOwnership As Boolean _
    )

    public:
    void SetRoomVariables(
    	List<RoomVariable^>^ varList,
    	int roomId,
    	bool setOwnership
    )

Parameters

varList (List<(Of <(RoomVariable>)>))
  an array of objects with the properties described farther on.

roomId (Int32)
  the id of the room where the variables should be set, in case of molti-room join (optional, default value: activeRoomId).

setOwnership (Boolean)
  false to prevent the Room Variable change ownership when its value is modified by another user (optional).

Remarks

Sends:
SFSEvent..::.OnRoomVariablesUpdateDelegate

Version:
SmartFoxServer Basic / Pro

Examples
Each Room Variable is an object containing the following properties:

term | description
--- | ---
name | (string) the variable name.
value | (*) the variable value.
isPrivate | (bool) if {@code true}, the variable can be modified by its creator only (optional, default value: {@code false}).
isPersistent | (bool) if {@code true}, the variable will exist until its creator is connected to the current zone; if {@code false}, the variable will exist until its creator is connected to the current room (optional, default value: {@code false}).

The following example shows how to save a persistent Room Variable called "score". This variable won't be destroyed when its creator leaves the room.
CopyC#

    List<RoomVariable> rVars = new List<RoomVariable>();
    rVars.Add(new RoomVariable("score", 2500, false, true));

    smartFox.SetRoomVariables(rVars);

The following example shows how to save two Room Variables at once. The one called "bestTime" is private and no other user except its owner can modify it.
CopyC#

    List<RoomVariable> rVars = new List<RoomVariable>();
    rVars.Add(new RoomVariable("bestTime", 100, true, false));
    rVars.Add(new RoomVariable("bestLap", 120));

    smartFox.SetRoomVariables(rVars);

The following example shows how to delete a Room Variable called "bestTime" by setting its value to {@code null}.
CopyC#

    List<RoomVariable> rVars = new List<RoomVariable>();
    rVars.Add(new RoomVariable("bestTime", null));

    smartFox.SetRoomVariables(rVars);

The following example shows how to handle the data sent in the previous example when the {@link SFSEvent#onRoomVariablesUpdate} event is received.
CopyC#

     SFSEvent.onRoomVariablesUpdate += OnRoomVariablesUpdate;

    public void OnRoomVariablesUpdate(Room room, Dictionary<string, object> changedVars)
    {
        // Iterate on the 'changedVars' dictionary to check which variables were updated
            foreach (string v in changedVars.Keys)
                Debug.WriteLine(v + " room variable was updated; new value is: " + room.getVariable(v));
    }

The following example shows how to update a Room Variable without affecting the variable's ownership. By default, when a user updates a Room Variable, he becomes the "owner" of that variable. In some cases it could be needed to disable this behavoir by setting the setOwnership property to {@code false}.
CopyC#

    // For example, a variable that is defined in the server-side xml configuration file is owned by the Server itself;
    // if it's not set to private, its owner will change as soon as a user updates it.
    // To avoid this change of ownership the setOwnership flag is set to false.
    List<RoomVariable> rVars = new List<RoomVariable>();
    rVars.Add(new RoomVariable("shipPosX", 100));
    rVars.Add(new RoomVariable("shipPosY", 200));

    smartFox.SetRoomVariables(rVars, smartFox.GetActiveRoom().GetId(), false);

See Also
GetVariable(String)
GetVariables()()()
SFSEvent..::.OnRoomVariablesUpdateDelegate

Assembly: SmartFoxClientAPI_Silverlight (Module: SmartFoxClientAPI_Silverlight) Version: 1.0.0.0 (1.0.0.0)