Page 1 of 1

Setting and getting userVariables problem!

Posted: 15 Apr 2005, 11:00
by fonager
Hi everyone!

I have a problem attaching uservariables to my users - right now I am working on a "1 on 1" chat, where 2 people can chat with eachother and use webcam.

I would use the uservaribles for each user, to contain their profile name, information about webcam (is it ON / OFF)

So I use this for setting my userVariables - I am setting these variables in the "onJoinRoom" event, as that is when I have entered my destination room :

Code: Select all

vars = new Object()
vars.screenname = screenname; = false;

I get no error from SmartFoxServer, so I assume everything went fine when setting the variables.

Later on, in the "onUserEnterRoom" event, I want to display information about the users who enters the room (my opponent chatter in this case), so I access his users variables like this :

Code: Select all

screenname = userObj.getVariable("screenname");

But it only returns "undefined" - and I cannot seem to see what I am doing wrong ...

Can anyone help me ?

Regards from Copenhagen,

Tony Fonager

Posted: 15 Apr 2005, 15:06
by Lapo
Hi fonager,
from the code you have posted everything seems ok.

Could you post some more code so that I can see it in context? That would help in better understanding what is going on...

Thanks :)

Posted: 16 Apr 2005, 07:31
by fonager
When I access the uservariables using "getVariable()" in the "onUserVariablesUpdate" event, I have no problems accessing the variables for the other user.

So I am sure my code is correct - but it seems like I cannot access the uservariables in the "onJoinRoom" event, using the "User" object that is sent into this event ...

I find it hard to believe that there is a difference in WHICH event I access the variables from ?

Or is there ?

Posted: 16 Apr 2005, 08:22
by Lapo
humm... the "onJoinRoom" is fired when you enter a new room
I don't understand why you should check User Variables in that point and also the object passed to the event is a Room object. You may read Room Variables when you enter a new room

I find it hard to believe that there is a difference in WHICH event I access the variables from ?

Yes, in fact there's no difference

Could you please explain your code a little bit better and post the relevant parts here?


Posted: 16 Apr 2005, 10:35
by fonager
Sorry, I wrote the wrong eventname - it should offcourse be the "onUserEnterRoom()" event.

When another user comes into my room, I wanna read his/her uservariables, so I can see their full name, webcam capailities, age and such stuff, which is set on each user using the "setUserVariables()" event.

But as I wrote earlier, I could not get these variables from a user, in the "onUserEnterRoom()" event, event though that event passes a "User" object to me.

When would be the perfect time to set a users variables, when a new user is coming into the chat ?

- when he just connected to the SmartFox server ?
- or first after he has joined a room ?

Now I have solved the problem, by entering a room, and when I have entered the room, I set my own userVariables, thereby sending them to all other users in the room.

I guess that might be the correct way to do it ?

Posted: 16 Apr 2005, 13:10
by Lapo
No problem.
I've found an example that will surely help you. In the "Avatar Chat" example provided in the SFS Basic package you have the same exact type of code.

When a user enters a new room the onUserEnterRoom event is fired an the client reads the User Vars of the new user and shows its avatar in the right place.

Here's the code that does this:

Code: Select all

smartfox.onUserEnterRoom = function(fromRoom, user)
   var userId:Number   = user.getId()
   var userName:String   = user.getName()
   // Add user to the userList listbox
   userList_lb.addItem(userName, userId)
   // Sort names
   userList_lb.sortItemsBy("label", "ASC")
   // Show the user avatar
   var mc:MovieClip = avatarMC.attachMovie("avatar", "avatar_" + userId, userId)

   mc._x          = user.variables["px"]
   mc._y          = user.variables["py"]    = userName


As you can see User Variables are accessed directly using the "variables" property, but there are also 2 other ways to do it:


Code: Select all

var varObj:Object = user.getVariables()
mc._x = varObj["px"]
mc._y = varObj["py"]


Code: Select all

mc._x = user.getVariable("px")
mc._y = user.getVariable("py")

Hope it helps! :)

Posted: 16 Apr 2005, 16:24
by fonager
Great, that gives me a few examples to play with - thanks!!!

But yet, you need to tell me WHEN to give my local user his own variables (name, age, gender, webcam ect.).

It is after I have connected to the SFS server or after I have joined a room/game ?

Posted: 16 Apr 2005, 16:35
by Lapo
As soon as you enter a room you can create all your variables. That's a clean way to do it.

Posted: 16 Apr 2005, 16:37
by fonager
Thanks, I will try playing with that right away - cannot wait to play around some more with your nice product ;-)