Saving and scenes

Saved games

A save is a Lua table: numbers, booleans, text, Vec3 values and tables of those.

Lua
TypeScript
SaveData.Save("Slot 1", {
	Level = 3,
	Position = self.Entity.Position,
	Inventory = { "Key", "Lamp" },
})

local save = SaveData.Load("Slot 1")
if save then
	self.Entity.Position = save.Position
end

Entities and functions cannot be saved. Save what is needed to make them again.

SaveData.Exists, SaveData.Delete and SaveData.List round it out. A save's name is letters, digits, spaces, dashes and underscores.

Where saves go

Each game has a folder of its own in the user's data directory, named after the project:

SystemFolder
macOS~/Library/Application Support/<Project name>
Windows%APPDATA%\<Project name>
Linux~/.config/<Project name>

Pass --user-data DIR to the editor or the runtime to use another folder.

Player settings

Settings.Save() keeps the bus volumes, the key bindings and the window size for the next run. Settings.Reset() goes back to the project's own.

Lua
TypeScript
Settings.SetResolution(1920, 1080, false)
Audio.SetVolume("Music", 0.4)
Settings.Save()

Switching scenes

Lua
TypeScript
Scene.Load(Assets.Find("Scenes/Level2.cairnscene"))   -- replace the scene
Scene.LoadAdditive(menu)                            -- add a scene's entities
Scene.Unload(menu)                                  -- take them away again
Scene.Load(Scene.GetCurrent())                      -- start the level over

A switch happens at the end of the frame, never in the middle of the script that asked.

Keeping things across scenes

Tick Persistent on a top-level entity and it stays through Scene.Load, with its children, its scripts and their values. Use it for a player, a score keeper, or music that plays on.

An entity that stayed is not made a second time when a scene that contains it is loaded again.