ESave
ESave Pro
ESave Pro
  • ESave Pro Documentation
  • Pro Comparison
  • đŸ•šī¸Getting Started
    • Installation
    • Demo
  • ESave Settings
  • 🧩Components
    • ESave Initializer
  • 📄Scripting
    • Initialization
    • Save Operation
    • Save States
      • Infinite Saves
        • Infinite Save Scripting
    • Saving & Loading Data
      • Saving
      • Loading
      • Custom Serializable Objects
      • Example Script
    • Data Deletion
  • đŸ› ī¸Support
    • Getting Help
  • 📚Changelogs
    • Latest Releases
  • ⭐Rate Me?
Powered by GitBook
On this page
  • Working with an Operation
  • Getting the Result
  • States
  • Events
  • Recommendation
  • Examples
  1. Scripting

Save Operation

The main class that handles saving and loading operations.

Many methods related to saving and loading return a SaveOperation<T> object. The T represents the result type of the operation.

Working with an Operation

The main purpose of the SaveOperation class is to support multithreading. Additionally, it allows you to register events, enabling you to receive results or trigger actions precisely when needed.

Getting the Result

You can get the result of an operation by accessing the Result field.

var result = saveOperation.Result;

The result is only equal to something after successful completion. See Recommendation.

States

You can get the current State of the operation with the state field.

Name
Description

NotStarted

The operation has not begun.

Ongoing

The operation has begun and it has not yet completed.

Completed

The operation has successfully completed.

Canceled

User canceled the operation. An operation can be canceled with saveOperation.Cancel().

Failed

The operation failed. This may happen if an error occurred during the operation.

Events

The recommended way of working with operations is by utilizing the available events.

Name
Description

onEnded

A callback for when the operation has completed, canceled, or failed. Events are cleared after the operation has ended.

onCompleted

A callback for when the operation has completed. Events are cleared after the operation has ended.

onFailed

A callback for when the operation has failed. Events are cleared after the operation has ended.

You may use the OnEnd, OnComplete, or OnFail methods respectively to register events.

Recommendation

Below is the recommended scripting pattern to make something happen after the operation has completed while also getting the result. This pattern is recommended because it works no matter what the multithread mode is set to.

NOTE: the SaveOperation result type may vary. However, it's usually SaveState or bool.

saveOperation.OnComplete(result =>
{
    Debug.Log(result);
});

The code is simply logging the result in the console after the operation is successfully completed. You can even chain register events as shown below.

saveOperation.OnComplete(result =>
{
    Debug.Log(result);
}).OnFail(() =>
{
    Debug.Log("failed");
});

Event registration like this is not necessary if the multithread mode is set to disabled.

Examples

Callbacks

The code below loads a save state and then immediately retrieves data from it.

public void Load()
{
    ESave.Load(0).OnComplete(saveState =>
    {
        saveState.GetData<string>("MyDataID").OnComplete(data =>
        {
            Debug.Log(data);
        });
    });
}

Async Pattern

If you prefer using the async pattern (async methods with await), you can do the following to achieve the same results as above:

public async void Load()
{
    var saveState = await ESave.Load(0).task;
    var data = await saveState.GetData<string>("MyDataID").task;
    Debug.Log(data);
}

No Multithreading

It's possible to do the same thing in another way, but only if multithread mode is set to disabled.

public void Load()
{
    var saveState = ESave.Load(0).Result;
    var data = saveState.GetData<string>("MyDataID");
    Debug.Log(data);
}
PreviousInitializationNextSave States

Last updated 26 days ago

📄