Saving and Loading
Most games need to remember things between sessions: which level the player reached, how many coins they have, whether they found the key. Renzora gives you a simple save system for that, built around named values and save slots. This page also covers switching to another scene with a loading screen.
New to scripts? Start with Rust scripts. Every example here is a few lines inside a script.
How saving works
There are two layers:
- Values. While the game runs you keep named values, like
"level","coins"or"has_key". These live in memory. - Slots. When you want to keep them, you write the values into a slot, such as
"slot1"or"autosave". When the player continues, you load a slot back into memory.
Nothing reaches the disk until you write a slot. A value you set and never wrote is gone when the game quits, which is exactly what you want for a checkpoint system.
A save holds only what you put in it, not a copy of the whole scene. That keeps saves small, and an old save still loads after you change a level.
Setting and reading values
The save system is renzora::save. A value is a ScriptValue: a number, a whole number, true/false, text, a 2D or 3D vector, or a colour.
use renzora::ScriptValue;
// Keep values as play goes on.
renzora::save::set("level", ScriptValue::Int(3));
renzora::save::set("coins", ScriptValue::Int(120));
renzora::save::set("has_key", ScriptValue::Bool(true));
// Read one back.
if let Some(ScriptValue::Int(coins)) = renzora::save::get("coins") {
info!("The player has {coins} coins");
}
| Call | What it does |
|---|---|
save::set(key, value) | Stores a value in memory, replacing any value with that name. |
save::get(key) | The value with that name, or None. |
save::remove(key) | Forgets one value. |
save::clear() | Forgets every value, for a new game. Slots already written are not touched. |
save::keys() | Every value's name, sorted. |
Writing and loading slots
// At a checkpoint, or when the player picks "Save".
if let Err(e) = renzora::save::write("slot1") {
error!("Could not save: {e}");
}
// When the player picks "Continue".
match renzora::save::load("slot1") {
Ok(()) => info!("Loaded slot1"),
Err(e) => warn!("Could not load slot1: {e}"),
}
| Call | What it does |
|---|---|
save::write(slot) | Copies every value into the slot, replacing what was there. |
save::load(slot) | Replaces the values in memory with the slot's. If the slot cannot be read, nothing changes. |
save::exists(slot) | Whether the slot has a save. Handy for greying out "Continue". |
save::delete(slot) | Removes a slot. Removing one that does not exist is fine. |
save::list() | Every slot, most recently saved first, each with its name and saved_at time (seconds since 1970). |
Slot names may use letters, digits, - and _, up to 64 characters. "slot1", "autosave" and "chapter-2" are fine; "my save" is not.
Writing is safe against crashes: the new save is written to a temporary file first and only then swapped in, so a crash or a full disk in the middle leaves the previous save intact. A damaged save file is left out of save::list() instead of hiding every other slot.
A tiny autosave script
Put this on any entity in a level's scene. When the level starts, the game remembers which level the player reached and writes an autosave.
use bevy::prelude::*;
use renzora::{ScriptCtx, ScriptHook, ScriptValue};
/// Change this in each level's copy of the script.
const LEVEL: i32 = 2;
fn update(_ctx: &mut ScriptCtx) {}
fn hooks(_ctx: &mut ScriptCtx, hook: &ScriptHook) {
match hook {
// Runs once, when the script starts.
ScriptHook::Ready => {
renzora::save::set("level", ScriptValue::Int(LEVEL));
if let Err(e) = renzora::save::write("autosave") {
error!("Autosave failed: {e}");
}
}
_ => {}
}
}
renzora::script!(update, hooks = hooks);
Where saves go
Each slot is one <slot>.json file in a saves folder named after your game (your project's name), in the player's own data folder:
| Platform | Folder |
|---|---|
| Windows | %APPDATA%\<game>\saves |
| macOS, iOS | ~/Library/Application Support/<game>/saves |
| Linux | $XDG_DATA_HOME/<game>/saves, or ~/.local/share/<game>/saves |
| Android | the app's own data folder, in saves |
| Web | no folder: saves last until the browser tab closes |
Saves live outside the game's install folder, so updating the game never wipes them.
Pressing Play in the editor uses the same folder your players will. A save you make while testing is right there for checking your Continue button. Delete the folder (or call save::delete) to test a fresh start.
Switching scenes with a loading screen
To move to another scene, load it as a load group. The engine loads the scene's assets in the background, tells you how far it has got, and switches when it is ready. The tools are in renzora::assets.
use renzora::assets::{AssetLoads, SceneActivation};
// Go to level 2 as soon as its assets are in.
if let Some(mut loads) = ctx.get_resource_mut::<AssetLoads>() {
loads.load_scene("level2", "scenes/level2.bsn", SceneActivation::WhenReady);
}
With SceneActivation::Manual the scene loads but waits for you. That is how you keep a title screen up until the player presses Start:
// On the title screen: start loading in the background.
loads.load_scene("level1", "scenes/level1.bsn", SceneActivation::Manual);
// Later, when the player presses Start: switch now, or the moment it is ready.
loads.activate("level1");
To draw a progress bar, read how far a group has got. fraction goes from 0 to 1:
if let Some(loads) = ctx.get_resource::<AssetLoads>() {
let done = loads.fraction("level1");
let ready = loads.is_ready("level1");
// Set your bar's width from `done`, show "Press Start" when `ready`.
}
You can also preload any files, not just scenes, so they are ready before they are needed:
loads.preload("boot", &["audio/music/theme.ogg", "art/ui/"]);
A path ending in / stands for every asset in that folder. A file that fails to load counts as done, so a missing file never leaves a loading screen stuck.
| Call | What it does |
|---|---|
load_scene(group, path, activation) | Loads a scene's assets, then switches to it. |
activate(group) | Switches to a Manual scene load. |
preload(group, paths) | Loads a list of files and folders. |
pause / resume / stop (group) | Controls a group's loading. |
unload(group) | Lets the group's assets go. Anything still in use stays loaded. |
fraction(group), is_ready(group), progress(group) | How loading is going. |
A request takes effect at the start of the next frame, so a group's progress changes one frame after you ask.
The current scene's entities, and their scripts, are removed when the new scene arrives. A loading screen that must survive the switch and hear the ScriptHook::SceneLoaded event belongs in a scene that stays loaded the whole game. See Rust scripts (engine) for the full list of hooks.