Search Unity

How to get / create metadata when you first enter the game? (play-games-plugin-for-unity)

Discussion in 'Scripting' started by LexaMV, May 14, 2020.

  1. LexaMV

    LexaMV

    Joined:
    Feb 20, 2018
    Posts:
    28
    Hey. We want to use the save on google drive to record the necessary data during the game. Faced a problem. If you enter the game for the first time, then there is no save file on google drive. When calling a method

    savedGameClient.OpenWithAutomaticConflictResolution(fileName,
    DataSource.ReadCacheOrNetwork,
    ConflictResolutionStrategy.UseLongestPlaytime,
    onDataOpen);

    metadata is null!!!!!

    We noticed that your code has a check for the co-existence of a save file, and if there is no file, it is created automatically. However, this does not work on all devices.

    Therefore, when trying

    savedGameClient.CommitUpdate (currentMetadata,
    updatedMetadata,
    data,
    (status, metadata) => currentMetadata = metadata)

    since currentMetadata == null, we get an exception.

    We decided to create our class and implement the ISavedGameMetadata interface there and try to assign the class object to currentMetadata. When savingGameClient.CommitUpdate, we get an error that our metadata was not created using savedGameClient. Tell me how to create your metadata for such cases?

    We will be grateful if you post an example.
     
    KyryloKuzyk likes this.
  2. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,145
    Encountered the same issue.
    The proposed way to create a game save is to call the
    PlayGamesPlatform.Instance.SavedGame.ShowSelectSavedGameUI()
    and let the user create a save file manually.

    But I want to create the save file from the code without asking a user to do it manually.
     
  3. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,145
    I think I've figured it out. The OpenWithAutomaticConflictResolution method should really be called OpenOrCreateWithAutomaticConflictResolution. If there is no previously saved file, the OpenWithAutomaticConflictResolution will automatically create it and return the metadata for it.
    Code (CSharp):
    1. using GooglePlayGames;
    2. using GooglePlayGames.BasicApi;
    3. using GooglePlayGames.BasicApi.SavedGame;
    4. using UnityEngine.Assertions;
    5.  
    6. public static class PlayGamesPlatformExample {
    7.     public static void OpenOrCreateWithAutomaticConflictResolution(string fileName) {
    8.         Assert.IsTrue(PlayGamesPlatform.Instance.IsAuthenticated(), "You must to be logged in to Play Games to call Saved Games API.");
    9.         PlayGamesPlatform.Instance.SavedGame.OpenWithAutomaticConflictResolution(fileName, DataSource.ReadNetworkOnly, ConflictResolutionStrategy.UseLongestPlaytime, (status, metadata) => {
    10.             if (status == SavedGameRequestStatus.Success) {
    11.                 Assert.IsNotNull(metadata, "Metadata should never be null if status is SavedGameRequestStatus.Success.");
    12.             }
    13.         });
    14.     }
    15. }
     
    SF_FrankvHoof likes this.