Fortnite has several creative devices with features ready to be used in a game.
I recommend getting to know the devices from the Devices module at this official documentation link:
In this article we are going to use a Timer Device and an End Game Device. The Timer Device will count down and when the time is up we will activate the End Game Device to end the match.
Communication between the devices will be done using Verse code.
Click on the Content Drawer at the bottom of the UEFN window. Fortnite creative devices are in the CollectGame Content > Fortnite > Devices folder. Select this folder and search for timer. Drag the Timer Device and drop it on the level:
The properties of the Timer Device that was added to the level are displayed in the Details tab because it is selected. Look for the User Options properties category and change the properties highlighted in the image below:
Place the Timer Device on top of the Verse Device:
Now let's add the End Game Device to the level. Access the Content Drawer and the CollectGame Content > Fortnite > Devices folder. Search for endgame. Drag the End Game Device and drop it on the level next to the Verse Device. By default, the End Game Device is invisible in the game. It will not be necessary to change any properties of the End Game Device.
Now let's create references to the Timer Device and the End Game Device in our Verse device code.
Click the Verse button on the toolbar to open Visual Studio Code.
In programming there is the concept of variable which is an identifier that points to a memory location where a value is stored. We also have the concept of constant which is similar to variable, except that its value cannot be changed during program execution. The Verse language favors the use of constants but we will delve into this subject in another article.
Add two constants in the collect_game_device class as shown in the code below. These constants will reference the creative devices we've added to the level so we can manage them using Verse code. Do not forget the text indentation of 4 spaces to indicate that the constants belong to the collect_game_device class:
collect_game_device := class(creative_device):
@editable
TimerDevice : timer_device = timer_device{}
@editable
EndGameDevice : end_game_device = end_game_device{}
The @editable above the constant name is a Verse language attribute. Attributes start with @ and describe behaviors used outside the scope of the Verse language. The @editable indicates that the variable or constant can be modified directly in UEFN.
Creating a constant in Verse uses the following format:
Identifier : type = initialization
In the Timer Device constant, the identifier (or name of the constant) is TimerDevice, its type is the timer_device class and initialization is done using the timer_device{} constructor which creates an instance of the timer_device class with default values.
Note the difference in naming patterns between variables/constants and classes.
Creative devices use the concept of events. When the Timer Device expires, an event will be triggered. We can register a function on the Timer Device that will be executed when the event happens. A function is a block of code that can be reused in multiple places in the code.
Let's create a function that will activate the End Game Device to end the match. This function will be registered in the Timer Device to be executed when the time is up.
Remove the comments and Print functions that came in the Verse code template. The OnBegin function and the new function will look like this:
OnBegin<override>()<suspends>:void=
TimerDevice.SuccessEvent.Subscribe(HandleTimerFinished)
HandleTimerFinished(MaybeAgent:?agent):void=
if(Agent := MaybeAgent?):
EndGameDevice.Activate(Agent)
Don't be scared off by the Verse language syntax. Gradually you will become familiar with some concepts that will help you understand everything written in the code above.
For now, just get the general idea of what's going on. When starting the game, the OnBegin function is executed. The OnBegin function has only one instruction at the moment which is to register the HandleTimerFinished function in the success event of the Timer Device.
The HandleTimerFinished function receives as a parameter an instance of type agent that represents the player. If the agent instance is valid, the Activate function of the End Gamer Device is called to end the game.
The code for this article should look like this:
# v0.1
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
collect_game_device := class(creative_device):
@editable
TimerDevice : timer_device = timer_device{}
@editable
EndGameDevice : end_game_device = end_game_device{}
OnBegin<override>()<suspends>:void=
TimerDevice.SuccessEvent.Subscribe(HandleTimerFinished)
HandleTimerFinished(MaybeAgent:?agent):void=
if(Agent := MaybeAgent?):
EndGameDevice.Activate(Agent)
Save the file and compile the Verse code using the Verse > Build Verse Code option from the UEFN menu.
Select the collect game device that is on the level. See in the Details tab that the TimerDevice and EndGameDevice properties are visible. They are visible because we use @editable. Click on the properties drop-down to select the devices that are in the level:
Save the level and start the session. Watch the Timer Device being updated. When time runs out, the game will end.