- Home /
 
 
               Question by 
               conguerror · Feb 12 at 05:47 PM · 
                savegoogle playsave datagoogle play gamesgoogle  
              
 
              Google play save data
Hello,
I am new into google API and I want to implement a save system in a google play services but I don't know how to use it practically. I got a script that saves and loads a string from a cloud but I want to get variables just like in PlayerPrefs so I can load items and the amounts of items in the beginning of the game. Shortly: how do I get load and save data to cloud and access them like in PlayerPrefs? So maybe I can create a function like this:
   void GetData(){
        healthPoints = cloudSave.GetHealthPointsFromCloud();//
     //how can I use google play save cloud to do something like this? 
     }
 
               My whole script just saves data in a singlestring which is honestly is useless to me in this state.
 using UnityEngine;
 using GooglePlayGames;
 using GooglePlayGames.BasicApi;
 using GooglePlayGames.BasicApi.SavedGame;
 using System;
 public class PlayGames : MonoBehaviour
 {
     public int playerScore;
     string leaderboardID = "";
     string achievementID = "";
     public static PlayGamesPlatform platform;
     private bool issaving = false;
     private string SAVE_NAME = "savegames";
     string savedString;//for saves,duh
 
 
     void Start()
     {
         if (platform == null)
         {
             PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().Build();
             PlayGamesPlatform.InitializeInstance(config);
             PlayGamesPlatform.DebugLogEnabled = true;
             platform = PlayGamesPlatform.Activate();
         }
 
         Social.Active.localUser.Authenticate(success =>
         {
             if (success)
             {
                 Debug.Log("Logged in successfully");
             }
             else
             {
                 Debug.Log("Login Failed");
             }
         });
         UnlockAchievement();
     }
 
     public void AddScoreToLeaderboard()
     {
         if (Social.Active.localUser.authenticated)
         {
             Social.ReportScore(playerScore, leaderboardID, success => { });
         }
     }
 
     public void ShowLeaderboard()
     {
         if (Social.Active.localUser.authenticated)
         {
             platform.ShowLeaderboardUI();
         }
     }
 
     public void ShowAchievements()
     {
         if (Social.Active.localUser.authenticated)
         {
             platform.ShowAchievementsUI();
         }
     }
 
     public void UnlockAchievement()
     {
         if (Social.Active.localUser.authenticated)
         {
             Social.ReportProgress(achievementID, 100f, success => { });
         }
     }
     public void ChangeAchievementID(string newValue){
         achievementID = newValue;
     }
     public void ChangeLeaderBoardID(string newValue){
         leaderboardID = newValue;
     }
     public void SetScore(int newValue){
         playerScore = newValue;
     }
     //cloud save
      public void OpenSaveToCloud(bool saving)
     {
        // debugtext.text = "hello";
         if(Social.localUser.authenticated)
         {
           //  debugtext.text = "hello2";
             issaving = saving;
             ((PlayGamesPlatform)Social.Active).SavedGame.OpenWithAutomaticConflictResolution
                 (SAVE_NAME, GooglePlayGames.BasicApi.DataSource.ReadCacheOrNetwork,
                 ConflictResolutionStrategy.UseLongestPlaytime, SavedGameOpen);
 
         
     }
     }
 
     private void SavedGameOpen(SavedGameRequestStatus status, ISavedGameMetadata meta)
     {
         if(status == SavedGameRequestStatus.Success)
         {
            // debugtext.text = "hello in save1";
             if (issaving)//if is saving is true we are saving our data to cloud
             {
                // debugtext.text = "hello in save2";
                 byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(GetDataToStoreinCloud());
                 SavedGameMetadataUpdate update = new SavedGameMetadataUpdate.Builder().Build();
                 ((PlayGamesPlatform)Social.Active).SavedGame.CommitUpdate(meta, update, data, SaveUpdate);
             }
             else//if is saving is false we are opening our saved data from cloud
             {
                 ((PlayGamesPlatform)Social.Active).SavedGame.ReadBinaryData(meta, ReadDataFromCloud);
             }
         }
     }
 
     private void ReadDataFromCloud(SavedGameRequestStatus status, byte[] data)
     {
         if(status == SavedGameRequestStatus.Success)
         {
             string savedata = System.Text.ASCIIEncoding.ASCII.GetString(data);
             LoadDataFromCloudToOurGame(savedata);
         }
     }
     private void SaveUpdate(SavedGameRequestStatus status, ISavedGameMetadata meta)
     {
         //use this to debug whether the game is uploaded to cloud
         Debug.Log("successfully add data to cloud");
     }
     private string GetDataToStoreinCloud()//  we seting the value that we are going to store the data in cloud
     {
         string Data = "";
     //data [0]
         Data += savedString;
         Data += "|";
         return Data;
     }
     private void LoadDataFromCloudToOurGame(string savedata)
     {
         string[] data = savedata.Split('|');
         Debug.Log(data[0].ToString());
     }
     public void SetSaveString(string newString){
         savedString = newString;
     }
     
 }
 
 
              
               Comment
              
 
               
              I need to do it because I need to structure my data properly
Your answer