Click or drag to resize

SetBuddyVariablesRequest Class

Sets one or more Buddy Variables for the current user.
Inheritance Hierarchy
SystemObject
  BaseRequest
    Sfs2X.Requests.BuddylistSetBuddyVariablesRequest

Namespace:  Sfs2X.Requests.Buddylist
Assembly:  SmartFox2X (in SmartFox2X.dll) Version: 1.8.0.0 (1.8.0)
Syntax
C#
public class SetBuddyVariablesRequest : BaseRequest

The SetBuddyVariablesRequest type exposes the following members.

Constructors
  NameDescription
Public methodSetBuddyVariablesRequest
Creates a new SetBuddyVariablesRequest instance.
Top
Remarks
This operation updates the Buddy object representing the user in all the buddies lists in which the user was added as a buddy. If the operation is successful, a BUDDY_VARIABLES_UPDATE event is dispatched to all the owners of those buddies 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.

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

Examples
The following example sets some Buddy Variables for the current user, one of which is persistent; the example also handles changes made by the user or by his buddies:
void SomeMethod() {
    // Add event listener for BuddyVariables
    sfs.AddEventListener(SFSBuddyEvent.BUDDY_VARIABLES_UPDATE, OnBuddyVarsUpdate);

    // Create two Buddy Variables containing the title and artist of the song I'm listening to
    BuddyVariable songTitle = new SFSBuddyVariable("songTitle", "Ascension");
    BuddyVariable songAuthor = new SFSBuddyVariable("songAuthor", "Mike Oldfield");

    // Create a persistent Buddy Variable containing my mood message
    BuddyVariable mood = new SFSBuddyVariable(SFSBuddyVariable.OFFLINE_PREFIX + "mood", "I Need SmartFoxServer 2X desperately!");

    // Set my Buddy Variables
    List<BuddyVariable> myVars = new List<BuddyVariable>();
    myVars.Add(songTitle);
    myVars.Add(songAuthor);
    myVars.Add(mood);
    sfs.Send(new SetBuddyVariablesRequest(myVars));
}

void OnBuddyVarsUpdate(BaseEvent evt) {
    // As the update event is dispatched to me too,
    // I have to check if I am the one who changed his Buddy Variables

    Buddy buddy = (Buddy)evt.Params["buddy"]);
    bool isItMe = (bool)evt.Params["isItMe"];
    List<string> changedVars = (List<string>)evt.Params["changedVars"];

    if (isItMe)
    {
        Console.WriteLine("I've updated the following Buddy Variables:");                       // .Net / Unity
        System.Diagnostics.Debug.WriteLine("I've updated the following Buddy Variables:");      // UWP

        for (int i = 0; i < changedVars.Count; i++)
        {
            string bVarName = changedVars[i];
            Console.WriteLine(bVarName + ": " + sfs.BuddyManager.GetMyVariable(bVarName).Value());                      // .Net / Unity
            System.Diagnostics.Debug.WriteLine(bVarName + ": " + sfs.BuddyManager.GetMyVariable(bVarName).Value());     // UWP
        }
    }
    else
    {
        string buddyName = buddy.Name;

        Console.WriteLine("My buddy " + buddyName + " updated the following Buddy Variables:");                     // .Net / Unity
        System.Diagnostics.Debug.WriteLine("My buddy " + buddyName + " updated the following Buddy Variables:");    // UWP

        for (int i = 0; i < changedVars.Count; i++)
        {
            var bVarName:String = changedVars[i];
            Console.WriteLine(bVarName + ": " + sfs.BuddyManager.GetBuddyByName(buddyName).GetVariable(bVarName).Value());                      // .Net / Unity
            System.Diagnostics.Debug.WriteLine(bVarName + ": " + sfs.BuddyManager.GetBuddyByName(buddyName).GetVariable(bVarName).Value());     // UWP
        }
    }
}
See Also