Node Copied to Clipboard!

Loop Node

Loop: is a node that will repeat over and over what ever set of functions/actions connected to it.

The image above is a simple demonstration of a loop. The Box has two animate nodes that will bounce the cube up and then back down in an endless loop. You could use an infinite loop like this one for a background animation that may play through the entire lesson.

To produce more advanced loops you can use the End Loop, Loop While and Continue Loop nodes.

End Loop

Below is a more complex/practicable example of the use of a loop node, if you wanted to create a lesson that contains a correct/wrong option. Let’s look at the break down.

right-click in the Storyboarder and select from the menu Input > Wait For Any Click. Click and drag from the Wait For Any Click node and select Branch.

Select the Branch node and hit the + sign in the bottom left corner once to open up an argument slot.

Ungraph the else parameter from the properties panel.

Create two message boxes with a "correct" and a "wrong try again" text.

Connect the correct message box as the first branch argument and connect the wrong try again message box as the Else argument of the branch node.

Connect an End Loop to the correct message box.

Drag the Box from the editor window or scene hierarchy as the second argument of the branch node. This will accept a click on the Box as the correct action and any other clicks to be incorrect. you can add a 2D label to the lesson to let the player know what is expected.

If done properly, when you hit play you will be able to click in the player window on the wrong choice and it will play the wrong try again message and repeat over and over until the correct choice is clicked which will play the correct message and end the loop.

Node

Loop While

A while loop can be used to execute a loop until a given condition is reached.

Define and initialize a variable. In this example the variable will be named x.

Right-click storyboard to create a Loop While node.

You can use several different Operators in condition statements for a loop while. This example will use the Is Less Than node.

Right-click and create a Is Less Than node and add two argument slots with the plus button.

Insert your variable as the first argument.

Right-click in the second, empty parameter and choose int(constant). Click on the constant and change the value to 5 in the properties panel.

Connect Is Less Than to the Condition argument of the Loop While.

The Action argument is where you attach the sequence of actions to be looped over. You must make sure to include an increment or decrement that will change your variable so that the condition will be reached and the loop can end.

In this example we will animate a Box to move to the right 5 times and then stop.

Drag the object from the asset panel or editor window (with drag enabled) to the storyboard and attach an animate node from the bottom handle.

Next call your variable by right-clicking the storyboard and typing the name of it in the search bar.

Attach the increment node from the bottom handle of your variable.

Right-click in the empty parameter it has and insert another int(constant).

Click on the int(constant) you just created in the increment node and change its value in the properties panel to 1.

This will increment the variables value by one and stop the loop when it is no longer less than 5 which will be 5 loops.

Node

Continue Loop

The Continue Loop node can be used to skip over an iteration of the loop. For example, if we wanted to print out the numbers 1 to 5 excluding 2 we can use the Continue Loop as shown in the image above.

From the previous example delete the animate node and attach the IsEqual node to the increment.

Add the variable x as the first argument and 2 as the second argument by right-clicking in the empty parameter slot and choosing int(constant) then changing the value to 2.

Add a Branch to the bottom of IsEqual and add two argument slots.

Right-click in the empty paramter slots and select Bool(Constant) for both.

They will both be False by default. Change one of them to true by clicking on it and then switching the value checkbox in the properties panel on.

Connect the Continue Loop to the True branch.

Right-click and select the x variable again and attach a toString node and then a WriteToConsole Node.

Attach this node to the False branch.

This will result in numbers 1,3,4,5 being printed in the console log.

Node

Loop Over Range

Loop Over Range will loop through a sequence of actions over a specified range of values.

For example, if we wanted to multiply the numbers 1-7 by two this is how we would do it:

To use Loop Over Range you must be in at least Expert complexity.

Right-click and select the Loop Over Range Node.

Right-Click in its Min parameter slot and select Int(Constant) change the Int value in the properties panel to 1.

Right-Click in its Max parameter slot and select Int(Constant) change the Int value in the properties panel to 7.

From the Action parameter we will attach a Begin node.

Click the plus button on the begin node to open up a parameter.

Right-click in the empty parameter slot and go to Variable > New to insert a new variable. Rename the variable to x. Compile the lesson.

Drag a connection from the Begin node and go to Operators > Multiply.

Click the plus button on the Multiply node to open up two parameter slots.

Right-click in the first parameter of Multiply and type in the name of your variable 'X'.

Right-click in the second parameter slot and select Int(Constant). Change the value to 2.

To see the results of this loop dereference a toString node from the bottom handle of the multiply node and dereference a WriteToConsole node from the bottom of that.

This will print the value of x to the console tab each iteration of the loop.

Playing the lesson should show 2,4,6,8,10,12,14 in the console tab.

If you wanted to multiply only every second number starting at 1 we can change the step value of the loop Over Range node from 1 to 2. Then the result would be 2,6,10,14.

Yield

While the program is running, it needs to regularly update the screen so that any changes that have occurred are visible to the user. This update occurs whenever a yield node is encountered. This will pause the program until the screen has been updated and then will continue running where it left off immediately afterwards.

Note: all loops by default will automatically add a yield. Without this, a loop that never exits would cause the program to appear to freeze because the screen would never get updated.

In some cases it may be desirable to disable this behaviour and this can be done by changing the AutoYield property to false in the properties of loop nodes. However, note that with AutoYield off, it may be important to add a Yield node instead.