Data file
A well-built Data is useless as long as it stays in memory; DataFile is what actually writes it to disk, as a JSON file tied to a simple name, and reads it back on the next launch.
Overview
DataFile is contained in the header "remake2d/data.hpp". Each DataFile maps a given name to a single JSON file stored under SaveManager's root directory, exposed through the singleton data. It accepts both a raw Data and any type implementing Savable directly in its save/load overloads.
Methods
void load(Data&);
void load(Savable&);
void save(const Data&);
void save(const Savable&);
void remove(void) noexcept; // remove file
bool exist(void) const noexcept; // check if file exist
std::string path(void) const noexcept; // get full path
std::string name(void) const noexcept; // get file name
Usage
Creating a file
No need to know the full path of a save file ahead of time; the constructor only needs a name, and the actual path resolves automatically against SaveManager's root directory:
Saving and loading
save/load accept either a raw Data or any type implementing Savable directly, with no distinction at the call site:
Save writes the resulting JSON to disk, overwriting any previous content, while load reads the file back and overwrites the object passed in.
Checking and removing a file
Before attempting a load on a save that might not exist yet, exist checks whether the underlying file is present on disk:
Remove deletes the file from disk:
Path and name
For logging, or for displaying available saves to the player, path and name return the file's resolved location and its original name:
std::string p = file.path(); // e.g. "data/remake2d/json/save1.json"
std::string n = file.name(); // "save1"
Save location
Every DataFile resolves its path against SaveManager's root directory, exposed through the singleton data, which defaults to "data/remake2d/", relative to the executable:
rmk::data.root("saves"); // root = saves/
std::string root = rmk::data.root();
rmk::DataFile file("save1"); // saves/json/save1.json
Changing the root only affects DataFile instances created afterwards.
Info
data.root() is also used by the Window::screenshot method to save screenshot.
Property
| Type | Copiable | Movable | Bases and Traits |
|---|---|---|---|
| DataFile | Yes | Yes | None |