SFSclient.SetUserVariables --> NullReferenceException ?

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

Moderators: Lapo, Bax

fernando24691
Posts: 30
Joined: 20 Oct 2010, 19:37

SFSclient.SetUserVariables --> NullReferenceException ?

Postby fernando24691 » 26 Oct 2010, 15:04

Hello :).

I have a little problem with SetUserVariables method in unity~C#. The only thing I tried to do was changing the value of my user variables in the server. As simple as that.

Server-side (extension):
Once an user has loged in successfully, I declare his user variables. I initialise both to zero and I let them be for a later client manipulation.

Client-side:
I handle login responses well. After loging, I load the level corresponding to the room where my player is in the server. I have done a functionally public chat, etc, and now I wanted to move my player around, so that's the reason why I wanted to use "SetUserVariables".

Here is the code in the Update() loop:

Code: Select all

void Update(){
...
Hashtable UV = new Hashtable();
UV.Add("x", 1);
UV.Add("y", 1);   
smartFox.SetUserVariables(UV);
...
}


In the beginning, there were some transform manipulations, but I started to erase code in order to see what was wrong, and now I have only that 4 sentences.

User variables are initialised both to zero when I create them just after loging (in server-side) so, I only expected to see the change from 0 to 1. The name of the variables is the same as in the server, and they are numeric type.

But then :), the error comes from debug message: "NullReferenceException: Object reference not set to an instance of an object".

I copied the sentences above from the c# api reference. I have also move it to diferent parts of the code and even to diferent working scripts, with the same results everywhere. I'm sure smartfox client is running, because I can chat, etc.

I have no more ideas to test, so you guys are the only answer! :D
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 26 Oct 2010, 15:49

Can you paste the entire stacktrace for the null reference? And if it points to a certain part of your code, please also the code from that part with a clear indication of where the NRE happens.

Thanks!

/Thomas
fernando24691
Posts: 30
Joined: 20 Oct 2010, 19:37

Postby fernando24691 » 26 Oct 2010, 16:00

This is the whole error code:

NullReferenceException: Object reference not set to an instance of an object
SmartFoxClientAPI.SmartFoxClient.SetUserVariables (System.Collections.Hashtable varObj, Int32 roomId)
SmartFoxClientAPI.SmartFoxClient.SetUserVariables (System.Collections.Hashtable varObj)
NetHandler.Update () (at Assets/Scenes/Work Entrance/SCripts/NetHandler.cs:114)


As I said, placed the same code part in many different scripts and the error is the same. Anyway, here is the update loop:

Code: Select all

void Update () {
      //// Esto solo se utiliza al cargar el nivel. Ignorarlo para el resto del código.
      if (!loading && BgMat.color.a > 0) BgMat.color -= new Color(0,0,0,0.7F)*Time.deltaTime;
      else if (!loading)  {
         renderer.enabled = false;
         interactive = true;
      }
      if(!interactive) return;
      ///// Código del ChatBox
      if(Maximized) {
         if(Input.GetMouseButtonDown(0) && new Rect(285,105,15,15).Contains(Input.mousePosition)) Maximized = false;
         if(Input.GetMouseButtonDown(0) && new Rect(270,105,10,10).Contains(Input.mousePosition)) {
            SChat = "";
            HChat = 0;   
         }
      }
      else if(Input.GetMouseButtonUp(0) && new Rect(290,28,15,15).Contains(Input.mousePosition)) Maximized = true;
      if(Input.GetKeyUp(KeyCode.Return) && !focusChat) focusChat = true;
      //// Codigo de los bubbles
      for (int i = 0; i < BList.Count; i++) {
         bubble B = BList[i];
         B.rtime -= Time.deltaTime;
         BList[i] = B;
      }
      BList.RemoveAll(tElapsed);
      
      /// Movimiento del jugador
      Hashtable uVars = new Hashtable();
      uVars.Add("x", 1);
      uVars.Add("y", 1);
      smartFox.SetUserVariables(uVars); <-- ERROR LINE
   }


Thank you so much! :)
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 27 Oct 2010, 05:49

Sorry - cannot reproduce.

I took tutorial 3, and in the Lobby.cs did this quick hack:

Code: Select all

   void OnPublicMessage(string message, User sender, int roomId) {
      chatWindow.AddChatMessage(sender.GetName() + " said " + message);
      
      Hashtable UV = new Hashtable();
      UV.Add("x", 1);
      UV.Add("y", 1);   
      smartFox.SetUserVariables(UV);
   }


No errors, and the user variables are being send nicely to the server:

Code: Select all

[SFS DEBUG] [Sending]: <msg t='sys'><body action='setUvars' r='8'><vars><var n='y' t='n'><![CDATA[1]]></var><var n='x' t='n'><![CDATA[1]]></var></vars></body></msg>

UnityEngine.Debug:Internal_Log(Int32, String, Object)
UnityEngine.Debug:Log(Object)
LobbyGUI:OnDebugMessage(String) (at Assets/Game/Scripts/LobbyGUI.cs:195)
SmartFoxClientAPI.SmartFoxClient:_DispatchEvent(SFSEvent)
SmartFoxClientAPI.SmartFoxClient:ProcessEventQueue()
LobbyGUI:FixedUpdate() (at Assets/Game/Scripts/LobbyGUI.cs:35)
 
(Filename: Assets/Game/Scripts/LobbyGUI.cs Line: 195)
fernando24691
Posts: 30
Joined: 20 Oct 2010, 19:37

Postby fernando24691 » 27 Oct 2010, 10:11

Hi again!

Thank you for your observation. Despite I couldn't fix the error, I found it very interesting: the way smartfox client is stored in a static variable aware of any level. I wasn't using that "strategy", but declaring smartfox client as static in a script attached to a scene.

I don't know if that matters in my problem, but actually I copied that "strategy" directly from the example provived. I have found more errors in my code and I'm fixing them, so the one I was asking for can wait :).

I have another error, this one is more simple.

If I use smartFox.SendPublicMessage() to the room where I am, nothing happens. Not even the debug messages tell me anything. No debug errors, no messages. I thought I lost smartfox connection, but smartFox.IsConnected() is true just before sending the public message. I also checked that smartFox.activeRoomId has some value stored (the correct).

The funny thing is that smartFox.SendPublicMessage() worked well when I was using the "old" strategy for declaring smartfox static variable. Actually the "public chat" was the only thing I have managed to finish, so that's quite disgusting for me :).

PD: I asked for that "strategies" some days ago here:
[url]
viewtopic.php?t=8783[/url]

Any idea why this is not working? Thank you! :D
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 27 Oct 2010, 12:09

Good question why that doesnt work for you. I would fix all the known issues first in your code and then retest.

For how to preserve the connection between levels - well - as I responded, I definitely would do it as I did in the examples. It works, its been tested and other people are doing that too. Static variables in a class that gets destroyed by Unity on scene load - sounds not like a very good strategy

/Thomas
fernando24691
Posts: 30
Joined: 20 Oct 2010, 19:37

Postby fernando24691 » 27 Oct 2010, 16:39

Thank you for your patience. I'll try to close this post with a solution these days. By the way, I think there is some problem registering the callbacks.
fernando24691
Posts: 30
Joined: 20 Oct 2010, 19:37

Postby fernando24691 » 28 Oct 2010, 13:02

I reinstalled Unity3 and re-tested everything, and now everything works fine, I have my chat working with the new static class setup. Everything is fine but the setUserVariables() :?

I'm getting a little tired of that problem, and I was wondering if overriding that method with a custom extension would be possible. I mean, passing the new values to the server through XTMessage, and the server will update the user variables for the client.

There would be any problem with this idea? :)

Thank you!
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 28 Oct 2010, 14:04

Nothing wrong with that idea - but the setUserVariables should work too. Are you setting these maybe before you have joined a room?

/Thomas
fernando24691
Posts: 30
Joined: 20 Oct 2010, 19:37

Postby fernando24691 » 28 Oct 2010, 14:30

I am using the same distribution as in the examples.

I coded this "game cycle":

Check SmartfoxClient static (from the outside class) --> (if is null) create a new one (else) proceed --> Register Callbacks ---> Custom Login ---> (if not success) try again! (else) GetRoomList ----> JoinRoom(the RoomId I received with successfull login from the database) ---> Unregister Callbacks ---> LoadLevel()--->...

Now the conflict room:

Check SmartfoxClient static (from the outside class) --> (if is null) go login room again (else) proceed --> Register Callbacks ---> bla bla bla

I do not destroy the first SFSClient instance because it is destroyed when Unity loads the new level, right?. I register all the new callbacks together, and they work fine. I think is more a sending problem, than a receiving one. Actually, I have my chat working again, and I handle publicmessages event well.

I cannot check if "OnUpdateUserVariables" is responding becauseI I cannot even send the HashTable, I still getting NullReferenceException. It is curious, because I am working with HashTables, subsets, dynamic lists, and I do not get nullreference errors ( I try to be careful :) ). There is some wrong with that, but I really don't know what could be.

Anyway, thanks for your patience, I will certainly try to fix it (sometimes :)).

As I was asking in my last post,

1-I'll use XTMessages to set user variables. Is that worst in terms of CPU/RAM/Bandwith usage of Client/Server than the original SetUserVariables method?

2-If I update user variables from the server, Will be the OnUserVariableUpdate event fired to the listening clients?

Thank You!! :lol:
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 28 Oct 2010, 23:20

I'm also using uservars and they work fine for me also. I join a room after login, set the vars and then join the actual room i want.

Code: Select all

Hashtable Avatar = new Hashtable();
Avatar.Add("avatarType", hit.transform.name);
Avatar.Add("outfit", outfitname);
Avatar.Add("boyType", boyType);
smartFoxClient.SetUserVariables(Avatar);

I'm using sfs 1.6 and U3
Hope it helps :)
fernando24691
Posts: 30
Joined: 20 Oct 2010, 19:37

Postby fernando24691 » 29 Oct 2010, 07:11

I downloaded C# SFS api and its version is 1.2.6. Is there any update?
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 29 Oct 2010, 07:59

There is a 1.2.7 rc1 which has to be used in a Unity 3 webplayer. Linked somewhere here in the forums
fernando24691
Posts: 30
Joined: 20 Oct 2010, 19:37

Postby fernando24691 » 24 Nov 2010, 09:29

I think I have an answer.

As I said, I used custom login in my game. If you read the C# API documentation, specifically OnUserLogin event, you will notice that using custom login does not define automatically some SmartFoxClient variables.

And having undefined variables will become in a null reference exception if you try to access them later, like in SetUserVariables() function.

I don not remember which variables specifically you must setup manually when using custom login code, but as I said, you can find them C# API documentation --> SmartFoxClientAPI Class --> "OnUserLogin" (i do not remember the exact name of the call).

So, be carefull with custom logins! :D
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 24 Nov 2010, 11:22

you need to set the user id and username manually also.

Return to “.Net / Unity3D API”

Who is online

Users browsing this forum: No registered users and 3 guests