Events are used as a form of communication between game elements. Creative devices have several events, and it's possible to create events on our devices using the verse language.
To create an event named MyEvent, do this:
MyEvent<public>:event() = event(){}
The event can have parameters. To define an event with parameters, you need to specify the parameter types. The example below defines an event with a parameter of type int (integer):
MyEventWithParameter<public>:event(int) = event(int){}
To activate the event use the event's Signal() function:
MyEvent.Signal()
In an asynchronous context, use the event's Await() function to wait until the event is activated:
MyEvent.Await()
At the time of writing, the event class does not have the subscribable interface. This means that it is not possible to register a function to be called when the event is activated.
Maybe this is a strategy by Epic Games to get programmers to adapt to concurrent programming using Await().
Let's do an example to illustrate the use of events. We'll create a device named trigger_group_device that has an array of trigger_device.
This device will have an event called AllTriggersActivated that will activate when all trigger_device in the array are activated. It's a simple implementation that assumes each trigger_device can only be activated once.
Open Verse Explorer, right-click on the project name and choose the Add new Verse file to project option.
In Device Name put trigger_group_device and click the Create Empty button.
Copy the Verse code below into the trigger_group_device file:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
trigger_group_device := class(creative_device):
@editable
TriggerGroup : []trigger_device = array{}
var NumberOfTriggers : int = 0
var TriggersActivated : int = 0
# Creating the event
AllTriggersActivated<public>:event(?agent) = event(?agent){}
OnBegin<override>()<suspends>:void=
set NumberOfTriggers = TriggerGroup.Length
for (Trigger : TriggerGroup):
Trigger.TriggeredEvent.Subscribe(OnTriggerActivated)
OnTriggerActivated( MaybeAgent:?agent ):void=
set TriggersActivated += 1
if (TriggersActivated = NumberOfTriggers):
# Activating the event
AllTriggersActivated.Signal(MaybeAgent)
The trigger_device has the TriggeredEvent event. In the OnBegin function, the for loop is used to iterate through the array and register the OnTriggerActivated() function in the TriggeredEvent event of each trigger.
When the player activates a trigger, the OnTriggerActivated() function is called, which increments the activated trigger count and compares it to the total number of triggers. The AllTriggersActivated event is activated when all triggers are activated.
Save the file and compile the Verse code using the Verse > Build Verse Code option from the UEFN menu.
Let's create another device that will wait for the AllTriggersActivated event to be activated to spawn an item using the Item Spawner device.
In the Verse Explorer, right-click on the project name and choose the Add new Verse file to project option.
In Device Name put event_device and click the Create Empty button.
Copy the Verse code below into the event_device file:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
event_device := class(creative_device):
@editable
TriggerGroupDevice : trigger_group_device = trigger_group_device{}
@editable
ItemSpawnerDevice : item_spawner_device = item_spawner_device{}
OnBegin<override>()<suspends>:void=
TriggerGroupDevice.AllTriggersActivated.Await()
ItemSpawnerDevice.SpawnItem()
The OnBegin() function uses the Await() function of the AllTriggersActivated event to wait until it is activated. After activation, the SpawnItem() function of the ItemSpawnerDevice will be executed.
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 the trigger_group_device and event_device to the level.
Add four Trigger Device to the level. Select all Trigger Device and in the Details tab, look for the Times Can Trigger property. Check this property and keep the value 1 so that each Trigger Device is activated only once.
Add an Item Spawner to the level. It will be used to generate a reward for the player after activating all Trigger Device. In the Details tab, uncheck the Spawn Item on Timer and Respawn Item on Timer properties.
To select the item that will be spawned, click the + button in the Item List row. Under Pickup to Spawn, select the item that will be spawned. I used the Mythic Goldfish.
Select the trigger_group_device in the level. In the Details tab, add 4 elements to the TriggerGroup array. For each element, select a Trigger Device instance.
Select the event_device level. In the Details tab, add references to the trigger group device and the Item Spawner device.
Save the level and click the Launch Session button to load the level into Fortnite. Pass over all triggers to spawn the item in the Item Spawner.