Writing and Using Functions

In this lesson you'll learn to

  • ✓Define your own function in Verse using a name, parameters, and a return type.
  • ✓Call (use) a function you wrote to run its code from another place.
  • ✓Use a function with a parameter to give it different information each time you call it.
  • ✓Connect a custom function to a real Fortnite island moment, like a player stepping on a trigger.

🎮 Why Do We Need Functions?

Imagine you're building a Fortnite island. Every time a player steps on a pressure plate, you want to:

  1. Add 10 points to their score.
  2. Play a sound.
  3. Print a message.

What if there are 20 pressure plates? You'd have to write those same 3 steps 20 times. That's exhausting! 😩

A function fixes this. Think of a function like a vending machine. You press a button (call the function), and the machine does all the work for you every time. You only have to set it up once.


🧩 What Is a Function?

A function is a named block of code that does one job. You write it once, then call it (run it) as many times as you want.

Think of it like a recipe card. The card says "make a sandwich." You follow the steps. Next time you're hungry, you grab the same card — you don't write a new recipe!


📦 What Is a Parameter?

A parameter is information you hand to a function so it can do its job. It's like telling the vending machine which snack you want. Different input → different result!

For example, imagine a function called GivePoints. You could tell it to give 10 points one time, and 50 points another time. Same function, different number!

In Verse, you write a parameter like this inside the parentheses ():

Points : int
  • Points is the name we give the information.
  • int means it's a whole number (like 5, 10, or 100). "int" is short for integer, which just means a number without a decimal.

🎁 What Is a Return Value?

Sometimes you want a function to give something back after it runs. That's called a return value.

Imagine you ask a friend, "How many lives do I have left?" Your friend counts and tells you back the answer. That answer is the return value!

In Verse, you write : int (or another type) after the parentheses to say "this function will hand back a number."

If a function doesn't need to give anything back, you just leave that part out (or use : void).


🛠️ The Parts of a Function in Verse

Here's what a function looks like, piece by piece:

FunctionName(ParameterName : type) : returnType =
    # Your code goes here, indented
  • FunctionName — what you call the function (like GivePoints).
  • (ParameterName : type) — the info you hand in.
  • : returnType — what type of thing the function gives back.
  • = — this is like saying "here comes the recipe!"
  • The indented lines below are the actual steps.

📣 Calling a Function

Calling a function means telling it to run. You just write its name and pass in any info it needs:

GivePoints(10)

That's it! The vending machine does its job. 🎉


🏝️ Connecting to Your Island

In UEFN, your Verse code lives inside a Verse Device — a special invisible gadget you place on your island. When something happens in the game (like a player steps on a Trigger device), your device can run a function.

Here's the big picture of how it works:

  1. You place a Trigger device on your island in UEFN.
  2. Your Verse device has a reference to that trigger.
  3. When the trigger fires, Verse calls your function automatically.
  4. Your function runs its steps — like printing a score message!

This is the magic of functions: the island event calls your function, and your function does all the hard work! 🚀

Worked Example

🔨 Worked Example: A Score Announcer Device

The scenario: A player walks into a trigger zone on your island. Your Verse device runs a function that calculates their bonus score and prints a hype message. Let's build it!


Step 1 — Set up in UEFN:

  • In UEFN, go to Verse > Verse Explorer, right-click your project, and choose Add new Verse file to project.
  • Pick Verse Device as the template. Name it score_announcer.
  • Drag a Trigger device onto your island.
  • Compile (Verse > Build Verse Code), then drag your new device into the level too.

Step 2 — The Verse code:

using { /Script/VerseRuntime }
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /Verse.org/Native }

# This is our Verse Device class.
score_announcer := class(creative_device):

    # We hook up a Trigger device here in the UEFN editor.
    @editable
    MyTrigger : trigger_device = trigger_device{}

    # OnBegin runs automatically when the game starts.
    OnBegin<override>()<suspends> : void =
        # Tell the trigger: when someone enters it, call OnPlayerTriggered.
        MyTrigger.TriggeredEvent.Subscribe(OnPlayerTriggered)

    # This function runs when the trigger fires.
    # Agent is the player who stepped on the trigger.
    OnPlayerTriggered(Agent : agent) : void =
        # Calculate the bonus using our helper function.
        Bonus := CalculateBonus(10)
        # Print the result so we can see it while playtesting.
        Print("Bonus points earned: {Bonus}")

    # ------------------------------------------
    # Our custom function!
    # It takes one parameter: BasePoints (a whole number).
    # It returns a whole number (the bonus score).
    # ------------------------------------------
    CalculateBonus(BasePoints : int) : int =
        # Double the base points and give that back.
        return BasePoints * 2

📖 Walkthrough — What Each Part Does

Line / Block Plain-English Meaning
score_announcer := class(creative_device) Creates our custom device. Think of it as naming our vending machine.
@editable / MyTrigger Lets us plug in a real Trigger device from the UEFN editor — like connecting a wire.
OnBegin<override>()<suspends> : void This special function runs the moment the game starts. void means it gives nothing back.
MyTrigger.TriggeredEvent.Subscribe(OnPlayerTriggered) "Hey Trigger, when you fire, call OnPlayerTriggered!" Like setting up a doorbell.
OnPlayerTriggered(Agent : agent) : void Runs when a player steps on the trigger. Agent is that player.
CalculateBonus(10) Calling our custom function and handing it the number 10.
CalculateBonus(BasePoints : int) : int Defining our custom function. It takes a number in and gives a number back.
return BasePoints * 2 Doubles the number and hands it back to whoever called the function.

🎮 Try It!

  1. Compile and drag both devices onto your island.
  2. In the UEFN editor, click your score_announcer device and set MyTrigger to point at your Trigger device.
  3. Hit Launch Session and walk into the trigger zone.
  4. Look at the on-screen log — you should see "Bonus points earned: 20"!

You just wrote and called your very own function. You're a real programmer now! 🏆

Try It Yourself

🕹️ Your Turn: The Multiplier Machine

You did awesome with CalculateBonus! Now it's time to level up. 💪

Your challenge:

Add a second custom function called CalculateTripleBonus to the score_announcer device. This function should:

  1. Take one parameter: a whole number called BasePoints.
  2. Multiply BasePoints by 3 (triple it!).
  3. Return the result.

Then, inside OnPlayerTriggered, call your new function with the number 15 and print the result like this:

Triple bonus: 45

🪜 Hints (if you need them!):

  • Your new function will look very similar to CalculateBonus. You can use it as a starting point!
  • To multiply in Verse, use the * symbol. So 15 * 3 gives you 45.
  • To call your function and save the result, use:
    TripleResult := CalculateTripleBonus(15)
    
  • To print it, use:
    Print("Triple bonus: {TripleResult}")
    

Bonus challenge 🌟: Can you call CalculateBonus AND CalculateTripleBonus in the same OnPlayerTriggered function and print both results?

Go for it — you've totally got this! 🎉

🏴‍☠️

Take on the Challenge

Pass the 4-question challenge to master this lesson and chart the quest in your journal.

Recap

🌟 Great Work — Here's What You Learned!

A function is a named block of code you write once and can run (call) as many times as you need — like a vending machine you set up once. You can hand a function information using parameters, and a function can return (give back) a result when it's done. In your Fortnite island, functions connect to real game moments — like a player stepping on a trigger — so your island actually does something when players play it. Keep building, keep creating, you're doing amazing! 🚀

Pass the quiz above to chart this quest in your Journal.

Sources

/docs/documentation/en-us/uefn/learn-the-basics-of-writing-code-in-verse/docs/documentation/en-us/uefn/create-your-own-device-in-verse/docs/documentation/en-us/fortnite/create-your-own-device-in-verse
Source

Biloxi Studios original lesson

Guided course

Add this lesson to your free study plan.

🧭 The Keeper’s log

Quest complete? Chart your next heading from the Verse Basics expedition.

⛵ Take me there