Put durable state in the World Engine’s data dictionary
Start in a practice story where you can edit World Engine code. Deep Realms runs Python around the generation of a continuation. Code outside the hooks initializes a story. Code in pre_generate() runs before the narrator writes, while post_generate() can inspect the resulting text. Use the earlier hook for a rule the narrator needs to know before describing the outcome.
For a first rule, choose a state with two possible values, such as unused and used. The engine provides a data dictionary that accepts JSON-compatible values. Give the value a clear key and supply its default only when that key is absent. Unconditionally restoring the default whenever initialization runs would make an old, used resource available again after an editor change. A string key and an integer are sufficient for this demonstration.
Match ui.action to a defined choice before changing the value
A ui.Choice has a title the player sees and an action the engine receives. Decide which submitted action changes the state. Exact matching is useful for a transaction: asking about a resource or mentioning it in conversation should leave the balance alone. Testing the old value as well as the action makes a repeated request harmless. An author who wants several valid phrasings can map those explicitly instead of searching every sentence for a keyword.
The example below fits in a fresh practice world. It uses a pass because a reader can verify its two states without first building an inventory or currency system. Submit an ordinary action to run the hook, then use the displayed choice. If adding this to an existing project, merge the relevant lines into its hook because a second Python definition with the same name replaces the first.
Send the stored result to both Story Information and the narrator
Assigning a string to ui.story_info inside a list gives the player a record they can inspect through Story Information. Passing the same underlying fact to ai.enforce supplies a narrative instruction. The distinction matters when prose sounds confident: a character describing a transaction cannot change the dictionary unless the script explicitly lets it. When a sentence contradicts the hint, inspect the stored result before editing the arithmetic.
A post_generate() check can be useful for outcomes decided by the generated text. Deep Realms also offers ai.check(), which asks the model to evaluate a statement in context. That introduces interpretation. Reserve it for events where a range of natural-language outcomes should count. An exact choice is easier to audit for a finite resource.
Verify a retry and reinitialization against the same saved state
| Operation | Expected state | Inspect if it differs |
|---|---|---|
| Start a fresh story and submit a neutral action | Unused, with a choice available | Default initialization and key spelling. |
| Submit the exact defined choice | Used, with the receipt in Story Information | The submitted action and its matching condition. |
| Retry the continuation or submit the same action again | Used, with only one grant | Whether the old value is checked before updating. |
| Reinitialize the edited practice code | The previous state survives | The data dictionary and its guarded default. |
Use Save to update hooks while preserving current in-memory values. Reinitialize runs setup again. The guarded initialization above retains the existing dictionary entry when that happens. A fresh story should receive the original unused state. Test all three paths before copying this rule into a larger project, and inspect Story Information after each action.
The engine exposes ui.redoing_generation to distinguish a redo from an ordinary submission when a design needs different behaviour. A one-way transition is especially useful here: once the value has changed, processing the request again should have no second charge to apply. A design that deducts an arbitrary amount on every call will need more careful transaction handling than this two-state example.
Sources & further reading
- Deep Realms World Engine reference deeprealms.io
- Deep Realms getting-started tutorial deeprealms.io
- Arcanum RPGs’ November replay of Deep Realms arcanumrpgs.com