Forum C# Nicknames

Nicknames

Postby Ultric » August 28th, 2010, 7:22 pm

I'm not good at working with server and client communications so I have no idea what I'm doing half of the time.I'm using the Chat.as in the sample games as a base for my own chat engine. The problem is, I can't seem to get any sort of nickname (located in BigDB) to replace the "simple<emailhere>" thing. I'm using QuickConnect and BigDB. My AS code is pretty much the same as the Chat code (except I moved the components around for visual appeal). Here's my C#:
Code: Select all
public override void UserJoined(Player player) {
         // this is how you send a player a message
            //Send info about all already connected users to the newly joined users chat
            Message m = Message.Create("ChatInit");
            //BigDB.Load("PlayerObjects",player.Id,);

            m.Add(player.Id);

            foreach (Player p in Players)
            {
                m.Add(p.Id, p.ConnectUserId);
            }

            player.Send(m);

            //Informs other users chats that a new user just joined.
            Broadcast("ChatJoin", player.Id, player.ConnectUserId);
      }

I tried to use the .PlayerObject.GetString(<valuehere>) function but I don't think it work. Any help would be appreciated.
Ultric
 
Posts: 4
Joined: August 25th, 2010, 12:24 am

Re: Nicknames

Postby Oliver » August 30th, 2010, 11:55 am

Hey,

We're here to help.

If you look in the ActionScript Chat.as file, you'll find this snippet:

Code: Select all
_connection.addMessageHandler("ChatJoin", function(m:Message, id:String, name:String){
   addUser(id,name)
})


Ie, when the client receives a "ChatJoin" message, it will take the assign the name (2nd argument) to the id of the 1st argument.

There is also the message sent with the complete userlist:

Code: Select all
_connection.addMessageHandler("ChatInit", onInit)
...
private function onInit(m:Message, id:String){
   for( var a:int=1;a<m.length;a+=2){
      addUser(m.getString(a),m.getString(a+1))
   }
}


So you have to change these two lines in the serverside code, if you want to assign another name to a user:

Code: Select all
...
m.Add(p.Id, p.ConnectUserId); <-- change p.ConnectUserId here
....
Broadcast("ChatJoin", player.Id, player.ConnectUserId); <-- change player.ConnectUserId


If the name is stored in the BigDB PlayerObject for each player, you can easily accomplish this by

a) Set "PreloadPlayerObjects = true" in the GameStarted method and
b) Change the lines to
Code: Select all
...
m.Add(p.Id, p.PlayerObject.GetString("username"));
....
Broadcast("ChatJoin", player.Id, player.PlayerObject.GetString("username"));


Hope this helps.

Best,
Oliver
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: Nicknames

Postby Ultric » August 30th, 2010, 5:52 pm

Ah, that was my problem. I forgot to preload the playerobjects. Thanks for the help!
Ultric
 
Posts: 4
Joined: August 25th, 2010, 12:24 am


Return to C#