Branch is a time flow control expression that executes other expressions and immediately ends, continuing the flow with the existing expressions after the branch. If the async context (such as OnBegin) ends, the expressions initiated by the branch are automatically canceled if they are still active.
The Branch can be used for side tasks that don't affect the main game logic, such as background animations.
Let's create a simple example to demonstrate how to use Branch. We'll use a function that continuously rotates a creative_prop. We'll use the Sleep function to keep the async context active for a few seconds so we can view the rotation initiated by Branch. When the timer expires, the OnBegin function will exit, and the rotation will automatically stop.
In any UEFN project, open Verse Explorer, right-click on the project name and choose the Add new Verse file to project option.
In Device Name put branch_device and click the Create Empty button.
Copy the Verse code below into the branch_device file:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /UnrealEngine.com/Temporary/SpatialMath}
branch_device := class(creative_device):
@editable
PropToRotate : creative_prop = creative_prop{}
OnBegin<override>()<suspends>:void=
branch:
RotateProp(PropToRotate)
Sleep(10.0)
Print("The RotateProp is canceled when the OnBegin completes")
RotateProp(Prop : creative_prop)<suspends>:void=
loop:
PropTransform := Prop.GetTransform()
PropLocation := PropTransform.Translation
PropRotation := PropTransform.Rotation
NewRotation := PropRotation.ApplyYaw(180.0)
Prop.MoveTo(PropLocation, NewRotation, 2.0)
The branch expression in the example above calls the RotateProp() function, which will rotate the creative_prop and then terminates. The Sleep() function will wait for 10 seconds before the OnBegin function completes and the RotateProp() function is canceled.
Save the file and compile the Verse code using the Verse > Build Verse Code option from the UEFN menu.
Access the Content Drawer and add our branch_device to the level. Add some Prop to the level to be rotated by our device. I used Office Chair 02.
Select the branch_device in the level. In the Details tab, add the Prop reference in PropToRotate field.
Save the level and click the Launch Session button to load the level into Fortnite. Watch the Prop rotate. After 10 seconds, it will stop rotating because the function initiated by Branch will be automatically canceled.