Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

XboxLiveUser.WindowsSystemUser is null... need it for GameSaveProvider

Discussion in 'Windows' started by MattWhiting, Sep 8, 2018.

  1. MattWhiting

    MattWhiting

    Joined:
    Jan 11, 2016
    Posts:
    79
    I need the WindowsSystemUser to create the GameSaveProvider. Am I doing this wrong? Is there some other way to get the WindowsSystemUser?

    Here's my initialization for Connected Storage (UWP version is GameSaveProvider)

    Code (CSharp):
    1.         XboxLiveUser xboxLiveUser = UWPUser.Get().GetUser();
    2.         if (null != xboxLiveUser)
    3.         {
    4.             var initTask = GameSaveProvider.GetForUserAsync(xboxLiveUser.WindowsSystemUser, s_scid).AsTask();
    5.             if (initTask.Result.Status == GameSaveErrorStatus.Ok)
    6.             {
    7.                 this.gameSaveProvider = initTask.Result.Value;
    8.             }
    9.  
    10.             this.m_isInitialized = true;
    11.             if (null != onInit)
    12.                 onInit(GameSaveErrorStatus.Ok == initTask.Result.Status);
    13.         }
    14.  
    I got my XboxLiveUser from this:
    Code (CSharp):
    1.     private async void SignIn()
    2.     {
    3.         Debug.LogError("SignIn");
    4.         SignInResult silentSignInResult = await m_user.SignInSilentlyAsync(m_dispatcher);
    5.         bool signedIn = false;
    6.         switch (silentSignInResult.Status)
    7.         {
    8.             case SignInStatus.Success:
    9.         Debug.LogError("Success");
    10.                 signedIn = true;
    11.                 break;
    12.             case SignInStatus.UserInteractionRequired:
    13.         Debug.LogError("UserInteractionRequired");
    14.                 SignInResult loudSignInResult = await m_user.SignInAsync(m_dispatcher);
    15.                 switch (loudSignInResult.Status)
    16.                 {
    17.                     case SignInStatus.Success:
    18.         Debug.LogError("Success");
    19.                         signedIn = true;
    20.         //m_service.UpdateAchievementAsync(m_user.XboxUserId, "3", 100);
    21.                         break;
    22.                     case SignInStatus.UserCancel:
    23.         Debug.LogError("UserCancel");
    24.                         // TODO: retry sign in
    25.                         break;
    26.                     default:
    27.         Debug.LogError("default");
    28.                         break;
    29.                 }
    30.                 break;
    31.             case SignInStatus.UserCancel:
    32.         Debug.LogError("UserCancel");
    33.                 // TODO: retry or exit
    34.                 break;
    35.             default:
    36.         Debug.LogError("default");
    37.                 break;
    38.         }
    39.  
    40.         if( signedIn )
    41.         {
    42.             m_context = new XboxLiveContext(m_user);
    43.             Debug.LogError("User signed in: " + m_user.Gamertag);
    44.         }
    45.         else
    46.         {
    47.             Debug.LogError("No User");
    48.         }
    49.     }
    50.  
     
  2. MattWhiting

    MattWhiting

    Joined:
    Jan 11, 2016
    Posts:
    79
    If anyone is following along, I found this:
    https://github.com/Microsoft/xbox-live-samples

    The interesting bit is in this comment here:
    // Getting a GameSaveProvider requires the Windows user object. It will automatically get the correct
    // provider for the current Xbox Live user.

    Code (CSharp):
    1.     public async Task<GameSaveErrorStatus> Initialize(XboxLiveContext context)
    2.     {
    3.         if (context == null)
    4.         {
    5.             throw new ArgumentNullException("context");
    6.         }
    7.  
    8.         // Getting a GameSaveProvider requires the Windows user object. It will automatically get the correct
    9.         // provider for the current Xbox Live user.
    10.         var users = await Windows.System.User.FindAllAsync();
    11.  
    12.         if (users.Count > 0)
    13.         {
    14.             GameSaveProviderGetResult result = await GameSaveProvider.GetForUserAsync(
    15.                 users[0], context.AppConfig.ServiceConfigurationId);
    16.             if (result.Status == GameSaveErrorStatus.Ok)
    17.             {
    18.                 m_context = context;
    19.                 m_saveProvider = result.Value;
    20.             }
    21.  
    22.             return result.Status;
    23.         }
    24.         else
    25.         {
    26.             throw new Exception("No Windows users found when creating save provider.");
    27.         }
    28.     }
    29.