Class SFSUserVariable

  • All Implemented Interfaces:
    UserVariable, Variable, java.io.Serializable, java.lang.Cloneable
    Direct Known Subclasses:
    MMOItemVariable, SFSRoomVariable

    public class SFSUserVariable
    extends java.lang.Object
    implements UserVariable, java.io.Serializable
    UserVariables are custom values attached to each User object that get automatically updated between client and server on every change. They are particularly useful to "attach" any custom data to each User such as the current game status, the profile data and a lot more.

    UserVariables support basic data types and nested complex objects:

    • Null
    • Bool
    • Int
    • Double
    • String
    • SFSObject
    • SFSArray

    UserVariables are visibile among users in the same Room. In other words all Users in the same Room will be able to read all other player's variables with the exclusion of those marked as hidden or private.

    • Hidden: a hidden User Variable is only available on the server-side and never transmitted to other clients, including its owner.
    • Private: a private User Variable will only be visible to its owner and never transmitted to other clients, including those in the same Room as the owner's.

    NOTE: Private User Variables are available since SFS2X 2.12.0 and require a Client API version 1.7 or higher.

    An easy way to create a private UserVariable is this:

     UserVariable privateVar = SFSUserVariable.newPrivateVariable("Name", "My Value");
     
    See Also:
    Serialized Form
    • Field Detail

      • name

        protected java.lang.String name
      • value

        protected volatile java.lang.Object value
      • hidden

        protected boolean hidden
      • priv

        protected boolean priv
      • storable

        protected boolean storable
    • Constructor Detail

      • SFSUserVariable

        public SFSUserVariable​(java.lang.String name,
                               java.lang.Object value)
        Create a new UserVariable
        Parameters:
        name - name of the variable
        value - value of the variable (using any of the supported types)
      • SFSUserVariable

        public SFSUserVariable​(java.lang.String name,
                               java.lang.Object value,
                               boolean isHidden,
                               boolean isPrivate)
        Create a new UserVariable
        Parameters:
        name - name of the variable
        value - value of the variable
        isHidden - true if the variable needs to be server-side only
        isPrivate - true if the variable will only be seen by it's owner (other users are not updated)
      • SFSUserVariable

        public SFSUserVariable​(java.lang.String name,
                               java.lang.Object value,
                               boolean isHidden)
        Create a new UserVariable
        Parameters:
        name - name of the variable
        value - value of the variable
        isHidden - true if the variable needs to be server-side only
      • SFSUserVariable

        protected SFSUserVariable​(java.lang.String name)
      • SFSUserVariable

        protected SFSUserVariable​(java.lang.String name,
                                  VariableType type,
                                  java.lang.String literal)
      • SFSUserVariable

        protected SFSUserVariable​(java.lang.String name,
                                  java.lang.String type,
                                  java.lang.String literal)
    • Method Detail

      • newInstance

        public static SFSUserVariable newInstance​(java.lang.String name,
                                                  java.lang.Object value)
      • newFromStringLiteral

        public static SFSUserVariable newFromStringLiteral​(java.lang.String name,
                                                           java.lang.String type,
                                                           java.lang.String literal)
      • newFromSFSArray

        public static SFSUserVariable newFromSFSArray​(ISFSArray array)
        SFSArray is expected to be formatted as follows 0: (str) -> name 1: (byte) -> type id 2: (Object) -> value 3: (bool) -> isPrivate
        Parameters:
        array -
        Returns:
        the UserVariable
      • newPrivateVariable

        public static SFSUserVariable newPrivateVariable​(java.lang.String name,
                                                         java.lang.Object value)
      • setHidden

        public void setHidden​(boolean flag)
        Set the variable as "hidden". When this flag is turned on the variable will be available exclusively on the server side and it will never be transmitted to the client.
        Specified by:
        setHidden in interface UserVariable
      • isPrivate

        public boolean isPrivate()
        Checks whether a UserVariable is private. Private UserVariables are only visible to the owner.
        Specified by:
        isPrivate in interface UserVariable
        Returns:
        true if the UserVariable is private
        Since:
        2.12
      • setPrivate

        public void setPrivate​(boolean value)
        Setting a UserVariable as private restricts its visibility to the owner only. All other users in the same Room will not be able to see the variable nor they will be notified of any changes.

        When the flag is false, the UserVariable will be visible by all users in the same Room.

        Specified by:
        setPrivate in interface UserVariable
        Parameters:
        value -
        Since:
        2.12
      • setValueFromStringLiteral

        protected void setValueFromStringLiteral​(VariableType type,
                                                 java.lang.String literal)
      • getBoolValue

        public java.lang.Boolean getBoolValue()
        Get the value of the variable as Boolean
        Specified by:
        getBoolValue in interface Variable
        Returns:
        the value of the variable
      • getDoubleValue

        public java.lang.Double getDoubleValue()
        Get the value of the variable as Double
        Specified by:
        getDoubleValue in interface Variable
        Returns:
        the value of the variable
      • getIntValue

        public java.lang.Integer getIntValue()
        Get the value of the variable as Integer
        Specified by:
        getIntValue in interface Variable
        Returns:
        the value of the variable
      • getName

        public java.lang.String getName()
        Specified by:
        getName in interface Variable
      • getSFSArrayValue

        public ISFSArray getSFSArrayValue()
        Get the value of the variable as SFSArray
        Specified by:
        getSFSArrayValue in interface Variable
        Returns:
        the value of the variable
      • getSFSObjectValue

        public ISFSObject getSFSObjectValue()
        Get the value of the variable as SFSObject
        Specified by:
        getSFSObjectValue in interface Variable
        Returns:
        the value of the variable
      • getStringValue

        public java.lang.String getStringValue()
        Get the value of the variable as String
        Specified by:
        getStringValue in interface Variable
        Returns:
        the value of the variable
      • getValue

        public java.lang.Object getValue()
        Get the value of the variable as Object
        Specified by:
        getValue in interface Variable
        Returns:
        the value of the variable
      • isNull

        public boolean isNull()
        Test if a variable is null
        Specified by:
        isNull in interface Variable
        Returns:
        true if the variable is null
      • setNull

        public void setNull()
        This is used by the system to mark removed variables.
        Specified by:
        setNull in interface UserVariable
      • setValue

        protected void setValue​(java.lang.Boolean val)
      • setValue

        protected void setValue​(java.lang.Double val)
      • setValue

        protected void setValue​(java.lang.Integer val)
      • setValue

        protected void setValue​(ISFSArray val)
      • setValue

        protected void setValue​(ISFSObject val)
      • setValue

        protected void setValue​(java.lang.Object val)
      • isStorable

        public boolean isStorable()
        Storable variables can be saved by the Room Persistence API
        Specified by:
        isStorable in interface UserVariable
        Returns:
        true if the variable is storable, false otherwise
      • setStorable

        public void setStorable​(boolean value)
        Flags this variable as storable (default = true). All non-storable variables are ignored by the Room Persistence API
        Specified by:
        setStorable in interface UserVariable
      • populateArrayWithValue

        protected void populateArrayWithValue​(ISFSArray sfsa)
        Populate an SFSArray wih the typed value of this Variable
        Parameters:
        sfsa - the SFSArray
      • setValue

        protected void setValue​(java.lang.String val)
      • clone

        public java.lang.Object clone()
        Overrides:
        clone in class java.lang.Object
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object