SaveService

Summary: The object behind API.Default. Subscribe to its events to react to saves and loads.

Description

SaveService is the object behind API.Default. Every static API method forwards to it. You don’t need to create one: call API for saving, loading and slots, and use API.Default only to subscribe to the events below.

Namespace Gley.SaveSystem.

Declaration

C#
public class SaveService

Members

EventDescription
SaveStartedRuns when a named save starts, before the data is prepared.
SaveCompletedReports the result when a named save finishes.
LoadCompletedReports the final status when a named load finishes.
RecoveredFromBackupRuns when a load recovers from a temporary or backup copy.

The service also has the same methods as the static API, with the same arguments and results. Calling API.Save and API.Default.Save does the same thing.

Example

Create WatchSavesAndLoads.cs and attach it to an active GameObject. Use it alongside the Save or Load examples and enter Play mode. The Console prints the result of each save and load made through API.

C#
using Gley.SaveSystem;
using UnityEngine;

public class WatchSavesAndLoads : MonoBehaviour
{
    private SaveService _service;

    private void OnEnable()
    {
        _service = API.Default;
        _service.SaveCompleted += OnSaveCompleted;
        _service.LoadCompleted += OnLoadCompleted;
    }

    private void OnDisable()
    {
        _service.SaveCompleted -= OnSaveCompleted;
        _service.LoadCompleted -= OnLoadCompleted;
    }

    private void OnSaveCompleted(SaveEventInfo info, SaveResult result)
    {
        Debug.Log($"Save {info.Slot}/{info.Name}: {result.Status}", this);
    }

    private void OnLoadCompleted(SaveEventInfo info, SaveStatus status)
    {
        Debug.Log($"Load {info.Slot}/{info.Name}: {status}", this);
    }
}

Separate service (advanced)

new SaveService() creates a second service that reads the same project settings. Use it only when you need something fully apart from API, such as a test with its own MemoryStorage.

A separate service shares nothing you set up at runtime through API. Keys from SetKey, storage from SetStorage, steps from AddUpgradeStep, mappings from MapV2 and event subscriptions on API.Default do not apply to it. Set up each service you create. Services that use the same storage read and write the same saves.

Create UseSeparateSaveService.cs and attach it to an active GameObject. Enter Play mode to save to memory through a separate service. The shared service and its saves are not affected.

C#
using Gley.SaveSystem;
using UnityEngine;

public class UseSeparateSaveService : MonoBehaviour
{
    private void Start()
    {
        SaveService service = new SaveService();
        service.SetStorage(new MemoryStorage("type-reference-service"));
        SaveResult result = service.Save(transform.position, "position");
        Debug.Log($"Save status: {result.Status}", this);
    }
}
On This Page