user variables

Post here all your questions related with SmartFoxServer .Net/Unity3D API

Moderators: Lapo, Bax

appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

user variables

Postby appels » 13 Oct 2010, 18:13

Hi,

I'm trying to set user variables for my players at login and retrieve them when a player gets spawned. I get a 'null' value back.
Anyone have ideas ?
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 13 Oct 2010, 20:41

Got some code on how you set and read the variable?
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 13 Oct 2010, 20:51

in my OnConnection function i have :

Code: Select all

Hashtable avatarInfo = new Hashtable();
avatarInfo.Add("avatarType", "type1");
smartFox.SetUserVariables(avatarInfo);
SmartFox.Connection = smartFox;


in my UserEnterRoom function :

Code: Select all

Hashtable uVars = new Hashtable();
uVars = user.GetVariables();
foreach (DictionaryEntry entry in uVars)
   Debug.Log("enter room : " + entry.Key + " " + entry.Value);


when i debug user.GetVariables() i get a hash back but nothing shows up in the logs.
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 13 Oct 2010, 21:16

i just found out that if i send the variables to the server before i joined the room, i don't get them back.
Is that the way to handle it?
When i send them after joining a room, i do get them back.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 14 Oct 2010, 05:45

You usually have to be in a room (as well as have the room list) for anything to work. So best bet it to auto join into a "spawn room" on login, and then handle your player spawn there by setting the user variable.

/Thomas
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 14 Oct 2010, 11:26

ok, i'll try it out. Thanks :)
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 14 Oct 2010, 12:27

Thomas,

How would i change a remote player's avatar when it spawns ?
What i'm trying ( but not succeeding :( ) is a user selects his avatar when he is logged on, then gets spawned in the room.
It's easy for the localplayerobject... in my localplayer spawn function i check what is selected and spawn the resulting avatar but how would i handle the remotes ?
I thought uservars would be the best way to go but maybe you have a better way of handling this ?
Eddy.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 14 Oct 2010, 13:33

User variables are definitely a great way to do this.

When you first log in, you join some kind of room (on the server). Once you join that room (just on the server - not visually on the client side) - you show the selection screen of avatars to pick and THEN you spawn yourself into the game room with the right avatar.

Thats at least how I would (and have) done it.

/Thomas
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 14 Oct 2010, 13:53

awesome, thanks Thomas :)
lastowl
Posts: 36
Joined: 12 Aug 2010, 20:56

Postby lastowl » 15 Oct 2010, 16:52

would you be able to show this im very interested in how this works out
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 15 Oct 2010, 18:23

actualy i'm able to set the vars from the client but reading them back out is somthing i havent figured out yet :

1. the player needs to be in a room to be able to set them.
2. once your in a room you can create a hashtable and send it:

Code: Select all

string male = "male";
Hashtable avatarInfo = new Hashtable();
avatarInfo.Add("avatarType", male);
smartFoxClient.SetUserVariables(avatarInfo);


3. once you have sent this to the server, it will send it to all other players in the room so you need to handle the local player yourself.
4. you can retrieve them with :

Code: Select all

Hashtable aTypes = new Hashtable();
Hashtable uVars = new Hashtable();
uVars = user.GetVariables();
aTypes.Add(user.GetId(), uVars["avatarType"]);


this all works fine.
I have a problem with getting the value from the last created hashtable :

Code: Select all

int remoteId = user.GetId();
if(aTypes[remoteId].ToString() == "male") {
//process code


I tried converting the object from the hash value to a string to be able to compare it, but that doens't work.
Eddy.
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 15 Oct 2010, 22:16

ok, it's working now like it should :)

all you need to do is set the user vars ( send them to the server ) and to retrieve them just call GetVariable on a user object :
user.GetVariable("myhashkeyname");
and cast it to a string or anything else if needed :
user.GetVariable("myhashkeyname").ToString();
this will retrieve the Value in the hashtable with key 'myhashkeyname'

hope it helps someone :)

Return to “.Net / Unity3D API”

Who is online

Users browsing this forum: No registered users and 2 guests