Classes & Structs: Model the Racer

In this lesson you'll learn to

  • βœ“Define rally_racer class and mod_reward struct, instantiate, and choose class vs struct.

Define rally_racer class and mod_reward struct, instantiate, and choose class vs struct.

πŸ“– Reference & full walkthrough: Classes & Structs: Model the Racer

🧩 Your capstone piece: rally_racer class + mod_reward struct data model


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.
πŸ”’

Keep going β€” free

You've read the intro. The rest of this lesson is free for members. Sign in to continue and track your progress.

Sign in free to continue this lesson
Pass the quiz above to chart this quest in your Journal.

Sources

/guides/verse-classes-and-structs-to-model-your-game-s-objects
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 🐻 North Jungle expedition.

β›΅ Take me there