In this article, we’ll talk about Flow control nodes and how you can use them to create conditional execution flows in Lightact.
Default flow control nodes
Every layer layout by default includes 3 Flow control nodes: Run, On layer begin and On layer end. To explain their behavior lets look at the layout below.
On layer begin executes only once when the transport first hits the layer. Run executes with every frame and On layer end executes last – just before the transport exits the layer and only once. In the layout above I’ve connected a Print to log node to all 3 nodes. Print to log prints a line of text into Lightact logs.
In order to avoid too many log entries during Run phase (remember, Lightact runs at 60 fps), we’ve added a Delay node and set it to 1000 milliseconds. This means that the Print to log node connected to the Delay node will execute only every second. In the example above, we’ve made the layer 6 seconds long which means there should be 6 ‘Run‘ lines in the logs.
So, let’s look at what we have in the logs after we’ve pressed play, waited for the transport to go over the layer and pressed pause after it exits the layer.
We see the following lines:
- ‘Play pressed‘ tells us we’ve pressed play button
- ‘On layer begin‘ line was inserted by the Print to log node connected to the On layer begin node.
- ‘Run‘ lines were inserted by the Print to log node connected to the Delay node.
- ‘On layer end’ line was inserted by the Print to log node connected to the On layer end node and
- ‘Paused pressed‘ was inserted when we pressed paused button.
Additional flow control nodes
If you right-click somewhere in the empty area of a layout you’ll see a menu. And if you hover above Flow control category you’ll see all available flow control nodes
So let’s go through all of them one by one.
Sequence node fires the first output, then the second one. All in one frame. This node is useful when your layout becomes too long, for example, and you’d prefer to stack its branches vertically.
Delay node introduces a delay into the execution flow of the layout. First time when a Delay node gets a lifeline into its output it will fire 1 lifeline output immediately, but the next one will be allowed through only after the Time amount of milliseconds has passed.
For example, in the layout above the ‘delay testing’ line is going to be written in the logs the moment the transport hits the layer, but the second one will be written only after 5 seconds. The third one is going to be written after 10 seconds and so on.
If node outputs either on True or False output depending on Bool condition.
For example, in the layout above if an Integer variable, grabbed by Integer getter node, equals 1, the x offset of Render to canvas node is going to be 1920. If it doesn’t equal 1, then the x offset is 0.
Keypress node is one of lifeline generators because it doesn’t have any inputs. In its properties you select which key on a keyboard it will react to and when that key is pressed, it will output one single lifeline signal. If you hold the key it will, of course, output many of them.
Integer switch case
Integer switch case outputs on one of its outputs depending on the value of the Input. If Input equals the first case from the above, it will output on the first Output below Default. If it equals the second on the second and so on. If it doesn’t equal any of the cases, it will output on Default output. The node is useful if you have many different conditional cases and you’d like to avoid using too many If nodes.
Imagine we have an integer variable (grabbed by Integer getter node) and we’d like to perform different things based on the value of that variable. In the layout above, for example, we are writing different lines of text into the logs based on the value of the variable. If the variable equals 1, we are writing ‘Input=1’, if it equals 2, we are writing ‘Input=2’ and if its something else, we write ‘Input is not 1 nor 2’.
The first time a cycle node is executed, it outputs on the first Output, the second time, on the second and so on. In a way, it is similar to a Sequence node, but while a Sequence node runs through all of its outputs in every frame, a cycle node runs through only one per execution.
Run once node, as the name implies, fires its output only once. It won’t fire it again no matter how many times its Run once input is triggered until it gets a trigger on Reset input. Once the node is reset it will allow one more lifeline signal to pass through and then wait for another reset.