Model Game Objects with Verse Classes and Structs
Tutorialbeginnercompiles

Model Game Objects with Verse Classes and Structs

Updated beginnerCode verified

What you'll learn

  • What a class is: a template (like a cookie-cutter) you use to create objects called instances.
  • What a struct is: a simple, immutable data box for grouping related fields.
  • How to construct struct values and read their fields.
  • How to display data on-screen with the real player-UI API (GetPlayerUI[], AddWidget, text_block).

How it works

Think of a class as a cookie-cutter. The cutter is not the cookie — it's the shape. In Verse your device is a creative_device subclass, and when you place it on the island you create one instance of that class.

A struct is smaller and simpler. It just holds data — no behavior. We'll make a hero_info struct with a Name and a Power. You build a struct value with the struct{...} syntax and read fields with dot access: Info.Name.

To prove the data is real, we won't just Print to the log (only you see that). Instead we attach a text_block widget to each player's screen using GetPlayerUI[]. This is a fallible call ([] brackets), so it must live inside an if.

flowchart TD ST["struct hero_info<br/>(Name, Power)"] --> D["class hero_showcase<br/>(the device / cookie-cutter)"] D --> OB["OnBegin runs"] OB --> P["for each player"] P --> UI["GetPlayerUI[Player]"] UI --> W["AddWidget(text_block)"]

Let's build it

Place this Verse device on your island, then press Play. Each player sees a heroes panel built from struct data.

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

# STEP 1: The struct — a simple, immutable data box.
# It groups related fields but has no behavior of its own.
hero_info := struct:
    Name : string
    Power : int

# STEP 2: The class — our device blueprint (the cookie-cutter).
# Placing it on the island creates one instance.
hero_showcase := class(creative_device):

    # A field holding several stamped-out struct values.
    # Note struct{...} construction and field-by-field values.
    Heroes : []hero_info = array:
        hero_info{ Name := "Blaze",  Power := 90 }
        hero_info{ Name := "Frost",  Power := 75 }
        hero_info{ Name := "Bolt",   Power := 60 }

    # Build one text_block showing every hero from the struct list.
    MakePanel() : text_block =
        var Lines : string = "HERO ROSTER"
        # Read struct fields with dot access and interpolate them.
        for (H : Heroes):
            set Lines = "{Lines}\n{H.Name} - Power {H.Power}"
        text_block{ DefaultText := StringToMessage(Lines) }

    # Helper: string -> message so DefaultText accepts our text.
    StringToMessage<localizes>(S : string) : message = "{S}"

    # STEP 3: On start, show the panel to every player via player-UI.
    OnBegin<override>()<suspends> : void =
        Print("hero_showcase started with {Heroes.Length} heroes")
        for (Player : GetPlayspace().GetPlayers()):
            # GetPlayerUI[] is fallible, so bind it inside an if.
            if (PlayerUI := GetPlayerUI[Player]):
                Panel := MakePanel()
                # AddWidget actually puts the widget on the player's HUD.
                PlayerUI.AddWidget(Panel)```

## Try it yourself

*   Add a fourth `hero_info{...}` to the `Heroes` array and confirm it appears in the panel — no other code changes needed.
*   Add a `Speed : int` field to the `hero_info` struct and include it in the interpolated line.
*   Wrap the panel in a `canvas` and give it a `canvas_slot` so you can position it on screen.

## Recap

*   A **class** is a template; each placed device is one **instance** stamped from it.
*   A **struct** groups simple data with no behavior; build values with `struct{...}` and read fields with `.Name`.
*   `GetPlayerUI[Player]` is **fallible** — bind it in an `if` before calling `AddWidget`.
*   `text_block.DefaultText` needs a `message`, so we convert our built-up `string` with a `<localizes>` helper.

Verse source files

Check your understanding

Test yourself with an interactive quiz and track your progress + earn XP — free for members.

Turn this into a guided course

Add Verse classes and structs to model your game's objects to your free study plan — we'll suggest related pages and stitch the lot into one compile-checked, self-guided lesson with worked examples and quizzes.

Original tutorial generated by Verse Island from the Verse/UEFN knowledge base, with references to the Epic Games sources above. Code is validated against the knowledge base.

Comments

    Sign in to vote, comment, or suggest an edit.Sign in