Whimsy

Using variabes and conditionals in scenarios

Variables, combined with different blocks, allow you to make your game more dynamic. Together, they allow you to track progress on what a player has done and implement advanced mechanics with that, like quests or puzzles.

Tip

Check out our tutorial on making a simple quest to see how the concepts discussed here are applied in practice: Making a simple fetch quest.

Understanding Variables

Variables in Whimsy are like little storage boxes for information that your game needs to remember. They can store three types of data:

  • Numbers — for counting things like collected items, points, or tracking how many times something has happened.
  • Text — for storing character names, messages, or any text content you want to use later.
  • Yes/No (aka Boolean values) — for tracking states like whether a door is locked, a quest is complete, or a choice was made.

Variables have a name, their type, their initial value, and, in-game, their current value. For example, you can have a variable money with type "Number" and a starting value of 0 (tough life!), and you can increase this value in-game with scenarios.

There are two ways you can access and manage your variables:

  • You can open the Settings tab;
  • Or you can open any actor's scenario and press the "Manage Variables" button in the block library on the right side, in the Data section

You can add new variables by clicking the "Add variable" button.

Variable Blocks

Whimsy provides several blocks for working with variables, found in the "Data" category:

  • Read variable — Gets the current value of a variable so you can use it in other blocks.
  • Write variable — Sets a variable to a specific value, replacing whatever was there before.
  • Change a variable — Modifies a number variable using operations like addition, subtraction, or multiplication.

Working with Conditionals

Conditionals let your game make decisions based on variable values or other conditions. The main conditional block in Whimsy is the "Branch" block (found in the "Execution Flow" category), which works like an if-else statement in programming. This block has an input for a Condition, and it is a Yes or No value that executes the corresponding outcome of this block. To get this branch working, you either have to connect a variable with a "Read variable" block, or compute this "Yes/No" values with other blocks. For example, this can be done with these blocks:

  • Comparison — Compare two numeric values (like checking if a variable is greater than, equal to, or less than another value).
  • Pick a logic value — Pick A or B depending on the third value. Similar to the Branch itself.

Practical Example: Keys and Doors

Let's create a simple collection mechanic where the player can collect and spend keys to unlock doors. Once a key is used, it vanishes with a door — unrealistic and a too gamey approach, but a great way to create puzzles!

The Key actor

  1. Create a number variable called "key count" with a starting value of 0
  2. For a key actor, create this scenario:
    • Start with a "When touched by a hero" event
    • Connect it to a "Change a variable" block
      • Set Variable (A) to key count
      • Set Operation to "+"
      • Set Value (B) to 1
    • Connect to a "Say a line" block with text:
      You found a key! You now have ${key count} of them.
      
    • Connect to a "Destroy themself" block

The Door actor

For a door that requires the keys collected above:

  1. Create a door actor with this scenario:
    • Start with a "When touched by a hero" event.
    • Connect to a "Branch" block.
    • Connect a "Comparison" block to the Branch's condition input.
      • Link the first value (A) to a new block Read Variable, with variable being key count.
      • Set comparison to ">=" (greater than or equal to).
      • Set second value to 1.
    • From the "Yes" output, connect:
      • A "Say a line" block with text: "You unlock a door with a key! As by magic, both disappear."
      • Then "Change a variable" that changes key count, but this time, it will subtract 1.
      • Then a "Destroy themself" block. (This will delete a door).
    • From the "No" output, connect:
      • A "Say a line" block with text: "This door is locked, and you don't have any keys."

Data pickers

For data, there exist "data pickers" with blocks named "Pick a number", "Pick a string", and "Pick a logic value". They both behave similarly and allow you to pass A if condition is true, and B if it is not. It is similar to ternary operators in programming languages.

Here is an example where a player will pay only 40 coins for a key instead of 50 if they have good reputation among traders:

Tips for Using Variables and Conditionals

  • Name variables clearly — Use descriptive names like "key count" instead of just "k" or "adwahwfuwripgjeteotkgerhu".
  • Start simple — Begin with basic counters and branches before building complex logic.
  • Test frequently — Play your game often to check if your variables and conditionals work as expected. Use the Tools pane on the right to change your variables' values on the fly to skip manual labor.
  • Use text templates — Incorporate variables into your dialogue with ${variableName} to make your game feel responsive
  • Remember initial values — Set appropriate starting values for your variables (like 0 for counters and, say, 5 for lives.)