> For the complete documentation index, see [llms.txt](https://pahutyakv.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pahutyakv.gitbook.io/docs/savely/prefs.md).

# Prefs

**Savely.Prefs** provides a PlayerPrefs-like system for Unity with the following advantages:

* Stores any type (primitive or serializable class/struct) in a single JSON file.
* Preserves type information to prevent accidental mismatches.
* Easy inspection (all data in a human-readable JSON file).
* Supports automatic saving.

***

### <mark style="color:$success;">**Set / Save Values**</mark>

Automatically serializes primitive types (`int`, `float`, `bool`, `string`, `long`, `short`, `byte`) as strings. Serializable non-primitive types are saved as JSON using `JsonUtility`.

```csharp
Savely.Prefs.Set("volume", 0.8f); // float
Savely.Prefs.Set("playerPosition", player.transform.position); // Vector3
```

If `AutoSave` is enabled, any `Set` operation immediately writes to disk. If disabled, you must manually call `Savely.Prefs.Save()` to persist changes.

```csharp
Savely.Prefs.SetAutoSave(true);
Savely.Prefs.Save();
```

***

### <mark style="color:$success;">**Get / Load Values**</mark>

All values are loaded upon first access to the system, so if you are using a custom directory, you should set it before calling the first `Get` method.

```csharp
float volume = Savely.Prefs.Get("volume", 1f);
player.transform,position = Savely.Prefs.Get("playerPosition", Vector3.zero);
```

* Safely handles deserialization errors, returning `defaultValue` if needed or if the key does not exist.
* Logs a warning if the stored type does not match the requested type.

***

### <mark style="color:$success;">**Delete / Check Keys**</mark>

```csharp
Savely.Prefs.Delete("volume");
Savely.Prefs.DeleteAll();

bool hasSavedPostiion = Savely.Prefs.Has("playerPosition");
```

***

### <mark style="color:$warning;">**Limitations & Notes**</mark>

* Only supports types serializable by `JsonUtility` for non-primitive values.
* Type information is preserved for each key to prevent accidental type mismatches.
* Designed for game preferences and small save data, not for large binary assets.
* The name of the data file is `values.prefs`.
