Branches

Branching units split the control flow based on a value.

Branch

The common branch unit uses a boolean condition. You can think of them as an if the condition is true, do something, else, do something else.

Switches

You can also branch depending on the value of an enum, a string or an integer. These units are called Switch units, like they are in scripting. 

To switch on an enum, you first need to decide the type of the enum. This will make the branch output ports appear: 

To switch on a string or number, you first need to create each branch option in the graph inspector:

The unit will then be updated with each output port:

For strings, you can optionally choose to ignore the case of the selector.

Note that a Default port is always added. It is the path that the control flow should take if the input selector does not correspond to any other option.

Selects

Select units are the opposite of switch units. They allow you to select a single value from a set of options based on a selector.

Because they're so similar, we're not going to cover each of them, but here's an example of a Select On Integer unit that chooses a color based on a player number:


Note that predictive debugging here warns you that there will be a crash if playerNo is not within 1, 2, 3, or 4, because we haven't connected the Default port. If we're sure that it never will be for example 5 or 6, we can safely ignore this warning.

Loops

Loops allow you to repeat logic for a certain number of iterations before moving on.

The logic to be repeated is called the body of the loop. After the loop is over, the exit port is called.

Note that the body of every loop is called synchronously, not over the course of multiple frames. Coroutine-like behaviours are instead achieved by listening to the update event manually.

While Loop

While is the simplest form of loop. It will repeat its body while its condition remains true, or in other words, until it becomes false.

For example, this graph generates a new random name until the result isn't contained in the names application variable:


Be careful not to create an infinite loop! If the condition is always true, the editor will hang. Because loop bodies are synchronous, not parallel, there are few uses for while loops in Bolt. 

For Each Loop

For Each iterates over every element of a collection. It outputs the current index and item that is being looped over.

For example, this graph will output 3 messages to the console:

  • I like cats
  • I like dogs
  • I like birds

For Loop

For is a numeric loop. It requires 3 integers: a start index, an end index, and a step. The loop will start at the first index, then move towards the last index via increments of the step. It outputs the current index.

For example, this graph will count to ten by skipping odd numbers because of its step. In other words, its output will be 0, 2, 4, 6, then 8.

The for loop can also be very useful when combined to the Get List Item and Count Items units.

For example, this graph is very similar to the last, in that it will output I like {animal}s to the console. However, instead of using the for each unit that outputs each item, we get each item manually by its index in the list. This allows us to specify a different increment (in this case 2) and skip some items. Therefore, this graph will only output 2 messages:

  • I like cats
  • I like birds

Break Loop

You can tell a loop to finish early by using the Break Loop unit. As soon as this unit is entered, the exit port of the loop will be called, no matter how many more iterations remained.

For example, even though this for loop is supposed to count to 10, it will stop at 5 because of the break. Therefore, its output will be 0, 1, 2, 3, then 4.



Is this article helpful for you?