Page 1 of 1

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

Posted: 19 Aug 2017, 09:38
by birdinforest
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.

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

Posted: 21 Aug 2017, 07:55
by Bax
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();