Skip to content
Download

Saving data

Save keeps values under a name, and they are still there when the game is closed and opened again.

Save::set("recorde", pontos);
int recorde = Save::getInt("recorde");
Function What it does Example
void Save::set(std::string_view key, value) Stores a value: int, float, bool or text. Brasa writes it to disk by itself, on the same frame. Save::set("volume", 0.5f);
int Save::getInt(std::string_view key, int fallback = 0) The stored integer, or fallback if there is none. Save::getInt("recorde")
float Save::getNumber(std::string_view key, float fallback = 0.0f) The stored number. Save::getNumber("volume", 1.0f)
bool Save::getBool(std::string_view key, bool fallback = false) The stored yes/no. Save::getBool("tem_espada")
std::string Save::getText(std::string_view key, std::string_view fallback = {}) The stored text. Save::getText("nome", "Jogador")
bool Save::has(std::string_view key) true if there is a value with that name. if (Save::has("recorde")) ...
void Save::remove(std::string_view key) Erases one value. Save::remove("tem_espada");
void Save::clear() Erases everything (a “new game”). Save::clear();
void Save::set(std::string_view key, const std::vector<int>& values) Stores a whole list (also of float and of std::string). Save::set("fases", std::vector<int>{1, 2, 5});
std::vector<int> Save::getInts(std::string_view key) The stored list (getNumbers and getTexts for the other types); empty if there is none. for (int fase : Save::getInts("fases")) ...
bool Save::setJson(std::string_view key, std::string_view json) and std::string Save::getJson(std::string_view key) Any value written in JSON (lists inside lists, tables). It is how Lua scripts store tables. Save::setJson("mapa", "[[1,0],[0,1]]");
  • The file lives in %APPDATA%\Brasa\Jogos\<game name>\save.json, outside the game folder: it is still there when the game is updated.
  • The game name is the one in the Project settings. Changing the name starts a new file.
  • To pass a value from one room to another without writing to disk, see Keeping data between rooms.