Randomly get null user variable by User::GetVariable("param")

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

Moderators: Lapo, Bax

birdinforest
Posts: 2
Joined: 17 Aug 2017, 15:06

Randomly get null user variable by User::GetVariable("param")

Postby birdinforest » 19 Aug 2017, 09:38

As a learner of SFS I am struggling to fix a issue about getting a user variable from user object. Any advice and suggestions will be greatly appreciated.

In my Unity3D project I am using following code to get user's name:

Code: Select all

 
            UserVariable v = user.GetVariable("nickname");
            if(!v.IsNull())
            {
                nick.text = v.GetStringValue();
            }
            else
            {
                Debug.LogError("Can't get nickname.");
            }


It works in most of times but randomly get null exception on line if(!v.IsNull())

Firstly I am confused by the fact that it gets correct user variable in most times but there is a few chance to get null? Is it possibly due to internet delay?
Secondly, according to documents v.IsNull() is the right way to indicate that whether a user variable is null or not, but it causes null exception in Unity3D. Should I use v == null to do exception check?

Thanks.
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Randomly get null user variable by User::GetVariable("param")

Postby Bax » 21 Aug 2017, 07:55

A variable is null if it was specifically set to null, for example:

Code: Select all

new SFSUserVariable("username", null)

If it is null because you never set it, you have to check it like this:

Code: Select all

UserVariable v = user.GetVariable("nickname");
if(v != null)
    nick.text = v.GetStringValue();
Paolo Bax
The SmartFoxServer Team

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 30 guests