SetUserVariables

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

Moderators: Lapo, Bax

tpelham42
Posts: 9
Joined: 27 Jan 2009, 00:01

SetUserVariables

Postby tpelham42 » 07 Jun 2009, 17:17

Having a bit of an issue setting the user variables for a connected user. Inside an OnJoinedRoom call back I have the below code...

Code: Select all

Debug.Log("[NetMgr] OnRoomJoined");
if(room.GetName() == G_PlayerMgr.instance.GetUsername()){
   Debug.Log("[NetMgr] Joining My Own Room");
   //Set Room Variables
   ArrayList rVars = new ArrayList();
   rVars.Add(new RoomVariable("oid", G_PlayerMgr.instance.playerData.mem_id.ToString(), false, false));
   sfClient.SetRoomVariables(rVars);   

   Hashtable uVars = new Hashtable();         
   uVars.Add("id", G_PlayerMgr.instance.playerData.mem_id.ToString());
   uVars.Add("gen", G_PlayerMgr.instance.playerData.gender);      
         
   sfClient.SetUserVariables(uVars);
         
   Debug.Log("[NetMgr] OnRoomJoined... User Joined Their Own Room. Starting Layout Load...");         
   G_LayoutMgr.instance.loadHome();         
}//if
else{
   //Get The New Rooms Owner ID   
   Debug.Log("[NetMgr] Joining Remote Room: " + room.GetName());         
   Hashtable roomVars = room.GetVariables();
   int owner_id = int.Parse((string)roomVars["oid"]);
   G_LayoutMgr.instance.loadActiveLayout(owner_id);
}//else


The SetUserVariables throws back an acception: " Exception: System.NullReferenceException: Object reference not set to an instance of an object".

I know the variables I'm putting into the hashtable are fine and even tried putting in direct string values and continue to get this error. If I comment out the setuservariables line everything goes through fine with no error. The setRoomVariables above it works fine and does set the room variables. I even tried commenting that out to see if there was in an issue trying to set both variable types in the same block and that didn't make a difference.

I'm using Unity 2.5, SmartFox 1.6.6 and the latest api.
tpelham42
Posts: 9
Joined: 27 Jan 2009, 00:01

Postby tpelham42 » 11 Jun 2009, 21:41

Still no joy on this. I've tried the following

- reverting back to 1.0 and 1.1 of the smartfoxapi for Unity
- Reverting back to 1.6.2 of the Smart Fox Server (which is the last time this worked on an older build of the project)

I have tried a few combination's of the above to no avail. The issue seems to be in the API as I'm not seeing any errors or activity on the server end when I try to SetUserVariables. It states error dispatching event which would indicate it's client side. I have tried cleaning up some other unrelated issues in my code thinking perhaps that was causing some issue, but still nothing. Has anyone else seen this issue before?

Thanks.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 13 Jun 2009, 07:15

Hey - been totally busted with work lately. But will take a look soon!

(Just a quick life sign from me)
tpelham42
Posts: 9
Joined: 27 Jan 2009, 00:01

Postby tpelham42 » 13 Jun 2009, 17:40

Finally managed to solve this issue this morning. The cause was that I was using a custom login and was not manually setting the SmartFox.myUserId value. I had not even thought about my custom login causing issues and was not aware the myUserId value needed to manually set. After reviewing the docs several more times I stumbled upon it and now everything is working as expected. Figured it was something small and sure enough it was a one liner fix. hehe.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 13 Jun 2009, 17:59

Ahhhh :-)

Cool!

Also remember on custom login to send the room list to the API. Also something that is done behind the scenes in the build-in version.

Best
Thomas
tpelham42
Posts: 9
Joined: 27 Jan 2009, 00:01

Postby tpelham42 » 14 Jun 2009, 13:04

Also remember on custom login to send the room list to the API. Also something that is done behind the scenes in the build-in version.

Thanks. Is that done through the GetRoomList method or do I need to expressly pass the room list to the api?
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 14 Jun 2009, 13:23

Here is what I've done on the backend - see at the very last code parts

Code: Select all

   public void handleInternalEvent(InternalEventObject ieo) {
      System.out.println("Internal command recieved " + ieo.getEventName());
      if (ieo.getEventName().equals("loginRequest")) {
         authenticate(ieo.getParam("nick"), ieo.getParam("pass"), (SocketChannel)ieo.getObject("chan"));
      }
   }

   /**
    * Authenticate given credentials
    * @param username The username of the player who wants to login
    * @param password The password given for the username
    * @param channel SocketChannel the given player is connected to, so response can be sent back through that channel
    */
   private void authenticate(String username, String password, SocketChannel channel) {
      ActionscriptObject res = new ActionscriptObject();

      try {
         User user = helper.canLogin(username, password, channel, this.getOwnerZone());
         // The login was successfull
         res.put("_cmd", "logOK");
         res.put("id", String.valueOf(user.getUserId()));
         res.put("name", user.getName());

      } catch (LoginException e) {
         // The server did not allow the user to login
         res.put("_cmd", "logKO");
         res.put("err", e.getMessage());
      }

      // Send response back to calling client
      LinkedList ll = new LinkedList();
      ll.add(channel);
      sendResponse(res, -1, null, ll);

      // Send an update room list to the client to mimick the build in login fundtionality
      if (res.getString("_cmd").equals("logOK")) {
         helper.sendRoomList(channel);
      }
   }
tpelham42
Posts: 9
Joined: 27 Jan 2009, 00:01

Postby tpelham42 » 14 Jun 2009, 13:48

How does this differ from using GetRoomList on the extension response on the client side? Is this a better practice to do it as part of the custom login script? Thanks.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 14 Jun 2009, 14:32

The result is the same if you do it in 2 steps from your game code - or mimic the build in login.

As long as you are aware that you can do almost nothing with the API until the room list is populated - then you are fine.

/Thomas
tpelham42
Posts: 9
Joined: 27 Jan 2009, 00:01

Postby tpelham42 » 14 Jun 2009, 14:57

Great Thanks.

Return to “.Net / Unity3D API”

Who is online

Users browsing this forum: No registered users and 15 guests