# Save States

{% hint style="info" %}
To find out how to manage data inside a save state, see [Saving & Loading](/esave/esave-pro/scripting/saving-and-loading.md).
{% endhint %}

## Active Save State

It's important to understand what the active save state is before continuing. The active save state acts as a temporary copy that updates as the player progresses through the game. Instead of modifying the original save file directly, all changes are made to this active copy. The original save remains unchanged unless the player explicitly chooses to save, ensuring that it can be reloaded in its unedited state if needed.

## Creating New Saves

New save states require a unique integer ID. That's all you really need—but even that isn't necessary to manage yourself. Save state IDs are auto-incremented to ensure they are all unique. However, you can still create a save state with a specific ID.

Use the `ESave.CreateSave` method to create a new save. If an ID parameter is not passed, the ID will be automatically generated.

```csharp
ESave.CreateSave();
```

This method returns a [Save Operation](/esave/esave-pro/scripting/save-operation.md) (result type: `SaveState`).

To create a new save state with a specific ID, simply pass it as a parameter. In the code below, a new save with the ID 0 will be created if an existing one with the ID doesn't exist. If there is another save with the same ID, the existing save will be returned instead.

```csharp
ESave.CreateSave(0);
```

## Checking Existing Saves

Use the `HasSave` method to simply check if a save with a specific ID exists.

```csharp
int id = 0;
bool result = ESave.HasSave(id);
```

## Getting Save States

#### Active

You can get the active save state with the `SaveState.active` field.

#### By ID

You can get a save state with the `ESave.GetSaveState` method.

```csharp
var mySaveState = ESave.GetSaveState(mySaveStateID);
```

#### Getting All Save States

To get a list of all save states, use `ESave.GetAllSaveStates`.

<pre class="language-csharp"><code class="lang-csharp"><strong>ESave.GetAllSaveStates().OnComplete(allSaveStates =>
</strong>{
    Debug.Log(allSaveStates.Count);
});
</code></pre>

This method returns a [Save Operation](/esave/esave-pro/scripting/save-operation.md) (result type: `List<SaveState>`).

## Loading Save States

You can load a save state by passing its ID in the `ESave.Load` method. This will set the loaded save state as active.

```csharp
ESave.Load(mySaveStateID);
```

This method returns a [Save Operation](/esave/esave-pro/scripting/save-operation.md) (result type: `SaveState`).

## Checking Loaded State

You can check if a specific save state is loaded (active) with the `ESave.IsLoaded` method.

```csharp
bool isLoaded = ESave.IsLoaded(mySaveStateID);
```

Alternatively, you can check this from a `SaveState` itself with the `IsEditable` property (only the loaded save can be edited).

```csharp
if (mySaveState.IsEditable)
{
    // Your code here
}
```

## Saving Save States

To save all data of the active save state (the temporary copy) into the original save state, call `ESave.Save`.

```csharp
ESave.Save();
```

If an `int` (save state ID) or `SaveState` parameter is passed, the active save state will instead store its data into the save state of your choice. Either way, the targeted save state's data is completely overwritten by the active save state data.

This method returns a [Save Operation](/esave/esave-pro/scripting/save-operation.md) (result type: `SaveState`).

## Deleting Save States

### Deleting a Save State

The `DeleteSave` method completely removes a save state. It accepts an ID (`int`) or `SaveState` parameter.

```csharp
ESave.DeleteSave(mySaveState);
```

This method returns a [Save Operation](/esave/esave-pro/scripting/save-operation.md) (result type: `bool`). The result is true if the save was successfully&#x20;deleted. Otherwise, false.

### Deleting All Save States

Use the `DeleteAllSaves` method to completely remove all save states.

```csharp
ESave.DeleteAllSaves();
```

This method returns a [Save Operation](/esave/esave-pro/scripting/save-operation.md) (result type: `bool`). The result in this case is irrelevant (always true)


---

# Agent Instructions: 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:

```
GET https://espergames.gitbook.io/esave/esave-pro/scripting/save-states.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
