Soniq Variables: Adding Logic to Interactivity
Think of Variables as the "memory" of your interactive book. Normally, a pen just plays a sound when you touch a picture. With variables, the pen can remember what you did before and change its behavior accordingly.
Types of Variables
- Toggle (Yes/No): Use this for things that are either true or false.
- Examples: "has_key", "lights_on", "game_started".
- Counter (Numbers): Use this for things you need to count.
- Examples: "coins", "current_player".
How to Use Them
You interact with memory using two main tools in the Studio:
- Modify Variable: This "writes" to the pen's memory (e.g., "The player just picked up the key, so set has_key to true").
- Conditional Block: This "reads" the memory and asks a question (e.g., "IF the player has_key, THEN play the 'Door Opening' sound").
Example: The Locked Treasure Box
In this example, the child must find a hidden key before they can open a treasure box.
Step 1: Create the Variables
Go to Project Settings > Logic & Variables and click Add Variable:
- Name:
has_key - Type: Toggle (Boolean)
- Initial Value: No (False)
Step 2: Hide the Key (The "Old Oak Tree")
Find the picture of the tree where the key is hidden. In the Region Editor, add these actions:
- Play Audio: "You look under the roots and find a rusty old key!"
- Modify Variable: Set
has_keyto true.
Step 3: Create the Box Logic
Find the picture of the treasure box. We need two "Conditional Blocks" to handle both cases:
Case A: They don't have the key yet
- Add a Conditional Block:
IF has_key is false - Inside the block, add Speak Text: "The box is locked tight. Maybe there is a key hidden nearby?"
Case B: They have the key!
- Add a Conditional Block:
IF has_key is true - Inside the block, add Speak Text: "The key fits! You turn it and the box creaks open to reveal... gold coins! Now turn to next page."
How it works for the child:
- If they touch the Box first, the pen "checks" its memory, sees
has_keyis false, and tells them it's locked. - Once they touch the Tree, the pen "writes" to its memory that they now have the key.
- Once they touch the Box again, the pen "checks" again, sees
has_keyis now true, and plays the success sound!
Example: Dice Roll & Randomness
You can use variables to create games of chance, like a board game with a digital dice.
Step 1: Create the Dice Variable
Go to Project Settings > Logic & Variables:
- Name:
dice - Type: Counter (Number)
- Max value: 6
Step 2: The "Roll the Dice" Action
Find the picture of a dice in your book. In the Region Editor, add these actions:
- Modify Variable:
- Variable:
dice - Operation: Set (=)
- Value:
random(1,6)(This tells the pen to pick a random number between 1 and 6)
- Variable:
- Speak Text: "Dice rolled to {dice}." (The pen will automatically announce the number it just picked!)
How it works:
- The Random Command: When the pen sees
random(1,6), it uses its internal high-speed timer to pick a number. - Speaking the Number: Soniq handles the hard work of making the pen "speak" the variable. When you use
{dice}in a text, Soniq pre-generates the audio for every possible outcome (1, 2, 3, etc.) so the pen always says the correct result.