Put it all together - Cloud saves - (Unity module)
What's in the Starter Pack
In this section, you will learn how to update statistics (stats) values with gameplay data and display them in the Stats Profile Menu.
The CloudSaveHelper_Starter
class that subscribes to events related to changes in the Options Menu has been provided for you.
- CloudSaveHelper_Starter file:
/Assets/Resources/Modules/CloudSaveEssentials/Scripts/CloudSaveHelper_Starter.cs
The helper class has some functionalities already implemented:
The AccelByte Gaming Services (AGS) Game SDK libraries declarations to allow the use of the Statistic and ServerStatistic classes.
using AccelByte.Core;
using AccelByte.Models;Some event definitions to notify when the player logs in successfully, opens the Options Menu, and closes the Options Menu.
void Start()
{
LoginHandler.onLoginCompleted += tokenData => {};
OptionsMenu.onOptionsMenuActivated += (musicVolume, sfxVolume) => {};
OptionsMenu.onOptionsMenuDeactivated += (musicVolume, sfxVolume) => {};
}
Connect the UI to save data to cloud save
Open the
CloudSaveHelper_Starter.cs
.Define constant variables to hold the unique identifier and record the item names of the audio volume settings player records.
private const string GameOptionsRecordKey = "GameOptions-Sound";
private const string MusicVolumeItemName = "musicvolume";
private const string SfxVolumeItemName = "sfxvolume";Add a local variable to hold the reference of
CloudSaveEssentialsWrapper_Starter
and a dictionary of strings and objects that maps the player record item name of each desired audio volume setting.private CloudSaveEssentialsWrapper_Starter _cloudSaveWrapper;
private Dictionary<string, object> volumeSettings;In
Start()
, initialize the_cloudSaveWrapper
by using the utility class calledTutorialModuleManager
to get the module class. Also, initialize thevolumeSettings
dictionary with the corresponding values of music and sound effects (SFX) volumes that are stored locally with theAudioManager
.void Start()
{
_cloudSaveWrapper = TutorialModuleManager.Instance.GetModuleClass<CloudSaveEssentialsWrapper_Starter>();
volumeSettings = new Dictionary<string, object>()
{
{MusicVolumeItemName, AudioManager.Instance.GetCurrentVolume(AudioManager.AudioType.MusicAudio)},
{SfxVolumeItemName, AudioManager.Instance.GetCurrentVolume(AudioManager.AudioType.SfxAudio)}
};
}Create a callback function to inform when the save audio volume settings function completes.
private void OnSaveGameOptionsCompleted(Result result)
{
if (!result.IsError)
{
Debug.Log("Audio volume settings saved to cloud save!");
}
}Create a function to call the the
SaveUserRecord()
function inCloudSaveEssentialsWrapper_Starter
. Use theGameOptionsRecordKey
constant as the unique identifier of which player record you are saving. ThevolumeSettings
dictionary will use the latest value from theAudioManager
and save it to their corresponding player record item.private void SaveGameOptions()
{
_cloudSaveWrapper.SaveUserRecord(GameOptionsRecordKey, volumeSettings, OnSaveGameOptionsCompleted);
}To ensure the data is updated, create a function that will be called upon the Options Menu closing to get the latest values of the audio volume settings and call the
SaveGameOptions()
function to update the player record data.private void UpdateGameOptions(float musicVolume, float sfxVolume)
{
volumeSettings[MusicVolumeItemName] = musicVolume;
volumeSettings[SfxVolumeItemName] = sfxVolume;
SaveGameOptions();
}In the
Start()
function, modify the listener of theonOptionsMenuDeactivated
event to call theUpdateGameOptions()
function so that you will always save the latest value to AGS Cloud Save when the player is done changing their audio volume settings in the Options Menu.private void Start()
{
...
OptionsMenu.onOptionsMenuDeactivated += (musicVolume, sfxVolume) => UpdateGameOptions(musicVolume, sfxVolume);
}
Connect the UI to load data from cloud save
Open
CloudSaveHelper_Starter.cs
.Prepare the callback function to handle getting the player record results. In Byte Wars, the music and SFX volume settings are also saved locally using
PlayerPrefs
in addition to the player records in AGS Cloud Save. You will use theAudioManager
class to set and get the audio volume settings. If no player record exists, then you will save the current local settings.private void OnGetGameOptionsCompleted(Result<UserRecord> result)
{
if (!result.IsError)
{
Dictionary<string, object> recordData = result.Value.value;
if (recordData != null)
{
volumeSettings[MusicVolumeItemName] = recordData[MusicVolumeItemName];
AudioManager.Instance.SetMusicVolume(Convert.ToSingle(recordData[MusicVolumeItemName]));
volumeSettings[SfxVolumeItemName] = recordData[SfxVolumeItemName];
AudioManager.Instance.SetSfxVolume(Convert.ToSingle(recordData[SfxVolumeItemName]));
}
}
else
{
SaveGameOptions();
}
}Create a new function to call the
GetUserRecord()
function inCloudSaveEssentialsWrapper_Starter
and query the audio volume settings player record.private void GetGameOptions()
{
_cloudSaveWrapper.GetUserRecord(GameOptionsRecordKey, OnGetGameOptionsCompleted);
}Modify the
Start()
function to listen to theonLoginCompleted
event from theLoginHandler
to know when the player successfully logs in andonOptionsMenuActivated
event to know when the Options Menu opens. Bind those events to callGetGameOptions()
when the events trigger to set the audio volume settings according to the saved player record stored in AGS Cloud Save.private void Start()
{
...
LoginHandler.onLoginCompleted += tokenData => GetGameOptions();
OptionsMenu.onOptionsMenuActivated += (musicVolume, sfxVolume) => GetGameOptions();
}
Resources
- GitHub link to the file in the Unity Byte Wars repository: