If you've been using arcscript, Arcweave's scripting language, you've also used variables to keep track of game state, i.e. things like game score, player health, or the number of orcs guarding the gates of Mordor.
For quite a while, global variables have been the only ones available in Arcweave. Recently, scoped variables made their appearance: the board variables first, followed by component variables soon after.
This blog post is here to guide you through the use of board and component variables, so you get a clear idea of how they work and when to use them.
Global variables are best used to store a value that should be available throughout your entire project. Score, for example, fits the description.
To manage your story's score, create a global variable called score (type: integer) and increment it accordingly, as your story unfolds.
To create your score variable:
0.Arcweave validates the initial value against the selected type, so, for example, you can't enter a string where an integer is expected.
Once you've created it, you can access a global variable via arcscript, from anywhere in the project, simply by using its name. For example:
if score < 100 ...
For more details on global variables, see the Documentation.
Sometimes, a variable doesn't reflect a global value. It could be an attribute belonging to a character only, like the following examples:
health or gold (i.e. an integer or float).is_hungry (i.e. a boolean).Yardbird (i.e. a string).Arcweave components can carry attributes, and you probably knew that already. What's new, though, is that component attributes can now work as variables, hence we can also call them component variables.
Let's see how you can create a health variable for your player character:
Create a new component and name it "Player."
In the component's modal, under the component's name, notice its component custom ID, in snake_case. This by default will be player, but you can change it if you like.
Hover over the custom id, to read a summary of how to use it.
From the menu, select # Number > Integer.
10.Notice that the health attribute also has its own health custom id. This is in snake_case and you can modify it at will.
You can access a global variable via arcscript, from anywhere in the project, by using its component's custom id and the attribute name, connected with a dot: component_custom_id.attribute_name
For example, to increase the health of the player component by 1 point, type:
player.health += 1
While you type in an arcscript segment or branch, Arcweave will provide auto-complete suggestions.
For more information on component variables, see the Documentation.
Other times, a variable reflects the state of a particular board and is irrelevant to the rest of the project. This is where board variables come in handy.
To return to the Mordor example, if your game includes a scene in the Land of Shadow you can have a board dedicated to it. You can also give your board an attribute that stores the number of orcs guarding the gates.
First, create a new board:
Name the new board Mordor Scene.
Then, create the attribute itself:
Right-click the board and select Attributes.
On the board attributes modal, under the board's name, notice its board custom ID. This by default will be mordor_scene, but you can change it if you like.
From the menu, select # Number > Integer.
25.Notice that the new attribute also has its own custom id: orc_guards, in snake_case.
The board attribute can be irrelevant to the rest of the story, but you can still call it from anywhere, in a fashion similar to that of the component variables: board_custom_id.attribute_name.
Therefore, to increase the orcs by 10, include the following assignment expression in any element or connection label's arcscript:
mordor_scene.orc_guards += 10
For more information on board variables, see the Documentation.
By now, you hopefully have a clear idea of what each variable scope is for. The whole purpose of using scoped variables is to better organize your work, especially as your projects get bigger and more complex.
Remember:
And because Arcscript can access board and component attributes from anywhere in the project, using scoped variables doesn't limit their accessibility.
So the next time you need to store a value, ask: What does this value belong to?
As your Arcweave project grows, you'll thank yourself.
Do you integrate Arcweave with a game engine? We've written a blogpost that explains how variables work in the Arcweave JSON and the popular game engines, with particular care to projects that need migration from the previous Arcweave version.
And of course, you can always share your thoughts with us on the Arcweave Discord.