Coding Patterns Example
ESave Pro supports multiple coding patterns including synchronous, asynchronous, and callbacks. Here you can find examples for each coding pattern so that you can compare the differences and determine which one is best for your use case.
Each example does the exact same thing in a different way. Here's what the code is doing in order of operations:
Create a save state
Load the save state
Save a string to the save state
Load the string from the save state and log it in the console
Synchronous
Multithread mode must be disabled for synchronous coding.
/// <summary>
/// Synchronous example. Multithread mode must be disabled.
/// </summary>
public void SynchronousExample()
{
// Create save (if the save is already created, it's simply returned)
var mySave = ESave.CreateSave(0).Result;
// Load save (set as active)
// You can also load it by it's ID: ESave.Load(0)
ESave.Load(mySave);
// Add data
mySave.AddData("MyDataID", "some string");
// Load data and debug it
var myData = mySave.GetData<string>("MyDataID").Result;
Debug.Log(myData);
}
Asynchronous
Works in all multithread modes, but is meant for modes other than disabled.
/// <summary>
/// Async example. Should work on all multithread modes, but it's meant for Major and Minor or just Major.
/// </summary>
public async void AsynchronousExample()
{
// Create save (if the save is already created, it's simply returned)
var create = ESave.CreateSave(0);
await create.task;
var mySave = create.Result;
// Load it (set as active)
var load = ESave.Load(mySave);
await load.task;
// Add data
var add = mySave.AddData("MyDataID", "some string");
await add.task;
// Load data and debug it
var get = mySave.GetData<string>("MyDataID");
await get.task;
Debug.Log(get.Result);
}
Callbacks
Works in all multithread modes, but is meant for modes other than disabled.
/// <summary>
/// Callbacks example. Should work on all multithread modes, but it's meant for Major and Minor or just Major.
/// </summary>
public void CallbacksExample()
{
// Create save (if the save is already created, it's simply returned)
ESave.CreateSave(0).OnComplete(mySave =>
{
// Load it (set as active)
ESave.Load(mySave).OnComplete(mySave =>
{
// Add data
mySave.AddData("MyDataID", "some string").OnComplete(complete =>
{
// Load data and debug it
mySave.GetData<string>("MyDataID").OnComplete(result =>
{
Debug.Log(result);
});
});
});
});
}
Last updated