Basic Arcweave tutorial | Part 10: Global variables
Learn how to define global variables and assign them values.
Video tutorial
Watch this tutorial as a video:
⚠️ This video does not reflect the latest updates to Arcweave's UI.
✅ You can play the finished version of the Tutorial Project here and explore its diagrams and structure here.
Why variables
In previous tutorials, we saw how to create choices that immediately lead to different outcomes: the player chooses A over B and reads the contents of element A and not B.
But what if you want choices that influence later parts of the game? For example, what if there is a locked door that opens only when the player finds the appropriate key? How do you check whether the player has it?
To do this, you need variables.
Variables store pieces of information in the computer's memory so you can access them later in the game.
Variable types
In Arcweave, variables can hold the following types of values:
- Integer: A whole number, e.g.
1,-200, and0. - Float: A decimal number, e.g.
5.34,1.0,-3.5, and0.0. - String: A word or sentence, like the name of a character, e.g.
Galandrielor any combination of letters, numbers, and special characters like1234%hello. - Boolean: A binary variable that can be either
trueorfalse.
Variable scopes
Variables are always present in memory and can be accessed from anywhere in your project at any time.
Arcweave supports global and scoped variables:
- Global variables: variables accessed directly by their name, e.g.
score. - Board variables: variable attributes accessed by the board custom ID and attribute custom ID, e.g.
board_id.var_id. - Component variables: variable attributes using the same
component_id.var_idsyntax.
In this tutorial we will focus on global variables.
Locked drawer mechanic
To demonstrate variables in action, we'll add a locked drawer in the kitchen and a hidden key in the bathroom.
Add the drawer
- Go to the Kitchen element of your Apartment board.
- Add a new element next to it, title it Searching cabinets, and connect the Kitchen element to it.
- Label the connection Search the cabinets.
- In the new element, write text describing what the player finds: a locked drawer with nothing else of interest.
- Add a return connection back to the Kitchen element, so the player is not stuck in the Searching cabinets element.
Add the key
- Go to the Bathroom element.
- Add a new element titled Bathroom mirrors and connect it to the Bathroom, labelling the connection Examine the mirrors.
- Add content to the mirrors element describing what the player sees: When you stand at the centre of the room, both mirrors reflect the same corner: the top part of the door.
- From the Bathroom mirrors element, add another element, give it the title Finding the key, and draw a connection to it from Bathroom mirrors. Label the connection: Examine the corner above the door.
- In the Finding the key element, add the text that describes the player finding the key: You run your fingertips along the door's top frame. You stop, as they touch something small and jagged. It's a key! You put it in your pocket.
- Add a key image or icon as element cover, as we've described in the Assets part of this tutorial.
- Connect back to the Bathroom element or, even better, use a jumper to keep the board cleaner.
Test, but...
Set the Living room as the starting element (Play Mode > Set Play Mode start), then run the project. You can navigate to the bathroom, examine the mirrors, examine the corner above the door, and find the key.
However, you'll notice a problem: you can repeat this sequence as many times as you want, finding the key over and over again.
This is because we haven't stored the information of having found the key anywhere. No matter how logical the intent, the game will not keep track of it unless we use variables.
Create a global variable
- Go to the lower-left side of the screen and click Global variables.
- Click + New variable.
- Name it
has_key. - Set the type to Boolean.
- Set the initial value to
false, since the player does not start the game with the key. - Press Esc or click outside the Global variable dialog to close it.
Now, all we need to do is use the variable in two points of the story:
- When the player finds the key, the variable will change from
falsetotrue. This is called a value assignment. - When the player tries opening the locked drawer. This will be a condition evaluation that checks whether the player has the key.
Let's see these one by one.
Assign the variable
Insert the assignment
We need the has_key variable to change from false to true when the player picks up the key.
- Go to the Finding the key element.
- Double click the element's body to edit its content, place your cursor at the end of the text, and press Return to create a new line.
- In the formatting menu, click Arcscript (the <> icon) to insert an Arcscript code segment.
- Inside the Arcscript segment, type:
has_key = true.
✅ Arcweave's auto-complete will assist you when typing Arcscript.
The element should look like this:
You run your fingertips along the door's top frame. You stop, as they touch something small and jagged.
It's a key! You put it in your pocket.
has_key = trueTest the assignment
Arcweave has a built-in debugger you can use to inspect variable values as you play through a project.
The debugger shows all variables and their values:
- Previous value: The variable's value before visiting the current element.
- Current value: The variable's value after visiting the current element (and thus having executed any code included in it).
To open the debugger:
- In the project environment, click the Play icon (Play Mode) in the top menu.
- Once in Play Mode, hover at the top of your browser window; this causes the Play Mode menu to appear.
- Click Debug, the orange bug icon at the right side of the menu.
At the start of the game, has_key shows as false. Navigate to the bathroom, examine the mirrors, examine the corner above the door, and pick up the key. The debugger panel refreshes and shows has_key is now true.
✅ The debugger is particularly useful when things don't go as expected and you need to track how variable values change as the player moves through the project.
Prepare for branching
We've now assigned the variable, but we still haven't prevented the player from repeatedly finding and refinding the key.
To do that, we need to show different content depending on the value of has_key:
- If
has_keyisfalse→ let the player find the key. - If
has_keyistrue→ show the mirror room again, but with no key to find.
To support these two outcomes, duplicate the Bathroom mirrors element and update each version:
- Copy and paste the mirrors element.
- Rename the original to something like Bathroom mirrors – first time.
- Rename the copy to Bathroom mirrors – has key.
- In the "has key" version, remove any text or connection that leads to finding the key.
What's next
You now have two separate paths ready. In the next tutorial, we will add branches to control which version the player sees based on the value of has_key.