{"id":"3d96769b-e06f-4957-a77e-6eab38b8fd1e","title":"Variables and Rules","slug":"playbooks/variables-and-rules","content":"Part of [Playbooks](/wiki/playbooks).\n\nVariables are the status. Rules are what change that status every turn.\n\nIf you only remember one thing from this page: set up your variables first, then write rules that update them. Lorebooks and advanced stuff come after that.\n\n## Variables\n\nYou start by defining variables — up to 15. Each one is either:\n\n- **Yes / no** (boolean), like `is_married`\n- **A number**, like `trust_level` from 0 to 100\n- **Short text**, like `location`\n\nAll of these are available to the LLM so it knows the current status of the chat. Some people call this the HUD or chat state.\n\nYou can also write plain-English descriptions for each value, so the LLM sees something nicer than raw `true` or `42`. For numbers, you can set ranges like \"0-10 strangers\", \"11-50 friends\", and so on.\n\nIf a variable is only for bookkeeping and you don't want it in the visible status line, you can hide it from the story status. It still works for rules and for the LLM.\n\n## Variable IDs and display names\n\nEach variable has a stable `id` and a display `name`. State, rule targets, lore flip conditions, and starting-scenario values refer to the ID. Luau expressions and scripts also use IDs, for example `param.trust_level` or `state.trust_level`.\n\nKeep variable IDs stable when you want values to carry between Playbooks. Display names can change independently. An ID of `trust_level` can have a display name of \"Trust\" or \"Relationship trust\" without breaking the match. Giving two different IDs the same display name does not connect their values.\n\nStored values are normalized against the active Playbook's variable definitions: missing or incompatible values use defaults, and numeric values may be clamped to the current bounds. Reusing an old ID can recover a retained value, so use a new ID for a different piece of state. Inactive values are subject to the [50-entry state bag limit](/wiki/playbooks/using-multiple-playbooks#what-happens-to-the-old-playbook-data).\n\n## Starting scenarios\n\nYou can set different starting values based on which companion starting message / scenario the chatter picks.\n\nIn the easiest playbook, it can just be:\n\n- Scenario 1: not married, relationship level 0\n- Scenario 2: married, relationship level 100\n\nThe first scenario uses your default values. Extra scenarios are an ordered list that maps onto starting messages 2, 3, and so on. If a scenario doesn't set a variable, that variable keeps its default.\n\n## Rules\n\nEvery turn, rules can update variables. You can have up to 15 rules across both phases; at most three of those can be LLM conditionals, and at most three can be code rules. They run in two phases:\n\n1. **Before the reply.** Update status so the LLM can read it while writing.\n2. **After the reply.** Update status based on what just happened in the reply (and what the user said).\n\nA simple way to think about it:\n\n- Before = set the stage\n- After = record what changed\n\n## When a rule runs\n\nA rule only runs when its condition matches. Basic conditions look at things like:\n\n- Variable values (trust is above 50, married is true)\n- How long the chat is / message checks\n- Chance (roll a percentage each time)\n\n## What a rule can do\n\nWhen a rule fires, it updates variables. Common options:\n\n- Set a value\n- Increase or decrease a number\n- Flip a yes/no\n- Pick something random (a number, a string from a list, and so on)\n\nRandom rolls happen fresh every time. Re-running a turn can produce a different result. That's intentional.\n\nFor calculated values or full branching logic, see [Advanced Rules](/wiki/playbooks/advanced-rules).\n\n## LLM conditionals\n\nFor fuzzy judgment calls that are too hard for basic checks, you can add an LLM conditional. Example: \"If the player was hostile, decrease trust by 1-5.\"\n\nThat makes the playbook a premium playbook. When any of those actually run, it costs one extra utility credit for that generation, including regenerations.\n\nEvery LLM conditional should have a normal fallback. Chatters can also turn conditionals off on the chat; when that happens, the fallback is used instead.\n\n## Tips\n\n- Put \"set the stage\" rules before the reply.\n- Put \"react to what was said / written\" rules after the reply.\n- Keep variable IDs stable when you want values to carry between Playbooks. Display names can change independently; rules and lore flips reference IDs.\n- Prefer a small set of meaningful variables over a giant pile of flags.\n\n## Related\n\n- [Dynamic Lorebook Context](/wiki/playbooks/dynamic-lorebook-context): use variables to reveal lore\n- [Advanced Rules](/wiki/playbooks/advanced-rules): Luau expressions and scripts (gets technical)\n- [Playbooks](/wiki/playbooks): overview\n","access_level":"public","category_id":"ee78c80a-866a-4d2e-84d4-ff69a8ade8c1","display_order":11,"keywords":["variables","rules","playbook","status","turn","two-phase"],"description":"Set up playbook variables first, then write the rules that update them.","created_at":"2026-08-07T16:54:55.47053Z","updated_at":"2026-09-21T16:02:48.385351Z"}