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

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.

1 Like

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.

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.

using GooglePlayGames;
using GooglePlayGames.BasicApi;
using GooglePlayGames.BasicApi.SavedGame;
using UnityEngine.Assertions;

public static class PlayGamesPlatformExample {
    public static void OpenOrCreateWithAutomaticConflictResolution(string fileName) {
        Assert.IsTrue(PlayGamesPlatform.Instance.IsAuthenticated(), "You must to be logged in to Play Games to call Saved Games API.");
        PlayGamesPlatform.Instance.SavedGame.OpenWithAutomaticConflictResolution(fileName, DataSource.ReadNetworkOnly, ConflictResolutionStrategy.UseLongestPlaytime, (status, metadata) => {
            if (status == SavedGameRequestStatus.Success) {
                Assert.IsNotNull(metadata, "Metadata should never be null if status is SavedGameRequestStatus.Success.");
            }
        });
    }
}
1 Like