> For the complete documentation index, see [llms.txt](https://mindcodeinteractive.gitbook.io/easy-build-system/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mindcodeinteractive.gitbook.io/easy-build-system/reference/building-manager/building-save-system.md).

# Building Save System

Handles saving and loading of all placed [Building Parts](/easy-build-system/reference/building-part.md) with multiple storage providers.

***

### How Saving Works

The Save System serializes all placed parts to persistent storage and restores them on load.\
Each part’s position, rotation, scale, socket, variant, and custom state are saved.\
Save keys are scene-based so each scene has its own save data.

Configure on the [Building Manager](/easy-build-system/reference/building-manager.md) inspector under **Save Settings**.\
The Save System requires the **Enable Saving** toggle to be active.

***

### Save Modes

**Automatic**: The system loads on `Start` and saves on `OnDisable`.\
If auto-save is enabled, it also saves at a configurable interval during gameplay.\
On mobile, saving is triggered on `OnApplicationPause` and `OnApplicationFocus`.

**Manual**: You control when to save and load via script.\
Used for multiplayer setups where the network adapter handles authority.

***

### Storage Providers

**PlayerPrefs**: Stores data in Unity's PlayerPrefs. Simple but limited in size.

**LocalFileData**: Saves to `Application.dataPath`. Good for editor testing.

**LocalFilePersistent**: Saves to `Application.persistentDataPath`. Recommended for builds.

Backup files (`.bak`) are created alongside the main save to protect against corruption.

***

### Custom Save Data

Hook into save/load with callbacks on individual parts:

```csharp
part.SaveCallback += (stateData) =>
{
    stateData.SetInt("health", currentHealth);
    stateData.SetString("owner", playerId);
};

part.LoadCallback += (stateData) =>
{
    currentHealth = stateData.GetInt("health");
    playerId = stateData.GetString("owner");
};
```

***

### Scripting Examples

Save and load manually:

```csharp
BuildingManager.Instance.SaveSystem.SaveBuildings();
BuildingManager.Instance.SaveSystem.LoadBuildings();
```

Delete save data:

```csharp
BuildingManager.Instance.SaveSystem.DeleteSave();
```

Check if save data exists:

```csharp
bool hasSave = BuildingManager.Instance.SaveSystem.HasSaveData();
```

Listen for save events:

```csharp
EventPublisher.Subscribe<BuildingSaveEvent.SaveStartedEventArgs>(e =>
{
    Debug.Log("Save started");
});

EventPublisher.Subscribe<BuildingSaveEvent.SaveCompletedEventArgs>(e =>
{
    Debug.Log("Saved " + e.SavedParts.Count + " parts");
});

EventPublisher.Subscribe<BuildingSaveEvent.LoadStartedEventArgs>(e =>
{
    Debug.Log("Load started");
});

EventPublisher.Subscribe<BuildingSaveEvent.LoadCompletedEventArgs>(e =>
{
    Debug.Log("Loaded " + e.LoadedParts.Count + " parts in " + e.LoadTime + "s");
});
```

***

### Networking

For multiplayer, set a network save adapter to control authority:

```csharp
BuildingManager.SetNetworkSaveAdapter(mySaveAdapter);
```

The adapter implements `INetworkBuildingSaveAdapter` to handle saving and spawning parts.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mindcodeinteractive.gitbook.io/easy-build-system/reference/building-manager/building-save-system.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
