Use the SDK for cloud saves - Cloud saves - (Unity module)
Unwrap the wrapper
In this tutorial, you will learn how to store and query your game settings using the AccelByte Gaming Services (AGS) Game SDK. The game options will be stored as a Player Record in the AGS Cloud Save service.
Byte Wars uses a wrapper class named CloudSaveEssentialsWrapper
that acts as the wrapper to cache and handle login-related functionalities when using the AGS Game SDK.
The CloudSaveEssentialsWrapper
class uses the ApiClient
and CloudSave
class provided by the AGS Game SDK. They interact with AGS to provide the ability to create, query, update, and delete records in the Cloud Save service.
What's in the Starter Pack
The class CloudSaveEssentialsWrapper_Starter
has been provided for you as the starter code. You can find the file in the Resources section.
- CloudSaveEssentialsWrapper_Starter file:
/Assets/Resources/Modules/CloudSaveEssentials/Scripts/CloudSaveEssentialsWrapper_Starter.cs
There are some functionalities already implemented for you:
Namespace declarations for using the
CloudSave
class in the AGS Game SDK.using AccelByte.Api;
using AccelByte.Core;
using AccelByte.Models;A local variable that holds the reference to the
CloudSave
class in the AGS Game SDK.private CloudSave cloudSave;
Integrate Cloud Save using the SDK
In this section, you will implement using the AGS Game SDK to integrate the Cloud Save service, first on the client side, then on the server side.
Client side
Open Byte Wars in Unity.
Open
CloudSaveEssentialsWrapper_Starter
. InStart()
, initialize thecloudSave
variable to get the clientApiClient
CloudSave
object.void Start()
{
var apiClient = AccelByteSDK.GetClientRegistry().GetApi();
var cloudSave = apiClient.GetCloudSave();
}Implement the function for saving player records. Since the process is asynchronous, create a callback function that will inform the user regarding the result. Add the AGS Game SDK
Result
andResultCallback
as parameters so the result will be accessible from other scripts.private void OnSaveUserRecordCompleted(Result result, ResultCallback customCallback = null)
{
if (!result.IsError)
{
Debug.Log("Save player record from client successful.");
}
else
{
Debug.Log($"Save player record from client failed. Message: {result.Error.Message}");
}
customCallback?.Invoke(result);
}Create the wrapper function that will call the
SaveUserRecord()
function in theCloudSave
class. The function will take arecordKey
string as the unique identifier for the record, arecordRequest
dictionary that maps the record unique identifier to the update request object, aResultCallback
that holds the callback function that will be called when the operation completed, and a boolean to set the record as public.public void SaveUserRecord(string recordKey, Dictionary<string, object> recordRequest, ResultCallback resultCallback)
{
cloudSave.SaveUserRecord(
recordKey,
recordRequest,
result => OnSaveUserRecordCompleted(result, resultCallback),
true
);
}noteThe
SaveUserRecord()
function will automatically create the player record if it doesn't exist yet. If the player record exists, this function will update the record by overwriting with the value in the update request.Implement the function for querying player records. Since the process is asynchronous, create a callback function that will inform the user regarding the result. Add the AGS Game SDK
Result
andResultCallback
as parameters so the result will be accessible from other scripts.private void OnGetUserRecordCompleted(Result<UserRecord> result, ResultCallback<UserRecord> customCallback = null)
{
if (!result.IsError)
{
Debug.Log("Get player record from vlient successful.");
}
else
{
Debug.Log($"Get player record from client failed. Message: {result.Error.Message}");
}
customCallback?.Invoke(result);
}Create the wrapper function that will call the
GetUserRecord()
function in theCloudSave
class. The function will take arecordKey
string as the unique identifier to the record and theResultCallback
that holds the callback function that will be called when the operation completes.public void GetUserRecord(string recordKey, ResultCallback<UserRecord> resultCallback)
{
cloudSave.GetUserRecord(
recordKey,
result => OnGetUserRecordCompleted(result, resultCallback)
);
}
Resources
- GitHub link to the file in the Unity Byte Wars repository: