This document provides quick snippets of code organized by theme that will get you started with most of the server side coding tasks. Feel free to suggest more "recipes" by sending us an email or posting in our support forums
» User Variables Recipes
User Variables can be accessed from server side to easily propagate User related values across the users in the same rooms. This is typically used to change user state in games. The following recipes show a number of common use cases and examples of usage.
In this recipe we show how to update User Variables upon user request. The user sends a certain amount of points that will be added to a score User Variable, we then fire and update which will update all clients that can interact with this user.
NOTE:
This is not the a recommended way of keeping score even if the variables update is sent from server side. This is just an example focusing on the usage of User Variables, not a tutorial on score keeping.
The setUserVariables() method takes the following parameters:
ExtensionHelper helper;
public void init()
{
helper = ExtensionHelper.instance();
}
public void handleRequest(String cmd, ActionscriptObject asObj, User user, int fromRoom)
{
if (cmd.equals("addScore"))
{
// Get the current score from the user variables
int currentScore = Integer.parseInt(user.getVariable("score").getValue());
// Get the additional points from the user request
int points = (int) asObj.getNumber("points");
// Update variables
Map<String, UserVariable> vars = new HashMap<String, UserVariable>();
vars.put("score", new UserVariable(String.valueOf(currentScore + points), TYPE_NUMBER));
// Set Variables
helper.setUserVariables(u, vars, true);
}
}
One final note on the code above: one User Variables are stored as String values so they need a bit of type conversion when using numbers or booleans.
From version 1.6.5.02 and higher, User and Room Variables can handle types other than String in a more convenient way (see next recipe).
This is the same example as in the previous recipe, using the additional methods introduced in SmartFoxServer PRO 1.6.5.02 and higher.
The following is a list of overridden methods added since that version:
ExtensionHelper helper;
public void init()
{
helper = ExtensionHelper.instance();
}
public void handleRequest(String cmd, ActionscriptObject asObj, User user, int fromRoom)
{
if (cmd.equals("addScore"))
{
// Get the current score from the user variables
int currentScore = user.getVariable("score").getIntValue();
// Get the additional points from the user request
int points = (int) asObj.getNumber("points");
// Update variables
Map<String, UserVariable> vars = new HashMap<String, UserVariable>();
vars.put("score", new UserVariable(currentScore + points);
// Set variables
helper.setUserVariables(u, vars, true);
}
}
One final note on the code above: one User Variables are stored as String values so they need a bit of type conversion when using numbers or booleans.
From version 1.6.5.02 and higher User and Room Variables can handle types other than String in a more convenient way (see next recipe).
In this recipe we show how to create different types of user variables using any version of SmartFoxServer PRO < 1.6.5.02
ExtensionHelper helper;
public void init()
{
helper = ExtensionHelper.instance();
}
public void setSomeVariables(User user)
{
boolean isActive = true;
int theScore = 12345;
double phi = 1.61803399;
String avatarType = "kermitTheFrog";
String extraInfo = null;
// Populate variables
Map<String, UserVariable> vars = new HashMap<String, UserVariable>();
vars.put("isActive", new UserVariable(String.valueOf(isActive), TYPE_BOOLEAN);
vars.put("theScore", new UserVariable(String.valueOf(theScore), TYPE_NUMBER);
vars.put("phi", new UserVariable(String.valueOf(theScore), TYPE_NUMBER);
vars.put("avatarType", new UserVariable(avatarType, TYPE_STRING);
vars.put("extraInfo", new UserVariable(extraInfo, TYPE_STRING);
// Set variables
helper.setUserVariables(user, vars, true);
}
In this recipe we repeat the same example of Recipe #3, using the updated classes found in SmartFoxServer PRO 1.6.5.02 and higher.
ExtensionHelper helper;
public void init()
{
helper = ExtensionHelper.instance();
}
public void setSomeVariables(User user)
{
boolean isActive = true;
int theScore = 12345;
double phi = 1.61803399;
String avatarType = "kermitTheFrog";
String extraInfo = null;
// Populate variables
Map<String, UserVariable> vars = new HashMap<String, UserVariable>();
vars.put("isActive", new UserVariable(isActive));
vars.put("theScore", new UserVariable(theScore));
vars.put("phi", new UserVariable(phi));
vars.put("avatarType", new UserVariable(avatarType));
vars.put("extraInfo", new UserVariable(extraInfo));
// Set variables
helper.setUserVariables(user, vars, true);
}
| doc index |