terça-feira, 15 de julho de 2025

UEFN Verse: Module

A Verse module allows you to organize multiple code elements for reuse across your project. A module can also be distributed for use in other projects.

The first Verse lines of a creative device have the using statement that imports Verse modules from UEFN with the necessary elements.  

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

A module is created using the module keyword. A module can contain other modules, classes, constants, functions, and other elements.

A folder within the UEFN project is treated as a module. For example, you can create a folder with your domain to store your modules that can be distributed to the public.

To create a folder that will be used as a module, follow these steps:

  • Open Verse Explorer
  • Right-click on the Content folder.
  • Select the Create Submodule option.
  • Uncheck the Hide Empty Directories so that the new folder is displayed.
  • Right click on the new folder and rename it with the name of your module.

The image below shows the folder I created for my RomeroBlueprints module. My module contains the dice.verse file, which is one of the files we'll create in this article.


As an example, let's create the Dice module. This module contains the following elements:

  • The die class with fields and a method to roll the die.
  • The GenerateDice function that receives as parameters the amount of dice and the number of faces on the dice. The function creates and returns an array of dice (instances of the die class).
  • The SumDice function, which receives an array of dice as a parameter, adds up the values of all the dice and returns the result of the sum.

Right-click on the folder that was created, in my example the folder name is RomeroBlueprints, and choose the Create New Verse File option.

Use the name dice for the file and click the Create Empty button.

Copy the Verse code below into the dice file:

using { /Verse.org/Random }

Dice<public> := module:

    die<public> := class():

        NumFaces<public> : int = 6
        var TopFace<public> : int = 1

        Roll<public>(): int =
            set TopFace = GetRandomInt(1, NumFaces)
            TopFace

    GenerateDice<public>(Amount: int, _NumFaces: int): []die =

        var DiceArray : []die = array{}

        for (Counter := 1..Amount):
            CurrentDie := die{NumFaces:= _NumFaces}
            CurrentDie.Roll()
            set DiceArray += array{CurrentDie}
        
        DiceArray

    SumDice<public>(DiceArray: []die): int =

        var sum : int = 0

        for (Currentdie : DiceArray):
            set sum += Currentdie.TopFace

        sum

Save the file and compile the Verse code using the Verse > Build Verse Code option from the UEFN menu.

To use the Dice module in another Verse file that is in the project's Content folder, you need to import the module with the using statement with the folder name and the module name separated by "." as shown in the line below:

using { RomeroBlueprints.Dice }

This way, the die class and functions from the Dice module will be available to be used in another Verse file.

Let's create a device in UEFN to test the Dice module. In Verse Explorer, right-click on the project name and choose the Add new Verse file to project option.

In Device Name put module_device and click the Create Empty button.

Copy the Verse code below into the module_device file:

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

using { RomeroBlueprints.Dice }

module_device := class(creative_device):

    DieInstance : die = die{}

    var DiceArray : []die = array{}

    OnBegin<override>()<suspends>:void=

        DieInstance.Roll()
        Print("DieInstance.TopFace = {DieInstance.TopFace}")

        set DiceArray = GenerateDice(10,6)

        Print("Dice generated:")

        for( CurrentDie : DiceArray):
            Print("{CurrentDie.TopFace}")

        Print("The sum of the dice is { SumDice(DiceArray) }")

The module_device contains an instance of the die class and an array of die instances. The OnBegin method executes the functions available in the Dice module to generate 10 dice and add their values. The results of these functions are written in the log.

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 module_device to the level.

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

After starting the session in Fortnite, press the Tab key and select the Log tab to view the log with the messages written by the Print functions.


Table of Contents Verse