Time flow in the Verse language refers to the concurrency between asynchronous expressions and it is possible to specify how they relate to each other.
Sync is one of the asynchronous structured expressions. It must be used in an asynchronous context that is a function with the suspends specifier, such as the OnBegin() function.
The sync expression executes other expressions simultaneously and only completes when all expressions in its code block complete.
The code below shows a simple example of the sync expression:
OnBegin<override>()<suspends>:void=
sync:
Trigger1.TriggeredEvent.Await()
Trigger2.TriggeredEvent.Await()
Trigger3.TriggeredEvent.Await()
Print("The three Triggers have been activated")
Trigger1, Trigger2 and Trigger3 are instances of trigger_device. The Await() function waits until the event is triggered. The sync expression ends when all three triggers are activated.
Let's create a device in UEFN with this sync example. 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 sync_device and click the Create Empty button.
Copy the Verse code below into the sync_device file:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
sync_device := class(creative_device):
@editable
Explosive : explosive_device = explosive_device{}
@editable
Trigger1 : trigger_device = trigger_device{}
@editable
Trigger2 : trigger_device = trigger_device{}
@editable
Trigger3 : trigger_device = trigger_device{}
OnBegin<override>()<suspends>:void=
EventsOutput := sync:
Trigger1.TriggeredEvent.Await()
Trigger2.TriggeredEvent.Await()
Trigger3.TriggeredEvent.Await()
MaybeAgent := EventsOutput(0)
if(Agent := MaybeAgent?):
Explosive.Explode(Agent)
This device has references to three trigger_device and one explosive_device. The explosive will detonate when all three triggers are activated.
No exemplo o EventsOutput está sendo usado para armazenar o retorno da expressão sync. O retorno da expressão sync é uma tupla contendo o retorno de cada função.
The Await() function returns an ?agent (option agent) that needs to be converted to an agent to be passed as a parameter to the Explode() function.
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 sync_device to the level. Also add three Trigger Device and an Explosive Device. Place the explosive device away from the trigger devices or reduce the explosion's range to avoid hitting the player.
Select the sync_device in the level. In the Details tab, add the device references.
Save the level and click the Launch Session button to load the level into Fortnite. Walk over the three triggers to detonate the explosive.