microsoft / xbox-live-unity-plugin

The Xbox Live Unity Plugin provides a way for developers in the Xbox Live Creators Program to quickly and easily integrate Xbox Live functionality into their Unity based game. For ID@Xbox developers, this Xbox Live Unity Plugin does not yet support all the features you will need. Instead, contact your Microsoft representative.
MIT License
167 stars 62 forks source link

CreateSocialUserGroupFromList can return 0 users in the group #311

Open NoelStephensNILC opened 3 years ago

NoelStephensNILC commented 3 years ago

I am still testing some things with this plugin, but it would appear that within the LoadProfileInfo method of the PlayerAuthentication.cs file the call to: XboxLive.Instance.SocialManager.CreateSocialUserGroupFromList(this.xboxLiveUser, new List<string> { this.xboxLiveUser.XboxUserId });

Can sometimes return a users count of zero for a short period of time, but long enough period of time that the SocialManagerComponent.Instance.EventProcessed event notifications can fire off and notify the PlayerAuthentication's SocialManagerEventProcessed method before the XboxSocialUserGroup property (userGroup) has any entries in the Users list.

This series of events will cause the FinishLoadingProfileInfo Coroutine to throw an "invalid index" exception if it executes prior to there being any entries in the Users list contained within userGroup (XboxSocialUserGroup). This bug appears typically when running in any visual studio compiled release mode (i.e. Debug builds from within Unity Editor compiled as Release) version.

To verify this bug, you can add the following code within the LoadProfileInfo method:

                userGroup = XboxLive.Instance.SocialManager.CreateSocialUserGroupFromList(this.xboxLiveUser, new List<string> { this.xboxLiveUser.XboxUserId });
                if (userGroup.Count == 0)
                {
                    ExceptionManager.Instance.ThrowException(ExceptionSource.PlayerAuthentication, ExceptionType.CreateSocialUserGroupFailed, new Exception("CreateSocialUserGroupFromList Failed to return any users for the Group!"));                        
                }

A temporary work around (for anyone interested) would be something like: (this is just pseudo-example code and you could also simply just check the userGroup.Users.Count prior to using the existing code that uses GetUsersFromXboxUserIds)

    XboxSocialUser currentsocialuser = null;
    private IEnumerator FinishLoadingProfileInfo()
    {
        while (currentsocialuser == null)
        {
            foreach (XboxSocialUser xboxsocuser in userGroup.Users)
            {
                currentsocialuser = xboxsocuser;
                if (currentsocialuser.XboxUserId == this.xboxLiveUser.XboxUserId)
                {
                    break;
                }
            }
            if(currentsocialuser != null)
            {
                break;
            }

            yield return new WaitForSeconds(2);
        }

        //var socialUser = userGroup.GetUsersFromXboxUserIds(new List<string> { this.xboxLiveUser.XboxUserId })[0];
        //var www  = new UnityWebRequest(currentsocialuser.DisplayPicRaw + "&w=128");
        var www = new WWW(currentsocialuser.DisplayPicRaw + "&w=128");
        yield return www;

        try
        {
            if (www.isDone && string.IsNullOrEmpty(www.error))
            {
                GamerPic = www.texture;
                UpdateGamerPic = true;
            }

        }
        catch (Exception ex)
        {
            ExceptionManager.Instance.ThrowException(
                        ExceptionSource.PlayerAuthentication,
                        ExceptionType.LoadGamerPicFailed,
                        ex);
        }

    }

Anyway, this one took me awhile to realize that it was a timing issue. This could be due to more recent changes in various libraries or the like and most likely at the time the original code was written did not present itself as it does today.

In case anyone is having issues with UserGroups, loading of profiles, and are using the original code from this library then you will most likely find this post helpful!

Cheers! :)