Ren'Py State: define, default, and persistent
Pick the right Ren'Py state layer so affection rewinds with rollback while gallery unlocks survive a new game.
04 Oct 2025, 14:09 UTC

The wrong storage layer breaks saves and rollback
You finish a romance route. Affection rises when the player chooses kind dialogue, and a gallery image unlocks after the true ending. A tester saves mid-route, loads the file, rolls back a choice, and starts a new game. Suddenly the affection counter does not rewind, or the gallery unlock disappears when a new game begins. The problem is usually not the dialogue. It is where the value was stored.
In Ren'Py, state should be classified by lifetime before you write branches. Use define for constants, default for per-playthrough variables, and persistent for facts that outlive a single save. Mixing them up makes saves lie, rollback skip state, and new games forget unlocks.
define: fixed at init time
define binds a name to a value when the game initializes. It is right for constants such as MAX_AFFECTION = 3, character metadata, or UI colors. It is not a mechanism for per-save state. A define value is identical in every save slot and every playthrough, and mutating it at runtime does not give you rollback-safe per-run data.
default: one playthrough, one save slot
default creates a variable that starts fresh when a player begins a new game. Ren'Py captures default variables inside each save file, restores them on load, and rewinds them during rollback. Rollback is the engine's ability to rewind script-visible state to an earlier point. That makes default the normal home for affection, flags, inventory, and route position.
persistent: across replays
persistent lives outside any single save slot. A value written there survives starting a new game, and Ren'Py does not rewind it when the player rolls back. Use it for gallery unlocks, seen endings, and other meta progression. Do not use it for route-local affection, because rollback should be able to undo that choice.
Worked example: affection, ending, and gallery
Put the declarations in your project's game/script.rpy:
define MAX_AFFECTION = 3
default affection = 0
default met_true_ending = False
persistent.gallery_unlocked = False
Track affection during a choice:
menu:
"Be kind":
$ affection += 1
jump scene_two
"Stay distant":
jump scene_two
Set the irreversible unlock at the ending:
label true_ending:
"You reached the true ending."
$ met_true_ending = True
if affection >= MAX_AFFECTION:
$ persistent.gallery_unlocked = True
return
Reveal the CG from a gallery screen:
screen gallery():
if persistent.gallery_unlocked:
textbutton "Unlocked CG" action Show("cg")
else:
textbutton "Locked" action None
Affection is default, so saves, loads, and rollback handle it. persistent.gallery_unlocked is written once at the ending and survives a new game. Rollback before the ending will not unset it, which is the intended behavior for an irreversible unlock.
Comparison by lifetime
| Layer | Scope | Saved per slot? | Rewound by rollback? | Survives new game? |
|---|---|---|---|---|
| define | init-time constant | No | N/A | Yes, same value |
| default | one playthrough | Yes | Yes | No |
| persistent | cross-playthrough | No | No | Yes |
Limitation: persistent is convenience state, not proof
persistent lives in local platform storage. A determined user can delete or edit it, and it is shared across save slots rather than isolated per slot. Treat it as player-facing progress, not authoritative entitlement or DRM. Ren'Py also cannot roll back real-world side effects such as writing files or network calls; gate those behind explicit end-of-route actions or player confirmation.
Verify before you branch widely
Build a disposable project with the declarations above. Play through, save, load, roll back a choice, then start a new game and open the gallery. Confirm that affection rewinds with rollback, that the save file carries the affection value, and that the gallery unlock remains after a new game. Inspect the project's save and persistent files in a disposable copy to see which values are per-slot and which persist across replays. Exact file locations and launcher behavior vary by Ren'Py release and platform, so check the documentation for your installed version before making version-specific claims.
Closing rule of thumb
One run means default. Constant means define. After a new game means persistent. Prototype the save-load-rollback path early, before you have dozens of branches depending on the wrong layer.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.