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 protected
SFSUserVariable(java.lang.String name)
protected
SFSUserVariable(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 UserVariableprotected
SFSUserVariable(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.Object
clone()
java.lang.Boolean
getBoolValue()
Get the value of the variable as Booleanjava.lang.Double
getDoubleValue()
Get the value of the variable as Doublejava.lang.Integer
getIntValue()
Get the value of the variable as Integerjava.lang.String
getName()
ISFSArray
getSFSArrayValue()
Get the value of the variable as SFSArrayISFSObject
getSFSObjectValue()
Get the value of the variable as SFSObjectjava.lang.String
getStringValue()
Get the value of the variable as StringVariableType
getType()
Return the data type of the variablejava.lang.Object
getValue()
Get the value of the variable as Objectboolean
isHidden()
Checks if the variable is "hidden".boolean
isNull()
Test if a variable is nullboolean
isPrivate()
Checks whether a UserVariable is private.boolean
isStorable()
Storable variables can be saved by the Room Persistence APIstatic SFSUserVariable
newFromSFSArray(ISFSArray array)
SFSArray is expected to be formatted as follows 0: (str) -> name 1: (byte) -> type id 2: (Object) -> value 3: (bool) -> isPrivatestatic SFSUserVariable
newFromStringLiteral(java.lang.String name, java.lang.String type, java.lang.String literal)
static SFSUserVariable
newInstance(java.lang.String name, java.lang.Object value)
static SFSUserVariable
newPrivateVariable(java.lang.String name, java.lang.Object value)
protected void
populateArrayWithValue(ISFSArray sfsa)
Populate an SFSArray wih the typed value of this Variablevoid
setHidden(boolean flag)
Set the variable as "hidden".void
setNull()
This is used by the system to mark removed variables.void
setPrivate(boolean value)
Setting a UserVariable as private restricts its visibility to the owner only.void
setStorable(boolean value)
Flags this variable as storable (default = true).protected void
setValue(ISFSArray val)
protected void
setValue(ISFSObject val)
protected void
setValue(java.lang.Boolean val)
protected void
setValue(java.lang.Double val)
protected void
setValue(java.lang.Integer val)
protected void
setValue(java.lang.Object val)
protected void
setValue(java.lang.String val)
protected void
setValueFromStringLiteral(VariableType type, java.lang.String literal)
ISFSArray
toSFSArray()
Privatejava.lang.String
toString()
-
-
-
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:
isHidden
in 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:
setHidden
in interfaceUserVariable
-
isPrivate
public boolean isPrivate()
Checks whether a UserVariable is private. Private UserVariables are only visible to the owner.- Specified by:
isPrivate
in 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:
setPrivate
in 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:
getType
in 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:
getBoolValue
in interfaceVariable
- Returns:
- the value of the variable
-
getDoubleValue
public java.lang.Double getDoubleValue()
Get the value of the variable as Double- Specified by:
getDoubleValue
in interfaceVariable
- Returns:
- the value of the variable
-
getIntValue
public java.lang.Integer getIntValue()
Get the value of the variable as Integer- Specified by:
getIntValue
in interfaceVariable
- Returns:
- the value of the variable
-
getSFSArrayValue
public ISFSArray getSFSArrayValue()
Get the value of the variable as SFSArray- Specified by:
getSFSArrayValue
in interfaceVariable
- Returns:
- the value of the variable
-
getSFSObjectValue
public ISFSObject getSFSObjectValue()
Get the value of the variable as SFSObject- Specified by:
getSFSObjectValue
in interfaceVariable
- Returns:
- the value of the variable
-
getStringValue
public java.lang.String getStringValue()
Get the value of the variable as String- Specified by:
getStringValue
in 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:
setNull
in 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:Variable
Private- Specified by:
toSFSArray
in interfaceVariable
-
isStorable
public boolean isStorable()
Storable variables can be saved by the Room Persistence API- Specified by:
isStorable
in 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:
setStorable
in 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:
clone
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-