# rmp::save

> rmp/save.h — rmp::Value, and saving the game.

Source: https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-save.html

namespace `#include <rmp/save.h>`

rmp/save.h — [`rmp::Value`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Value.html), and saving the game.

```cpp
rmp::Value v;
v["level"] = 7;
v["name"] = "Omar";
v["unlocked"].push("forest");
rmp::save::write("slot1", v);

rmp::Value v;
if (rmp::save::read("slot1", v)) {
    int level = v["level"].as_int(1); // 1 when the key is not there
}
```

EVERY READ HAS A DEFAULT, AND NOTHING THROWS. A save written by yesterday's version is not corrupt, it is missing the keys that were added since — and a missing key reading as its default is what lets an update ship without a migration nine times out of ten. A key of the wrong type reads as the default too. For the tenth time, `[save] version` in the .toml is stored in the file and `v.version()` hands it back:

```cpp
if (v.version() < 2) v["coins"] = v["gold"].as_int(0);
```

WHAT A FAILED READ TELLS YOU. `read()` is true or false in an `if`, and when it is false, .status says why — because "there is no save yet", "the file was cut short" (the power went during a write) and "the file has been changed" are three different messages to show a player:

```cpp
const rmp::save::Result r = rmp::save::read("slot1", v);
if (r.status == rmp::save::Status::MODIFIED) show("this save was edited");
```

WHERE IT GOES. The user's data folder, or next to the executable with `[save] portable` — see `directory()`. A write goes to a temporary file first and replaces the old save only once it is complete, so a crash mid-write leaves the previous save intact. On the web it lands in the browser's IndexedDB and survives a reload.

SEALED SAVES are tamper RESISTANCE, not security: the key is in the binary. They stop a player opening the file in a text editor and giving themselves a thousand coins, and any change to a sealed file is detected. Nothing stops someone determined, and nothing here pretends to.

The JSON underneath is cJSON, and it appears in no header: this file is all a game ever sees of it.

### `Status`

enum class Status

enum [include/rmp/save.h:307](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L307-L313)

Why `read()` did or did not fill the Value. A slot name `read()` refuses is UNREADABLE too.

| Value |  | Means |
|---|---|---|
| `OK` |  | Read, checked, and `out` filled |
| `MISSING` |  | Nothing saved in that slot yet: the first run |
| `TRUNCATED` |  | Cut short, as a write the power went out on would be |
| `MODIFIED` |  | The checksum or the seal does not match: edited, or damaged |
| `UNREADABLE` |  | Not a save at all, or one from a newer framework |

### `Result`

struct Result

struct [include/rmp/save.h:317](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L317-L320)

A read's outcome. True in an `if` only when it is OK, so the three-line load stays three lines; .status is there when the reason matters.

| Field | Type | Default | What it is |
|---|---|---|---|
| `status` | `Status` | `Status::MISSING` | Why; MISSING until a `read()` sets it |

explicit operator bool() const

OK, and only OK

### `WriteOptions`

struct WriteOptions

struct [include/rmp/save.h:324](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L324-L326)

How `write()` stores a save. Declaration order is the order they are written in, as C++20 requires.

| Field | Type | Default | What it is |
|---|---|---|---|
| `encrypted` | `bool` | `RMP_SAVE_ENCRYPT != 0` | `[save] encrypt` decides the default |

### `ReadOptions`

struct ReadOptions

struct [include/rmp/save.h:329](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L329-L336)

What `read()` accepts as a save.

| Field | Type | Default | What it is |
|---|---|---|---|
| `sealed_only` | `bool` | `RMP_SAVE_ENCRYPT != 0` | With `[save] encrypt` on, a PLAIN file reads as MODIFIED: anyone can write one — the format is documented and a plain save shows it — and accepting it would let a player replace a sealed save with their own. A game that turns sealing on in an update reads its players' old plain saves with { .sealed_only = false } for as long as they may have one. |

### `write`

bool write(std::string_view slot, const Value &value, const WriteOptions &options = {})

function [include/rmp/save.h:347](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L347)

A slot is a file name without the extension: letters, digits, '_', '-' and '.', not starting with '.', at most 64 of them. Anything else is refused and said once — a slot name is not a place to put a path.

False when it could not be written; the previous save, if any, is intact. Also false, and said once, for a Value that could not be read back: more than 64 lists and objects inside each other, a NUL character in a string or a key, or a number that is not finite — a NaN or an infinity, which JSON cannot hold.

### `read`

Result read(std::string_view slot, Value &out, const ReadOptions &options = {})

function [include/rmp/save.h:352](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L352)

Fills `out` and says so. On anything but OK, `out` is left exactly as it was, so a game can fill it with defaults first and read over them. A file holding a number too large for a double (1e400) is UNREADABLE: no save holds one.

### `exists`, `remove`

bool exists(std::string_view slot)

bool remove(std::string_view slot)

function [include/rmp/save.h:357](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L357)

Whether there is a save in that slot, in any folder `read()` looks in. It does not open it, so a file that would read as TRUNCATED or MODIFIED exists too. False, without a warning, for a slot name `write()` would refuse.

`remove`: False when there was nothing to remove

### `directory`

std::string directory()

function [include/rmp/save.h:365](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/include/rmp/save.h#L365)

Where saves are written: the folder `[save] portable` asked for if it can be written, otherwise the user's data folder — never where the .toml asked for if that turned out to be read-only. With a trailing separator. With `[save] portable`, `read()` looks in both folders and takes the newer save, so a session that had to fall back and the next one that did not agree.
