Class SFSUserVariable
- java.lang.Object
-
- com.smartfoxserver.v2.entities.variables.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
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedSFSUserVariable(java.lang.String name)protectedSFSUserVariable(java.lang.String name, VariableType type, java.lang.String literal)SFSUserVariable(java.lang.String name, java.lang.Object value)Create a new UserVariableSFSUserVariable(java.lang.String name, java.lang.Object value, boolean isHidden)Create a new UserVariableSFSUserVariable(java.lang.String name, java.lang.Object value, boolean isHidden, boolean isPrivate)Create a new UserVariableprotectedSFSUserVariable(java.lang.String name, java.lang.String type, java.lang.String literal)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Objectclone()java.lang.BooleangetBoolValue()Get the value of the variable as Booleanjava.lang.DoublegetDoubleValue()Get the value of the variable as Doublejava.lang.IntegergetIntValue()Get the value of the variable as Integerjava.lang.StringgetName()ISFSArraygetSFSArrayValue()Get the value of the variable as SFSArrayISFSObjectgetSFSObjectValue()Get the value of the variable as SFSObjectjava.lang.StringgetStringValue()Get the value of the variable as StringVariableTypegetType()Return the data type of the variablejava.lang.ObjectgetValue()Get the value of the variable as ObjectbooleanisHidden()Checks if the variable is "hidden".booleanisNull()Test if a variable is nullbooleanisPrivate()Checks whether a UserVariable is private.booleanisStorable()Storable variables can be saved by the Room Persistence APIstatic SFSUserVariablenewFromSFSArray(ISFSArray array)SFSArray is expected to be formatted as follows 0: (str) -> name 1: (byte) -> type id 2: (Object) -> value 3: (bool) -> isPrivatestatic SFSUserVariablenewFromStringLiteral(java.lang.String name, java.lang.String type, java.lang.String literal)static SFSUserVariablenewInstance(java.lang.String name, java.lang.Object value)static SFSUserVariablenewPrivateVariable(java.lang.String name, java.lang.Object value)protected voidpopulateArrayWithValue(ISFSArray sfsa)Populate an SFSArray wih the typed value of this VariablevoidsetHidden(boolean flag)Set the variable as "hidden".voidsetNull()This is used by the system to mark removed variables.voidsetPrivate(boolean value)Setting a UserVariable as private restricts its visibility to the owner only.voidsetStorable(boolean value)Flags this variable as storable (default = true).protected voidsetValue(ISFSArray val)protected voidsetValue(ISFSObject val)protected voidsetValue(java.lang.Boolean val)protected voidsetValue(java.lang.Double val)protected voidsetValue(java.lang.Integer val)protected voidsetValue(java.lang.Object val)protected voidsetValue(java.lang.String val)protected voidsetValueFromStringLiteral(VariableType type, java.lang.String literal)ISFSArraytoSFSArray()Privatejava.lang.StringtoString()
-
-
-
Field Detail
-
name
protected java.lang.String name
-
value
protected volatile java.lang.Object value
-
type
protected VariableType type
-
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 variablevalue- 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 variablevalue- value of the variableisHidden- true if the variable needs to be server-side onlyisPrivate- 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 variablevalue- value of the variableisHidden- 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)
-
isHidden
public boolean isHidden()
Checks if the variable is "hidden".- Specified by:
isHiddenin interfaceUserVariable- Returns:
- true if the variable is hidden
- See Also:
UserVariable.setHidden(boolean)
-
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:
setHiddenin interfaceUserVariable
-
isPrivate
public boolean isPrivate()
Checks whether a UserVariable is private. Private UserVariables are only visible to the owner.- Specified by:
isPrivatein interfaceUserVariable- 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:
setPrivatein interfaceUserVariable- Parameters:
value-- Since:
- 2.12
-
setValueFromStringLiteral
protected void setValueFromStringLiteral(VariableType type, java.lang.String literal)
-
getType
public VariableType getType()
Return the data type of the variable- Specified by:
getTypein interfaceVariable- Returns:
- the data type of the variable
- See Also:
VariableType
-
getBoolValue
public java.lang.Boolean getBoolValue()
Get the value of the variable as Boolean- Specified by:
getBoolValuein interfaceVariable- Returns:
- the value of the variable
-
getDoubleValue
public java.lang.Double getDoubleValue()
Get the value of the variable as Double- Specified by:
getDoubleValuein interfaceVariable- Returns:
- the value of the variable
-
getIntValue
public java.lang.Integer getIntValue()
Get the value of the variable as Integer- Specified by:
getIntValuein interfaceVariable- Returns:
- the value of the variable
-
getSFSArrayValue
public ISFSArray getSFSArrayValue()
Get the value of the variable as SFSArray- Specified by:
getSFSArrayValuein interfaceVariable- Returns:
- the value of the variable
-
getSFSObjectValue
public ISFSObject getSFSObjectValue()
Get the value of the variable as SFSObject- Specified by:
getSFSObjectValuein interfaceVariable- Returns:
- the value of the variable
-
getStringValue
public java.lang.String getStringValue()
Get the value of the variable as String- Specified by:
getStringValuein interfaceVariable- Returns:
- the value of the variable
-
getValue
public java.lang.Object getValue()
Get the value of the variable as Object
-
isNull
public boolean isNull()
Test if a variable is null
-
setNull
public void setNull()
This is used by the system to mark removed variables.- Specified by:
setNullin interfaceUserVariable
-
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)
-
toSFSArray
public ISFSArray toSFSArray()
Description copied from interface:VariablePrivate- Specified by:
toSFSArrayin interfaceVariable
-
isStorable
public boolean isStorable()
Storable variables can be saved by the Room Persistence API- Specified by:
isStorablein interfaceUserVariable- 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:
setStorablein interfaceUserVariable
-
populateArrayWithValue
protected void populateArrayWithValue(ISFSArray sfsa)
Populate an SFSArray wih the typed value of this Variable- Parameters:
sfsa- theSFSArray
-
setValue
protected void setValue(java.lang.String val)
-
clone
public java.lang.Object clone()
- Overrides:
clonein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-