You’ve mastered the fundamentals of Variables. You’ve familiarized yourself with the ways to manipulate them with math. You’re even combining them and applying that knowledge across multiple variables in the same scripts. But, as you continue to explore GB Studio’s events, you may have encountered another way to manage your variables: Flags. This tutorial will introduce you to the basics of Flags, an overview of the events that work directly with flags, and provide an example of how you could use them in your game.
What Are Flags?
Flags are another way to assign a value to a variable by manipulating the individual bits of the data that variable uses. Variables in GB Studio have 8 bits, so this allows us to individually track 8 specific flags.
Each Flag corresponds directly to a specific numeric value. When a flag is added to a variable, it’s value changes based on the flag assigned. When multiple flags are assigned, their values are combined, up to a total of 255 (the maximum value that can be stored in a variable).
Flag | Actual Value |
Flag 1 | 1 |
Flag 2 | 2 |
Flag 3 | 4 |
Flag 4 | 8 |
Flag 5 | 16 |
Flag 6 | 32 |
Flag 7 | 64 |
Flag 8 | 128 |
Flags are tracked independently within a variable, so a variable can have Flags 1 and 5 simultaneously for a total value of 17, but is still recognized as having these two specific flags. This is useful when using various events to check the values of a variable (for example, the [If Variable has Flag] event can check for a specific flag, but a [Variable: Math Function] or [If Variable Compare with Value] will work with the total value).
Flag Specific Events
There are currently 4 Flag based events in GB Studio. 3 events that deal directly with manipulating Flags, and 1 event to check flags:
- Variable: Set Flags
- Variable: Add Flag
- Variable: Clear Flags
- If Variable has Flag
Each of these events share a similar setup: when you create one of these events, you’ll need to assign a variable to edit, and then you are given the 8 options for which flags to manipulate.
[Variable: Set Flag] will overwrite a variable with the specific flag combination, and set the variable’s value to the sum of the new combination.
[Variable: Add Flag] will add the selected flags to the variable. Any flags not selected will be unchanged. When multiple flags are added to a variable, their values are combined (for example, adding Flag 1 (a value of 1) to a variable that has been set to Flag 5 (a value of 16) will make the variable’s new value 17.
[Variable: Clear Flag] will remove the selected flags from a variable. Any flags not selected will be unchanged. Clearing a variable’s flag subtracts that value from the variable if it had already been added.
This is a subtle, but important difference to recognize when choosing to use Set Flag over Add or Clear. While Add and Clear only edit the selected flags of a variable, Setting Flags completely replaces the current values with the new selections. This is useful for resetting a variable’s flag progress, tracking flags in a sequence, or other situations where a specific flag combination is required.
The [If Variable has Flag] event is the primary way to run a check for a flag, and will be your main event for acting on the flags that you’ve set. Like other compare events, with this event, you can check if your selected variable has the specific flag, and then run different scripts depending on if that variable has the chosen flag or not.
While [If Variable has Flag] can only check if a single flag is assigned to a variable (or not) at time, remember that ‘flags’ are just specific values being given to a variable. You can also use other variable comparison events, such as [If Variable Compare with Value] or a [Switch], to check them as well. Just like with other Compare Events, you can nest [If Variable has Flag] inside other [If Variable has Flag] events to check multiple flags independently, or check for flags across different variables.
Using Flags in Your Game
Flags can be useful when you want to use a single variable to track multiple stages or values. Let’s say you’re sending your player on a quest for making breakfast, and they need to find the items on their groceries list. You could use multiple global variables to track “Eggs”, “Milk”, and “Bacon” separately, or you could create a single variable (“Grocery List”), and use flags to keep track.
Flag 1 could be “Eggs”, Flag 2 could be “Milk”, and Flag 3 could be “Bacon”. Whenever the player finds these items, instead of incrementing an individual item variable to the quantity, you can track the player’s progress by adding the appropriate flag. When the “Grocery List” variable has all 3 flags (you can use the compare events to check), they’ve completed their quest.
Tips and Tricks
- Every variable type (Global/Local/Temp) can have flags. Just remember the basics for where and when you can check/edit those variables.
- Every variable’s flags are different. In our example, Flag 1 on the variable “Grocery List” could be assigned when the player receives “Eggs”, but Flag 1 on a different variable in our game could represent something completely different!
- Currently, Flags can not be renamed. Keep track of which flags refer to what action in your game!
- Remember the difference between Setting and Adding variables! Setting will overwrite your current Flag assignments with whatever you’ve selected.
- Assigning multiple Flags changes the variable’s total value, but the individual flag settings are still remembered. This gives you some flexibility in how you use events to check a variable’s values!
Friendly Neighborhood Game Designer (he/him)
GB Studio Games | Tabletop Games