How to use user variables

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

COB
Posts: 68
Joined: 28 Dec 2010, 08:54

How to use user variables

Postby COB » 05 Apr 2011, 06:32

I'm trying to use user variables, but without success at the moment.
I have this code to subscribe:

Code: Select all

smartFoxClient.AddEventListener(SFSEvent.USER_VARIABLES_UPDATE, OnUserVarsUpdate);

then this to set some values:

Code: Select all

List<UserVariable> userVars = new List<UserVariable>();
userVars.Add( new SFSUserVariable("avatarType", "SwedishCook") );
userVars.Add( new SFSUserVariable("country", "Sweden") );
smartFoxClient.Send ( new SetUserVariablesRequest(userVars) );

and at last this to receive them:

Code: Select all

private void OnUserVarsUpdate(BaseEvent evt)
     {
        List<UserVariable> changedVars = (List<UserVariable>)evt.Params["changedVars"];
        User user = (User)evt.Params["user"];
       Debug.Log("Received");

     }

Unfortunately I don't see debug log in the console, so it seems that they are not received. Can you tell me what I'm doing wrong? Or maybe I have to configure something on the server.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 05 Apr 2011, 07:22

My first hunch would be, that you have run into same documentation bug as someone else recently.

viewtopic.php?t=10624

The changedVars contains a List<String> with the names of changed variables. Not the variabled themselves.

Hope that help

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 05 Apr 2011, 09:08

Thank you. Without this code I can see debug message, so it's not bad.

Code: Select all

List<UserVariable> changedVars = (List<UserVariable>)evt.Params["changedVars"];
        User user = (User)evt.Params["user"];

However, will I be able only to receive list of changed variables and not values itself? If so, it's not a good news.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 05 Apr 2011, 10:17

Once you got the changed variable name, you can simply look it up in the API to get it.

Done this way (I guess) to reduce overhead of sending all the changed variables to the client code. Its not always you want to do something with the variables, and this way you can say "if variable name == posX or posY - then do something. If its == my personal info, then ignore"

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 05 Apr 2011, 10:46

It's a good approach, but still I don't know how to get these variables. I can't see dedicated request in requests namespace.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 05 Apr 2011, 12:48

Ahhh - its not a request!

What happens is, that when the response comes from the server, then the user or the room instance has its variables updated inside the API. And the names of the changed variables are then sent to the callback.

So you basically query

UserVariable var = user.GetVariable("<<insert name here>>");

same for the room if its a room variable thats updated

/Thomas
Full Control - maker of Unity/C# and Java SFS API and indie games

Follow on twitter: http://twitter.com/thomas_h_lund
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 06 Apr 2011, 07:41

Now, it's working. The proper code to get updated variables is:

Code: Select all

private void OnUserVarsUpdate(BaseEvent evt) {
        ArrayList changedVars = (ArrayList)evt.Params["changedVars"];
      foreach (string chv in changedVars) {
         Debug.Log(chv);
      }
   }
User avatar
Whiskey
Posts: 45
Joined: 28 Oct 2010, 06:27
Location: The Netherlands
Contact:

Postby Whiskey » 07 May 2011, 12:03

Thanks for the information in this topic. It got me to understand uservars as well. For completeness sake and lazy people like me :wink: , below the code that works with a small addition so that the value of the variable itself is also listed.

Code: Select all

private void OnUserVarsUpdate(BaseEvent evt) {
   ArrayList changedVars = (ArrayList)evt.Params["changedVars"];
   User user = (User)evt.Params["user"];
     foreach (string chv in changedVars) {
         Debug.Log("Uservars update for user " + user.Name +
         " -> " + chv + ": " +  user.GetVariable(chv).Value);
   }
}


It is also often useful to check first if the variable you want to query actually exists. You can do that with:

Code: Select all

if (user.ContainsVariable("variable_name") ) {
      // do something with user.GetVariable("variable_name")
}
tpenn
Posts: 95
Joined: 03 Aug 2010, 18:48

Postby tpenn » 09 May 2011, 16:14

If you ever remove variables, it is very important that you check that the variable exists. The same event is used to communicate removal of a UserVariable. If you get the name of a given variable in the list of changed vars but it doesn't exist, that's how you know it was removed.

RoomVariables work the same way.
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 16 May 2011, 13:09

I thought that I already know how to use user variables, but unfortunately I have some problems.
I use this code to set variables:

Code: Select all

         List<UserVariable> userVars = new List<UserVariable>();
         userVars.Add( new SFSUserVariable("email1", email1) );
         userVars.Add( new SFSUserVariable("email2", email2) );
         userVars.Add( new SFSUserVariable("tel1", tel1) );
         userVars.Add( new SFSUserVariable("tel2", tel2) );
         smartFoxClient.Send ( new SetUserVariablesRequest(userVars) );

I see on server side that they have been set. Then I try to read them with this code attached to some button:

Code: Select all

         Debug.Log("------*----");
         Debug.Log(smartFox.MySelf.GetVariables());
         foreach(UserVariable u in smartFox.MySelf.GetVariables()) {
            Debug.Log(u);
         }
         Debug.Log("------*----");

But the list is empty because I can't see any in the console. Moreover, I get null when I try to read them directly:

Code: Select all

Debug.Log(smartFoxClient.MySelf.GetVariable("email1"));

What I am doing wrong?
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 16 May 2011, 15:25

After some additional tests I noticed that user variables work well in WebPlayer (the same code as in Unity Editor). However, still I am not able to read "my own" variables. Only ones from other users. In Editor with the same code I can't see both "mine" and other users variables.
I have a feeling that this is a bug. My last tests were done on RC1b and now I have RC2a. Can someone check this? It's very important for me because tomorrow we have a presentation of our application and now I have this problem with implementation of a new feature that has to work tomorrow.
User avatar
Whiskey
Posts: 45
Joined: 28 Oct 2010, 06:27
Location: The Netherlands
Contact:

Postby Whiskey » 16 May 2011, 18:08

I tried your retrieval code with some variables I had already set myself and it seems to be working for me. However, I notice that you are not using the same smartfox client object in the different pieces of code that you list. You are using "smartFoxClient.Send" and "smartFoxClient.MySelf" and "smartFox.MySelf" for retrieval. Maybe that is part of your problem?

One strange thing though: at first your code did not seem to be working, but while I was tinkering, it suddenly worked and it turned out the original code worked as well. I haven't been able to reproduce, I only know I tried to get the variable by name with GetVariable which worked. That this did not work for you might be related to the different smart fox objects you're using.

Sorry I can't be more specific. Good luck.

edit: I'm also using RC1b.
COB
Posts: 68
Joined: 28 Dec 2010, 08:54

Postby COB » 16 May 2011, 19:00

This code is taken from different scripts and indeed, there is some inconsistency in naming of variable that represents smartfox connection.

As you said code seemed not to work at the beginning and I am observing now probably similar thing. I use Unity editor (lets name it User A) and WebPlayer (User B) for tests. When I run firstly run WebPlayer and then Unity editor I am able to read User B variables from Unity editor (User A), but I am not able to read User A variables from WebPlayer (User B). When I run firstly run in editor and then WebPlayer situation opposite. In both cases I am not able to read User A variables in Unity editor and User B variables in WebPlayer. Also in both cases variables are visible in monitor in admin panel.

Maybe these problems concern only RC2a?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 18 May 2011, 12:03

How the code works here is not really clear.
Couple of quick tips:
1- for two users to see their respective variables they must be in the same Room

2- If you set user variables with the regular SetUserVariablesRequest or it's server side equivalent you must wait for the asynchronous USER_VARIABLES_UPDATE event to fire.

We are not really aware of any basic problems with UserVars such as this in RC2a. We have our own OpenSpace2 product which does heavy use of User Variables and work consistently.
Lapo
--
gotoAndPlay()
...addicted to flash games

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 36 guests