GetVariable exception

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

Nexic
Posts: 31
Joined: 26 Jun 2008, 12:23

GetVariable exception

Postby Nexic » 14 Jul 2018, 10:18

Sometimes our Unity 5.6 game reports the following error:

Code: Select all

KeyNotFoundException: The given key was not present in the dictionary.
System.Collections.Generic.Dictionary`2[System.String,Sfs2X.Entities.Variables.UserVariable].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
Sfs2X.Entities.SFSUser.GetVariable (System.String name)
SFSManager.OnUserVarsUpdate (Sfs2X.Core.BaseEvent evt) (at Assets/Scripts/Multiplayer/SFSManager.cs:930)
Sfs2X.Core.EventDispatcher.DispatchEvent (Sfs2X.Core.BaseEvent evt)
Rethrow as Exception: Error dispatching event userVariablesUpdate: The given key was not present in the dictionary.   at System.Collections.Generic.Dictionary`2[System.String,Sfs2X.Entities.Variables.UserVariable].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
Sfs2X.Entities.SFSUser.GetVariable (System.String name)
SFSManager.OnUserVarsUpdate (Sfs2X.Core.BaseEvent evt) (at Assets/Scripts/Multiplayer/SFSManager.cs:930)
Sfs2X.Core.EventDispatcher.DispatchEvent (Sfs2X.Core.BaseEvent evt)
Sfs2X.Core.EventDispatcher.DispatchEvent (Sfs2X.Core.BaseEvent evt)
Sfs2X.SmartFox.ProcessEvents ()
SFSManager.ProcessEvents () (at Assets/Scripts/Multiplayer/SFSManager.cs:72)


The offending code is simply:

Code: Select all

if(user.ContainsVariable(key) )
{
   UserVariable newVar = user.GetVariable(key); //actual error line (930)
   changedVarsDict[key] = newVar.Value;
}



The documentation for this function says that it should return null if the variable does not exist, not create an exception. In addition,the variable should exist since the key we are trying to get comes directly from the changedVars parameter sent from the OnUserVarsUpdate callback AND because we have already checked the value with ContainsVariable.

For now I have managed to work around this issue by handling the failure more gracefully, but it would be nice if this could be fixed.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: GetVariable exception

Postby Lapo » 14 Jul 2018, 10:39

Hi,
what version of the C# API are you running please?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
Nexic
Posts: 31
Joined: 26 Jun 2008, 12:23

Re: GetVariable exception

Postby Nexic » 14 Jul 2018, 12:10

1.7.6, and using the debug version of the DLL although I'm pretty sure this happened in the release version too.
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: GetVariable exception

Postby Bax » 16 Jul 2018, 10:24

We double checked the behavior, requesting a non-existent variable, and everything is fine.
Also, the code is straightforward in the API, and I don't know how anything could go wrong:

Code: Select all

      public UserVariable GetVariable(string name) {
         if (!variables.ContainsKey(name)) return null;
         return variables[name];
      }

Something else must be going on here.
Also, you say you use the ContainsVariable method, which works the same as above:

Code: Select all

      public bool ContainsVariable(string name) {
         return variables.ContainsKey(name);
      }

Maybe you can send an example causing the issue?
Thank you.
Paolo Bax
The SmartFoxServer Team
Nexic
Posts: 31
Joined: 26 Jun 2008, 12:23

Re: GetVariable exception

Postby Nexic » 18 Jul 2018, 16:08

There really is nothing more that could be going on, I've copied and pasted you the exact code from that section and as the trace shows, the error is happening inside your function. In your example, could GetVariable could be failing in if "variables" is null maybe? Or perhaps variables is a custom dictionary class and something is failing deeper within your code?

Either way, it's not the end of the world. It seems fairly rare and I've manged to work around it. Just would be nice to fix for other developers I guess :)
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: GetVariable exception

Postby Bax » 19 Jul 2018, 08:05

"variables" is a standard Dictionary:

Code: Select all

Dictionary<string, UserVariable> variables

It can't be null, or we would be calling a method on a null object, causing a different exception.
What platform are you building your project for? Maybe this is a platform-specific issue? Or could it have to do with your version of Unity? We are testing under Unity 2017.1.
Paolo Bax
The SmartFoxServer Team

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 21 guests