Saving games
A save holds what you mark as persistent, not the whole scene. Loading a save reloads its scene from the project and then applies the save on top.
In Try it, the Checkpoint script saves after one second and then loads that save every few seconds. The green ball has Persist and comes back to where it was saved; the grey one has not and starts over from the scene file. In Try it saves stay in memory and are gone when the page closes.
| Mark | What is saved |
|---|---|
save.Persist component |
the node's transform and enabled flag |
sdk.Persist("field", …) on a component type |
those fields of every such component |
@persist on a script field |
that field of every script instance |
OnSave() / OnLoad() |
values the script writes with save.Put* |
Besides that a save remembers which scene nodes were destroyed and every object made with scene.Spawn: its prefab, parent, pose and persistent data.
@persist
var Coins int
func OnSave() {
save.PutVec2("checkpoint", lastCheckpoint)
}
func OnLoad() {
lastCheckpoint = save.GetVec2("checkpoint", node.Position())
}
| Function | |
|---|---|
save.Save(slot) |
write the game to a slot at the end of the frame |
save.Load(slot) |
reload the saved scene and restore the slot |
save.Checkpoint() |
save to the slot checkpoint |
save.Autosave(seconds) |
save to autosave every given number of real seconds |
save.Exists(slot), save.Delete(slot) |
|
save.SetGlobalInt(key, v), save.GlobalInt(key, def) |
game-wide progress carried by every save; also Float, Bool, String |
Slot names use letters, digits, - and _. Saves live next to the player settings, in saves/ of the profile.
Try it
Change the example parameters and compare the result. Reset restores the initial scene.
Scene6 nodes
Scriptmain.aes
package demo
import "aego/save"
// Checkpoint saves once, then keeps loading that save: the green ball has Persist and comes back
// to where it was saved, the grey one has not and starts over from the scene file.
script Checkpoint {
@export var SaveAt float = 1
@export var LoadEvery float = 2.5
var t float
func Update(dt float) {
t += dt
if !save.Exists("demo") {
if t >= SaveAt {
save.Save("demo")
t = 0
}
return
}
if t >= LoadEvery {
save.Load("demo")
}
}
}
- Loading order Loading a save rebuilds the game in a fixed order.
- Modules A module saves its own state with a section: