The Control Flow nodes, together with the Logic nodes, are the heart of coding.
They are used for decision-making, guiding a Script Graph on dynamically executing gameplay based on the game’s current state.
When we speak about control flow in Visual Scripting, we refer to how our scripts are executed: when different actions are performed and in what order.
We previously got to know data types and that nodes have inputs and outputs of data values. We also briefly mentioned that nodes feature green arrows – each arrow is a Flow.
When we connect an output Flow into an input Flow, we tell the Script Graph that after performing a node, it should move on and perform the connected node.
We now know the two types of connections in our Script Graph, the Control Flow of execution order and the Data Flow of passing values around. The two flows are not dependent; a node can be used without the Control Flow; we can also pass the Data Flow to one node and the Control Flow to another node.
We previously learned how Events are the starting points of Script Graphs; therefore, they feature only an output Flow.
They allow us to react to the players’ actions, and we can utilize the them to update our response to the player’s actions dynamically while taking the game’s state into account.
But first, let’s ask: when do we need dynamic decision-making in our scripts?
Let’s say we want our character to unlock a door with a key. We only want the door to open when the character is holding the key.
In this scenario, we have a condition, and we want to behave differently depending on the condition’s state: If the character lacks a key, the condition is not fulfilled; otherwise, the condition is met.
The If node uses a boolean condition to determine which of two output flows to execute. When the condition is true, it will do something; otherwise, it will do something else.
This node has two inputs, a Flow and a Boolean. It also has two Flow outputs.
This may be the first node you encounter that consists of more than one output Flow.
The Control Flow will continue to only one of the branches; according to the input condition, the node chooses a branch – one branch for a True value and another for a False value.
The Switch node is similar to the If node but uses a value input instead of a boolean condition. As a result, this node can have more than just two branches of Flow outputs.
When we select a Switch node, we can add, edit, and remove options in the Graph Inspector – each option we add will create an additional output Flow for the node.
For example, you can use an Integer Switch node that executes a different Flow for each value of 1, 5, and 21.
Moreover, the Switch node has a Default Flow output; it is used when the input value doesn’t match any given option.
The Switch nodes come in three data type flavors: Integer, String, and Enum.
We saw how Switch nodes translate a value into one of the multiple Flow branches.
A Select node, in contrast, maps such values into other data values instead of Flows.
Let’s say we have a boolean; we can map it to 5 when it’s true and 8 when it’s false.
A more advanced example will use Select On Integer:
in the Graph Inspector, we can add a few options, say -2, 4, and 12. In the graph, we can then connect a different Vector 3 to each option.
So now, when we pass one of the configured integer options, the node will provide us with the matching Vector 3.
It’s essential always to provide input to the Default port.
In addition to Boolean and Integer inputs, there are also Select versions for Flow, String, and Enum.
Like a light switch that can turn a light off and on, we can use the Toggle Flow node to execute different branches depending on whether the Toggle is on or off.
The On and Off outputs flow will execute based on the current state.
We can connect Flows into the On/Off inputs to change the state or into the Toggle input to flip it. Immediately after we switch a Toggle, it will perform the Turned On or Off Flows. And in any given point, we can determine the current state through the Is On output.
Another form is the Toggle Value node; it’s similar to the Toggle Flow in a sense it has an On and Off switch. Yet, instead of executing different Flow branches, it will determine the output value based on the input On/Off Values provided. When the Toggle is turned off, the output value will be whatever is connected to the Off input and vice-versa.
When we use the On Update event node as our starting point, its Flow will execute each and every frame; we can use the Once node to limit a portion of the Flow to run only one time, ignoring subsequent executions of the script.
Using the After output, we can differentiate between the first and subsequent executions.
Last, we can use the Reset input Flow to rerun the Once Flow.
The last node is Sequence; it will branch out the execution to multiple Flows and perform them in order.
It can be used for better organization and readability by splitting a long flow. For a more advanced example, our first branch in the Sequence can be interrupted and stop performing based on an If node. We can add another branch that will perform whether or not the first branch is executed entirely.
In the following article, we will dive into Variables...
Hey! Before you move on...