Class SFSRoomVariable

java.lang.Object
com.smartfoxserver.entities.variables.BaseVariable
com.smartfoxserver.entities.variables.SFSUserVariable
com.smartfoxserver.entities.variables.SFSRoomVariable
All Implemented Interfaces:
RoomVariable, UserVariable, Variable, Serializable, Cloneable

public class SFSRoomVariable extends SFSUserVariable implements RoomVariable
RoomVariables are custom values assigned to Room objects that get automatically synchronized between client and server on every change. They are particularly useful to assign custom data such game state, or specific properties to keep track of the current game, especially in turn-based scenarios.

RoomVariables support a number of different data types:

  • Null
  • Bool
  • Byte
  • Short
  • Int
  • Long
  • Float
  • Double
  • Vector2
  • Vector3
  • String
  • SFSObject
  • SFSArray
SFSObject/SFSArray can be nested to create even more complex data structures, where needed.

RoomVariables also support different flags:

  • Private: a private variable can only be modified by its owner/creator
  • Persistent: a persistent variable will continue to exist even if its creator has left the Room. Server-created RoomVariables are never be autom-removed
  • Global: a global variable trigger updates not only to all Users in the Room but also to Users subscribed to the the Room's Group
  • Hidden: an hidden variable is be kept on the server side only and can't be created or accessed from the client side
  • Storable: all variables are storable by default. When the flag is turned off the Room Persistence API will ignore the variable
See Also:
  • Constructor Details

    • SFSRoomVariable

      protected SFSRoomVariable(String name)
    • SFSRoomVariable

      protected SFSRoomVariable(String name, String type, String literal)
    • SFSRoomVariable

      public SFSRoomVariable(String name, Object value)
      Create a new Room Variable
      Parameters:
      name - the variable name
      value - the variable value (see supported data types at the top)
    • SFSRoomVariable

      public SFSRoomVariable(String name, Object value, boolean isPrivate, boolean isPersistent, boolean isGlobal)
      Create a new Room Variable
      Parameters:
      name - the variable name
      value - the variable value (see supported data types at the top)
      isPrivate - the private flag
      isPersistent - the persistent flag
      isGlobal - the global flag
  • Method Details

    • newFromStringLiteral

      public static SFSRoomVariable newFromStringLiteral(String name, String type, String literal)
      Internal
    • newFromSFSArray

      public static SFSRoomVariable newFromSFSArray(ISFSArray array)
      Internal
    • isGlobal

      public boolean isGlobal()
      Checks if a RoomVariable is global. Global RV are visible outside of the Room and available to all Users subscribed to its Room Group
      Specified by:
      isGlobal in interface RoomVariable
      Returns:
      true if the variable is global
      See Also:
    • isPersistent

      public boolean isPersistent()
      Checks if a RoomVariable is persistent
      Specified by:
      isPersistent in interface RoomVariable
      Returns:
      true if the variable is persistent
      See Also:
    • isPrivate

      public boolean isPrivate()
      Checks if a RoomVariable is private. Private Room variables are visible to all Users in the Room but only the owner can modify or delete them.
      Specified by:
      isPrivate in interface UserVariable
      Overrides:
      isPrivate in class SFSUserVariable
      Returns:
      true if the variable is private
      See Also:
    • setGlobal

      public void setGlobal(boolean flag)
      When a RoomVariable is set to global all changes will be broadcast not only to the Users in the Room but also to all other Users in the same Room Group
      Specified by:
      setGlobal in interface RoomVariable
      Parameters:
      flag -
    • setPersistent

      public void setPersistent(boolean flag)
      When a RoomVariable is set to persistent the variable will not be removed from a Room when the user leaves it. RoomVariables created by a certain User are removed when the User disconnects
      Specified by:
      setPersistent in interface RoomVariable
      Parameters:
      flag -
    • setPrivate

      public void setPrivate(boolean flag)
      When a RoomVariable is set to private only its owner can modify or delete it.
      Specified by:
      setPrivate in interface UserVariable
      Overrides:
      setPrivate in class SFSUserVariable
      Parameters:
      flag -
    • getOwner

      public User getOwner()
      Return the User that created the variable. Null is returned if the variable is owned by the Server
      Specified by:
      getOwner in interface RoomVariable
      Returns:
      the variable owner
    • setOwner

      public void setOwner(User user)
      Sets the creator of a variable. Null can be used to set the Server as the owner. When a variable is owned by the Server it will never be removed.
      Specified by:
      setOwner in interface RoomVariable
      Parameters:
      user -
    • getOwnerAsString

      public String getOwnerAsString()
      Internal
    • toSFSArray

      public ISFSArray toSFSArray()
      Specified by:
      toSFSArray in interface Variable
      Overrides:
      toSFSArray in class SFSUserVariable
      Internal
    • toString

      public String toString()
      Overrides:
      toString in class SFSUserVariable