Setting and getting userVariables problem!

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

fonager
Posts: 9
Joined: 15 Apr 2005, 10:52
Location: Denmark

Setting and getting userVariables problem!

Postby fonager » 15 Apr 2005, 11:00

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;
vars.webcam = false;
smartFox.setUserVariables(vars);


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
User avatar
Lapo
Site Admin
Posts: 21139
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 15 Apr 2005, 15:06

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 :)
Lapo
--
gotoAndPlay()
...addicted to flash games
fonager
Posts: 9
Joined: 15 Apr 2005, 10:52
Location: Denmark

Postby fonager » 16 Apr 2005, 07:31

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 ?
User avatar
Lapo
Site Admin
Posts: 21139
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 16 Apr 2005, 08:22

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?

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
fonager
Posts: 9
Joined: 15 Apr 2005, 10:52
Location: Denmark

Postby fonager » 16 Apr 2005, 10:35

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 ?
Friendly greetings,

Tony Fonager
Copenhagen, Denmark
User avatar
Lapo
Site Admin
Posts: 21139
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 16 Apr 2005, 13:10

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")
   
   updateRoomStatus(fromRoom)
   
   // Show the user avatar
   var mc:MovieClip = avatarMC.attachMovie("avatar", "avatar_" + userId, userId)

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

   mc.disc.gotoAndStop(user.variables["col"])
}


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

1)

Code: Select all

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


2)

Code: Select all

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


Hope it helps! :)
Lapo

--

gotoAndPlay()

...addicted to flash games
fonager
Posts: 9
Joined: 15 Apr 2005, 10:52
Location: Denmark

Postby fonager » 16 Apr 2005, 16:24

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 ?
Friendly greetings,



Tony Fonager

Copenhagen, Denmark
User avatar
Lapo
Site Admin
Posts: 21139
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 16 Apr 2005, 16:35

As soon as you enter a room you can create all your variables. That's a clean way to do it.
Lapo

--

gotoAndPlay()

...addicted to flash games
fonager
Posts: 9
Joined: 15 Apr 2005, 10:52
Location: Denmark

Postby fonager » 16 Apr 2005, 16:37

Thanks, I will try playing with that right away - cannot wait to play around some more with your nice product ;-)
Friendly greetings,



Tony Fonager

Copenhagen, Denmark

Return to “SmartFoxServer 1.x Discussions and Help”

Who is online

Users browsing this forum: No registered users and 4 guests