aego/save
Save and Load take effect at the end of the frame. Put* belong in OnSave and Get* in OnLoad; their keys belong to the node. SetGlobal* values are carried by every save. The documentation sandbox has no saves, so this example needs a project. See Saving games.
Example
package demo
import (
"aego/log"
"aego/save"
)
script Checkpoints {
var reached int
var frames int
func Ready() {
log.Info("runs so far " + string(save.GlobalInt("runs", 0)) + ", slot1 exists: " + string(save.Exists("slot1")))
save.SetGlobalInt("runs", save.GlobalInt("runs", 0) + 1)
}
func Update(dt float) {
frames++
if frames == 30 {
reached = 2
node.SetPosition(Vec2{5, 0})
save.Save("slot1")
log.Info("saved at checkpoint " + string(reached))
}
if frames == 31 {
log.Info("slot1 exists: " + string(save.Exists("slot1")))
}
}
func OnSave() {
save.PutInt("reached", reached)
save.PutVec2("respawn", node.Position())
}
func OnLoad() {
reached = save.GetInt("reached", 0)
node.SetPosition(save.GetVec2("respawn", Vec2{}))
log.Info("loaded checkpoint " + string(reached))
}
}
Functions
| Autosave | (seconds float) | Saves to the slot named autosave every given number of real seconds; 0 turns it off. |
| Checkpoint | () | Saves the game to the slot named checkpoint. |
| Delete | (slot string) | Removes a save and its preview. |
| Exists | (slot string) bool | Reports whether a slot holds a save. |
| GetBool | (key string, def bool) bool | Reads a value this node stored, or def; it works in OnLoad. |
| GetFloat | (key string, def float) float | Reads a value this node stored, or def; it works in OnLoad. |
| GetInt | (key string, def int) int | Reads a value this node stored, or def; it works in OnLoad. |
| GetString | (key string, def string) string | Reads a value this node stored, or def; it works in OnLoad. |
| GetVec2 | (key string, def Vec2) Vec2 | Reads a value this node stored, or def; it works in OnLoad. |
| GlobalBool | (key string, def bool) bool | Reads game-wide progress, or def. |
| GlobalFloat | (key string, def float) float | Reads game-wide progress, or def. |
| GlobalInt | (key string, def int) int | Reads game-wide progress, or def. |
| GlobalString | (key string, def string) string | Reads game-wide progress, or def. |
| Load | (slot string) | Reloads the saved scene and restores the slot at the end of the frame. |
| PutBool | (key string, value bool) | Stores a value for this node; call it in OnSave. |
| PutFloat | (key string, value float) | Stores a value for this node; call it in OnSave. |
| PutInt | (key string, value int) | Stores a value for this node; call it in OnSave. |
| PutString | (key string, value string) | Stores a value for this node; call it in OnSave. |
| PutVec2 | (key string, value Vec2) | Stores a value for this node; call it in OnSave. |
| Save | (slot string) | Writes the game to a slot at the end of the frame. |
| SetGlobalBool | (key string, value bool) | Stores game-wide progress that every save carries. |
| SetGlobalFloat | (key string, value float) | Stores game-wide progress that every save carries. |
| SetGlobalInt | (key string, value int) | Stores game-wide progress that every save carries. |
| SetGlobalString | (key string, value string) | Stores game-wide progress that every save carries. |
Autosave function
func Autosave(seconds float)
Saves to the slot named autosave every given number of real seconds; 0 turns it off.
See the module example for this function in use.
Checkpoint function
func Checkpoint()
Saves the game to the slot named checkpoint.
Example
save.Checkpoint()
Delete function
func Delete(slot string)
Removes a save and its preview.
See the module example for this function in use.
Exists function
func Exists(slot string) bool
Reports whether a slot holds a save.
See the module example for this function in use.
GetBool function
func GetBool(key string, def bool) bool
Reads a value this node stored, or def; it works in OnLoad.
See the module example for this function in use.
GetFloat function
func GetFloat(key string, def float) float
Reads a value this node stored, or def; it works in OnLoad.
See the module example for this function in use.
GetInt function
func GetInt(key string, def int) int
Reads a value this node stored, or def; it works in OnLoad.
See the module example for this function in use.
GetString function
func GetString(key string, def string) string
Reads a value this node stored, or def; it works in OnLoad.
See the module example for this function in use.
GetVec2 function
func GetVec2(key string, def Vec2) Vec2
Reads a value this node stored, or def; it works in OnLoad.
See the module example for this function in use.
GlobalBool function
func GlobalBool(key string, def bool) bool
Reads game-wide progress, or def.
See the module example for this function in use.
GlobalFloat function
func GlobalFloat(key string, def float) float
Reads game-wide progress, or def.
See the module example for this function in use.
GlobalInt function
func GlobalInt(key string, def int) int
Reads game-wide progress, or def.
Example
kills := save.GlobalInt("kills", 0)
GlobalString function
func GlobalString(key string, def string) string
Reads game-wide progress, or def.
See the module example for this function in use.
Load function
func Load(slot string)
Reloads the saved scene and restores the slot at the end of the frame.
Example
if save.Exists("slot1") {
save.Load("slot1")
}
PutBool function
func PutBool(key string, value bool)
Stores a value for this node; call it in OnSave.
See the module example for this function in use.
PutFloat function
func PutFloat(key string, value float)
Stores a value for this node; call it in OnSave.
See the module example for this function in use.
PutInt function
func PutInt(key string, value int)
Stores a value for this node; call it in OnSave.
See the module example for this function in use.
PutString function
func PutString(key string, value string)
Stores a value for this node; call it in OnSave.
See the module example for this function in use.
PutVec2 function
func PutVec2(key string, value Vec2)
Stores a value for this node; call it in OnSave.
See the module example for this function in use.
Save function
func Save(slot string)
Writes the game to a slot at the end of the frame.
Example
save.Save("slot1")
SetGlobalBool function
func SetGlobalBool(key string, value bool)
Stores game-wide progress that every save carries.
See the module example for this function in use.
SetGlobalFloat function
func SetGlobalFloat(key string, value float)
Stores game-wide progress that every save carries.
See the module example for this function in use.
SetGlobalInt function
func SetGlobalInt(key string, value int)
Stores game-wide progress that every save carries.
See the module example for this function in use.
SetGlobalString function
func SetGlobalString(key string, value string)
Stores game-wide progress that every save carries.
See the module example for this function in use.