Class SFS2X.Requests.BuddyList.SetBuddyVariablesRequest

Sets one or more Buddy Variables for the current user.

Class Summary
Constructor Attributes Constructor Name and Description
 
Creates a new SetBuddyVariablesRequest instance.

Class Detail

SFS2X.Requests.BuddyList.SetBuddyVariablesRequest(buddyVariables)
Creates a new SetBuddyVariablesRequest instance. The instance must be passed to the SmartFox.send() method for the request to be performed.

This operation updates the SFSBuddy object representing the user in all the buddy lists in which the user was added as a buddy. If the operation is successful, a buddyVariablesUpdate event is dispatched to all the owners of those buddy lists and to the user who updated his variables too.

The Buddy Variables can be persisted, which means that their value will be saved even it the user disconnects and it will be restored when he connects again. In order to make a variable persistent, put the constant SFSBuddyVariable.OFFLINE_PREFIX before its name. Read the SmartFoxServer 2X documentaion about the Buddy List API for more informations.

NOTE: this request can be sent if the Buddy List system was previously initialized only (see the InitBuddyListRequest request description) and the current user state in the system is "online".

The following example sets some Buddy Variables for the current user, one of which is persistent and another one is the reserved variable used to set the nickname; the example also handles changes made by the user or by his buddies:

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSBuddyEvent.BUDDY_VARIABLES_UPDATE, onBuddyVarsUpdate, this);
	
	// Create two Buddy Variables containing the title and artist of the song I'm listening to
	var songTitle = new SFS2X.Entities.Variables.SFSBuddyVariable("songTitle", "Ascension");
	var songAuthor = new SFS2X.Entities.Variables.SFSBuddyVariable("songAuthor", "Mike Oldfield");
	
	// Create a persistent Buddy Variable containing my mood message
	var mood = new SFS2X.Entities.Variables.SFSBuddyVariable(SFS2X.Entities.Variables.SFSBuddyVariable.OFFLINE_PREFIX + "mood", "SFS2X rocks!");
	
	// Set my nickname
	var nick = new SFS2X.Entities.Variables.SFSBuddyVariable(SFS2X.Entities.Variables.ReservedBuddyVariables.BV_NICKNAME, "Bax");
	
	// Set my Buddy Variables
	var vars = [songTitle, songAuthor, mood, nick];
	sfs.send(new SFS2X.Requests.BuddyList.SetBuddyVariablesRequest(vars));
}

function onBuddyVarsUpdate(evtParams)
{
	// As the update event is dispatched to me too,
	// I have to check if I am the one who changed his Buddy Variables
	
	var isItMe = evtParams.isItMe;
	
	if (isItMe)
	{
		console.log("I've updated the following Buddy Variables:");
		
		for (var i = 0; i < evtParams.changedVars.length; i++)
		{
			var bVarName = evtParams.changedVars[i];
			console.log(bVarName + " -->; " + sfs.buddyManager.getMyVariable(bVarName).value);
		}
	}
	else
	{
		var buddyName = evtParams.buddy.name;
		
		console.log("My buddy " + buddyName + " updated the following Buddy Variables:");
		
		for (var i = 0; i < evtParams.changedVars.length; i++)
		{
			var bVarName = evtParams.changedVars[i];
			console.log(bVarName + " --> " + sfs.buddyManager.getBuddyByName(buddyName).getVariable(bVarName).value);
		}
	}
}
Parameters:
{Array} buddyVariables
A list of SFSBuddyVariable objects representing the Buddy Variables to set.
See also:
SFS2X.SmartFox#send
SFS2X.Entities.Variables.SFSBuddyVariable
SFS2X.Entities.Variables.ReservedBuddyVariables
SFS2X.SFSBuddyEvent.BUDDY_VARIABLES_UPDATE