---
# System prepended metadata

title: SaveState API Flow

---

# SaveState API Flow

## Initialization

### SaveStateManager

**`class CPGSAVESTATE_API UCPGSaveStateManager : public UActorComponent`**

* Must be component of `AGameState`
* `APGameState` creates and attaches this in its constructor, and marks it as Replicated
* `APGameMode` blocks match state in `PostInitializeComponents` via `RequestBlockMatchState` until `SaveStateManager` is ready -- listens for Promise on `APGameState`
    
### SaveStateLocalSystem

**`class CPGSAVESTATE_API UCPGSaveStateLocalSystem : public ULocalPlayerSubsystem`**

* Automatically constructed by Unreal

## Load
1. `AAPGameState::PostInitializeComponents()` ->
    * `SaveStateManagerComponent->RestoreMatchSaveState(...)` ->
        * `UCPGSaveStateLocalSystem::LoadMatch(...)` ->
            * `UGameplayStatics::LoadGameFromSlot`
        * `UCPGSaveStateFunctionLibrary::DeserializeObject(...)`

Note: this happens before the match starts, before `BeginPlay`.

## Save
1. `UCPGSaveStateManager::UpdateLatestSnapshot` ->
    * `UCPGSaveStateFunctionLibrary::SerializeObject(...)`
    * (Replicate `LatestSaveState`) ->
    * `UCPGSaveStateManager::OnLatestSaveStateChanged()` ->
        * `UCPGSaveStateLocalSystem::SaveSnapshot(...)` ->
            * `UGameplayStatics::SaveGameToSlot(...)`