Pages

quarta-feira, 7 de junho de 2023

Creating the first Verse device

We can create devices in UEFN using the Verse programming language. In this article we will see the necessary steps.

Click on the UEFN Verse menu item to see the available options:


Click on the Verse Explorer option to view existing Verse files in the project:


Files with digest.verse contain information from Verse modules. A Verse module contains code and functions that can be used in your project.

To create a Verse file in Verse Explorer, right-click on the project name and choose Add new Verse file to project: 


A window named Create Verse Script appears. In this window you can choose a template for your Verse file. There is currently only one template available for creating a Verse device. If you want to create an empty file, use the Create Empty button.

Class names in Verse follow a naming pattern using only lowercase letters and the '_' character to separate words. In Device Name put collect_game_device and click on Create button to use the template code. 


See in the Verse Explorer that a new Content folder was created with our Verse file: 


Double-click the Verse file to open it in Visual Studio Code. If you don't already have Visual Studio Code, UEFN will ask if you want to install it. Just confirm and UEFN will install it automatically.

The image below shows the Verse file opened in Visual Studio Code:



Let's look at the template code to see the basic structure of a Verse file.

At the beginning of the file we have the import of the Verse modules. To use a function from an existing Verse module, you need to import the module in your Verse file:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }  

Lines beginning with the # character are comments and are ignored by the Verse code. Comments are useful to make some remarks about the logic of the code.

# A Verse-authored creative device that can be placed in a level

The line below is declaring that collect_game_device is a class of type creative_device:

collect_game_device := class(creative_device):  

A class is a composite type that is made up of multiple elements. The creative_device is a UEFN class that contains variables and functions needed to create a Verse device. In the next articles we will learn about variables and functions.

The collect_game_device class is using creative_device as a superclass (or parent class). All variables and functions of the creative_device class are included in the collect_game_device class. This concept is known as inheritance.

The lines below collect_game_device represent the code block with the class definitions. Verse uses text indentation to define blocks of code and hierarchy. The indentation usually has 4 blank spaces. Verse also accepts the definition of code blocks using { }. 

The code below shows that the OnBegin function is indented. This means that the OnBegin function is part of the collect_game_device class:   

collect_game_device := class(creative_device):
    OnBegin<override>()<suspends>:void=

Verse uses different naming patterns for classes and for variables/functions.

Class names are lower case and words are separated by _ (underscore) as there can be no white spaces in the name.

Ex: collect_game_device

Variable and function names do not use _ to separate words and the first letter of each word is capitalized.

Ex: OnBegin

The OnBegin function has other data next to its name that compose the function's signature. Let's identify each one of them:

  •  OnBegin : Function name.
  • <override> : Specifier indicating that this function is overriding a function with the same name that exists in the superclass creative_device.
  •  ( ) : A function can receive values through parameters that are defined inside the parentheses. When the parentheses are empty, it means that the function has no parameters..
  •  <suspends> : Specifier indicating that this function is asynchronous. This means that the function runs in parallel with other functions and its completion does not have to be immediate.
  • :voidWhat comes after the colon is the function's return type. The void type indicates that the function does not return a value. 

The OnBegin function runs when the game starts for the device. In the template code, the OnBegin function contains two calls to the Print function passing different values as parameters. Note the indentation of the Print functions to indicate that they are part of the OnBegin function:

    OnBegin<override>()<suspends>:void=   
        Print("Hello, world!")
        Print("2 + 2 = {2 + 2}") 


The Print function writes text to the Fortnite log. The parameter of the Print function is of type string which is used to store text. Characters enclosed in double quotes " " are strings.

A pair of curly braces { } within a string represents an expression that will be evaluated and the result of the expression will be stored in the string. In the template example we have the expression {2 + 2} that will be converted to 4 in the string.

When changes are made to Verse code, you need to compile the code to validate the changes and convert the code. Compilation is done by selecting the Verse > Build Verse Code option in the UEFN menu.

In order for Verse device code to run, the device needs to be added to the level. Select the Content Drawer at the bottom of the UEFN window.

Our Verse device is in the folder CollectGame Content > CreativeDevices:


Drag the collect_game_device onto the level. Leave a grid square free between the Player Spawner and the Verse device as shown in the image:



Click the Launch Session button located on the UEFN toolbar to load the level into Fortnite.

After starting the game in Fortnite, press the Alt key and select the Log tab to view the log with the messages written by the Print functions:


Table of Contents Verse