LoadAsync

Summary: Load data by name and await the result.

Description

Load data by name and await the result.

Call LoadAsync from Unity’s main thread. Make your first Save System call on that thread too. Storage that requires the main thread, such as PlayerPrefs, also reads there.

Declaration

C#
public static Task<LoadResult<T>> LoadAsync<T>(string name, SaveOptions options = null, Func<T> defaultValue = null);

Parameters

ParameterTypeDescription
T—Type of the data to load.
namestringName of the save.
optionsSaveOptionsOptional. Selects the slot to read from. Null uses the default slot.
defaultValueFunc<T>Optional. Creates the fallback for Data when a load returns a failure status, including NotFound and Corrupt. The result keeps its failure status.

Returns

Await Task<LoadResult<T>> to get the load result. Check IsSuccess to see whether Data contains loaded data. On failure, check Status even if Data contains a fallback.

Example

Create LoadPositionAsync.cs and attach it to an active GameObject. Run the synchronous example on the Save page once to create position-sync-example. Enter Play mode to restore that position.

C#
using Gley.SaveSystem;
using UnityEngine;

public class LoadPositionAsync : MonoBehaviour
{
    private async void Start()
    {
        LoadResult<Vector3> result = await API.LoadAsync<Vector3>("position-sync-example");

        if (result.IsSuccess)
        {
            transform.position = result.Data;
        }
        else
        {
            Debug.LogWarning($"Load failed ({result.Status}): {result.Message}", this);
        }
    }
}
On This Page