Class SFSUserVariable

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

public class SFSUserVariable extends com.smartfoxserver.entities.variables.BaseVariable implements UserVariable
UserVariables are custom values assigned to User objects that are automatically synchronized between client and server on every change. They are particularly useful to assign custom data such as profile data, game related state etc... And keep it updated among all Users in the same Room(s)

UserVariables 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.

UserVariables are visible (and updated) to all Users in the same Room, 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 the owner.
  • Private: a private User Variable is only visible to its owner and never transmitted to other clients.
The example below shows how to assign UserVariables to a User on the server side:

 	var userVars = List.of((UserVariable) new SFSUserVariable("Country", "Italy"), new SFSUserVariable("Age", 30));
 	getApi().setUserVariables(user, userVars);
 
 
See Also:
  • Field Details

    • hidden

      protected boolean hidden
    • priv

      protected boolean priv
    • storable

      protected boolean storable
  • Constructor Details

    • SFSUserVariable

      public SFSUserVariable(String name, 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(String name, 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 can only be seen by it's owner
    • SFSUserVariable

      public SFSUserVariable(String name, 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(String name)
    • SFSUserVariable

      protected SFSUserVariable(String name, VariableType type, String literal)
    • SFSUserVariable

      protected SFSUserVariable(String name, String type, String literal)
  • Method Details

    • newFromStringLiteral

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

      public static SFSUserVariable newFromSFSArray(ISFSArray array)
      Internal
    • newPrivateVariable

      public static SFSUserVariable newPrivateVariable(String name, Object value)
      Creates a private variable
    • isHidden

      public boolean isHidden()
      Checks if the variable is hidden
      Specified by:
      isHidden in interface UserVariable
      Returns:
      true if the variable is hidden
      See Also:
    • setHidden

      public void setHidden(boolean flag)
      Description copied from interface: UserVariable
      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 clients.
      Specified by:
      setHidden in interface UserVariable
      Parameters:
      flag -
      Internal
    • 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
    • toSFSArray

      public ISFSArray toSFSArray()
      Specified by:
      toSFSArray in interface Variable
      Internal
    • 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
    • clone

      @Deprecated public Object clone()
      Deprecated.
      Overrides:
      clone in class Object
      Internal
    • toString

      public String toString()
      Overrides:
      toString in class Object