pyxen.storage
Persist data across play sessions. Save high scores, settings, progress, or any value that should survive when the game is closed and reopened.
storage.save("high_score", 9500)
storage.save("player_name", "Alice")
storage.save("sound_on", True)
score = storage.load("high_score") # 9500
name = storage.load("player_name") # "Alice"
missing = storage.load("no_key") # None
Values are stored as int, float, string, or bool. Other types (lists, dicts, objects) are not supported.
How it works
On iPad & Mac, storage is saved as storage.json inside the .pyxen project file. The data travels with the project — if you share the file, the saved data goes with it.
On the web player, storage uses the browser’s localStorage, keyed by game.
Storage is written when the game stops. You don’t need to flush manually.
Methods
| Method | Returns | Description |
|---|---|---|
save(key, value) | None | Save a value with the given key |
load(key) | value or None | Load a value, or None if the key doesn’t exist |
has(key) | bool | Check if a key exists |
delete(key) | None | Remove a key |
clear() | None | Remove all keys |
save(key, value)
storage.save("score", 100)
storage.save("name", "Alice")
storage.save("ratio", 3.14)
storage.save("completed", True)
Save a value with the given string key. If the key already exists, its value is overwritten.
Supported types:
| Type | Example |
|---|---|
int | storage.save("score", 100) |
float | storage.save("speed", 2.5) |
str | storage.save("name", "Alice") |
bool | storage.save("muted", False) |
Passing any other type (list, dict, etc.) raises a TypeError.
load(key)
score = storage.load("high_score")
Returns the value associated with the key, or None if the key doesn’t exist.
The returned value has the same type it was saved with — an int comes back as an int, a string as a string.
score = storage.load("high_score")
if score is None:
score = 0 # first time playing
has(key)
if storage.has("high_score"):
log("Welcome back!")
Returns True if the key exists, False otherwise.
delete(key)
storage.delete("old_setting")
Removes a key from storage. Does nothing if the key doesn’t exist.
clear()
storage.clear()
Removes all keys from storage. Use with care.
Examples
High score
from pyxen import *
best = storage.load("best") or 0
score = 0
def update():
global score, best
score += 1
if score > best:
best = score
storage.save("best", best)
Settings
from pyxen import *
sound_on = storage.load("sound") is not False
def update():
global sound_on
if input.keyboard.m.pressed:
sound_on = not sound_on
storage.save("sound", sound_on)
Level progress
from pyxen import *
level = storage.load("level") or 1
def next_level():
global level
level += 1
storage.save("level", level)