Page 1 of 1

user variables

Posted: 13 Oct 2010, 18:13
by appels

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 ?

Posted: 13 Oct 2010, 20:41
by ThomasLund
Got some code on how you set and read the variable?

Posted: 13 Oct 2010, 20:51
by appels
in my OnConnection function i have :

Code: Select all

Hashtable avatarInfo = new Hashtable();
avatarInfo.Add("avatarType", "type1");
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.

Posted: 13 Oct 2010, 21:16
by appels
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.

Posted: 14 Oct 2010, 05:45
by ThomasLund
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.


Posted: 14 Oct 2010, 11:26
by appels
ok, i'll try it out. Thanks :)

Posted: 14 Oct 2010, 12:27
by appels

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 ?

Posted: 14 Oct 2010, 13:33
by ThomasLund
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.


Posted: 14 Oct 2010, 13:53
by appels
awesome, thanks Thomas :)

Posted: 15 Oct 2010, 16:52
by lastowl
would you be able to show this im very interested in how this works out

Posted: 15 Oct 2010, 18:23
by appels
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);

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.

Posted: 15 Oct 2010, 22:16
by appels
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 :
and cast it to a string or anything else if needed :
this will retrieve the Value in the hashtable with key 'myhashkeyname'

hope it helps someone :)