# DataPersistenceManager

The `DataPersistenceManager` interacts with the [`FileDataHandler`](https://cowsinss-organization.gitbook.io/save-and-load-add-on/content/03.-content/03.1-datapersistence/filedatahandler) to manage data encryption and decryption, allowing the [`GameDataManager`](https://cowsinss-organization.gitbook.io/save-and-load-add-on/content/03.-content/03.1-datapersistence/gamedatamanager) to process and load game data seamlessly.

{% hint style="warning" %}
**IMPORTANT**: The `DataPersistenceManager` must exist in the scene for data to be saved.

You don’t need to manually add the `DataPersistenceManager` to each scene, as it is marked with `[DontDestroyOnLoad]`. Simply place the `DataPersistenceManager` in your Main Menu scene. When starting the game in a gameplay scene, data won’t be saved. However, when starting from the Main Menu, the `DataPersistenceManager` will persist across different scenes, ensuring data can be saved.
{% endhint %}

### Saving & Loading the Game programmatically

In Save & Load Pro Add-On, saving the game is very simple! You can access DataPersistenceManager.cs Singleton to Save & Load the game with a single line of code.

{% hint style="warning" %}
Only the Data Stored in GameDataManager will be saved into the Game Data Json, so make sure to Store the Required Data, otherwise nothing will be saved.
{% endhint %}

{% hint style="warning" %}
In order to access DataPersistenceManager, ensure to add the following library on top of your Script:
{% endhint %}

```csharp
using cowsins.SaveLoad; // Required to access DataPersistenceManager
```

#### Saving Games Programmatically

```csharp
DataPersistenceManager.instance.SaveGame();
```

#### Loading Games Programmatically

```csharp
DataPersistenceManager.instance.Loadame();
```

### Profile ID Management in Save & Load System

The `DataPersistenceManager` manages Profile IDs and interacts with the Save & Load Pro Add-On to handle data operations across different save slots. It maintains a Profile ID to monitor the current save slot, allowing for specific operations based on this ID. The Profile ID remains unchanged during gameplay and can only be modified from the Main Menu when selecting a different save slot.

***

### CONFIGURATION

* <mark style="color:purple;">dataPersistenceSettings:</mark> Defines General Settings for the Save & Load. These settings can be configured in the Save & Load Tab of the Cowsins Manager. Check [DataPersistence\_SO ](https://cowsinss-organization.gitbook.io/save-and-load-add-on/content/03.-content/03.1-datapersistence/datapersistence_so)for more information.

### Functions | DataPersistenceManager.cs&#x20;

> <mark style="color:green;">ChangeSelectedProfileId(string newProfileId)</mark>
>
> Selects a new Save Slot.
>
> * <mark style="color:purple;">newProfileId</mark>: New  Save Slot Index.

> <mark style="color:green;">SaveGame()</mark>
>
> Saves the Game ( Saves the Stored Data in GameDataManager into a Json file that corresponds to the current Profile ID )

> <mark style="color:green;">LoadGame()</mark>
>
> Loads the Game ( Loads the Data from the Json file that corresponds to the current Profile Id )

> <mark style="color:green;">HasGameData() | Returns Bool</mark>
>
> Returns False if the Game Data is empty or null.

> <mark style="color:green;">GetLastUsedProfileID() | Returns String</mark>
>
> Returns the most recently played Profile ID ( That corresponds to a Save Slot )

> <mark style="color:green;">IsAutoSaveEnabled() | Returns Bool</mark>
>
> Returns true if Auto Save is enabled.

> <mark style="color:green;">EnableAutoSave()</mark>
>
> Enables Auto Save.

> <mark style="color:green;">DisableAutoSave() |</mark>&#x20;
>
> Disables AutoSave

> <mark style="color:green;">IsNonSaveableScene() | Returns Bool</mark>
>
> Returns true if the current scene should not implement Save & Load Functionality.

> <mark style="color:green;">GetCurrentGameData() | Returns GameData</mark>
>
> Returns Current Game Data


---

# 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://cowsinss-organization.gitbook.io/save-and-load-add-on/content/03.-content/03.1-datapersistence/datapersistencemanager.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.
