Reference Verse compiles

Summing Arrays in Verse: Treasure Chest Score Tallies

Your pirate island has five treasure chests scattered across the cove. Each chest is worth a different number of gold coins, and you want to flash the total haul on-screen the moment a player triggers the final chest. Verse arrays make this trivially clean: store every chest's value in a `[]int`, loop over it with `for`, accumulate a running total, then push the result to a `hud_message_device`. No spreadsheet required — just sun, salt air, and a `for` loop.

Updated Examples verified on the live UEFN compiler

Overview

Summing an array is one of the most common jobs in a Fortnite island: you keep a list of per-player scores, per-round times, or per-chest values, and you need the total. There is no sum_an_array_device — Verse arrays already give you everything. You store your numbers in a []int (or []float), and you write a small function that loops with for and accumulates a running total.

Reach for this pattern whenever you catch yourself thinking "how many altogether?" — total coins on the dock, combined seconds across three race laps, sum of damage dealt. In our worked example, a sunny cove has three glowing shell-plates; each plate is a placed device that fires an event when a player stomps it. Every stomp appends a value to an array, and we sum that array to light up a VFX celebration when the beach reaches its treasure quota.

The device APIs we actually call to make this a game (not just math) are InteractedWithEvent on a button (the shell-plate stomp), and the vfx_spawner_device (Enable, Disable, Restart) for the celebration burst.

API Reference

(API surface could not be resolved for this device.)

Walkthrough

Here's the full cove scenario. Three shell-plate buttons each hold a point value. When a player stomps a plate, we push that value onto a Scores array, sum the whole array, and once the total reaches our quota we fire a VFX burst on the shore.

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

cove_tally_device := class(creative_device):

    # The three shell-plates players stomp. Buttons fire InteractedWithEvent.
    @editable
    PlateA : button_device = button_device{}

    @editable
    PlateB : button_device = button_device{}

    @editable
    PlateC : button_device = button_device{}

    # Celebration burst on the shore when the quota is hit.
    @editable
    CelebrationVfx : vfx_spawner_device = vfx_spawner_device{}

    # How many treasure-points the cove needs before we celebrate.
    @editable
    Quota : int = 30

    # The running list of point values earned this round.
    var Scores : []int = array{}

    # Reusable helper: walk an array of ints and return the total.
    Sum(Nums:[]int):int =
        var Total:int = 0
        for (N : Nums):
            set Total += N
        Total

    OnBegin<override>()<suspends>:void =
        # Keep the burst off until the cove earns it.
        CelebrationVfx.Disable()

        # Each plate contributes a different point value.
        PlateA.InteractedWithEvent.Subscribe(OnStompedA)
        PlateB.InteractedWithEvent.Subscribe(OnStompedB)
        PlateC.InteractedWithEvent.Subscribe(OnStompedC)

    # Handlers append a value, then re-check the running sum.
    OnStompedA(Agent:agent):void =
        set Scores += array{5}
        CheckTotal()

    OnStompedB(Agent:agent):void =
        set Scores += array{10}
        CheckTotal()

    OnStompedC(Agent:agent):void =
        set Scores += array{15}
        CheckTotal()

    CheckTotal():void =
        Total := Sum(Scores)
        Print("Cove total so far: {Total}")
        if (Total >= Quota):
            # Quota met — light up the shore!
            CelebrationVfx.Enable()
            CelebrationVfx.Restart()

Line by line:

  • The four @editable device fields let you drop the three buttons and one VFX spawner into the slots in UEFN. A device is unreachable from Verse unless it's an @editable field like this.
  • Quota : int = 30 is a tunable number you can edit per-map without touching code.
  • var Scores : []int = array{} is our accumulator array. array{} needs the []int annotation so Verse knows the element type.
  • Sum(Nums:[]int):int is the star: var Total starts at 0, the for (N : Nums) loop visits every element, set Total += N accumulates, and the last expression Total is the return value. This is the canonical sum-an-array pattern.
  • In OnBegin, CelebrationVfx.Disable() hides the effect, then we subscribe each plate's InteractedWithEvent to its handler. Subscriptions belong in OnBegin.
  • Each OnStomped* handler is a method taking (Agent:agent) — the shape InteractedWithEvent hands you. We set Scores += array{...} to append a value, then call CheckTotal.
  • CheckTotal calls Sum(Scores), prints the tally for debugging, and when the sum meets the quota calls Enable() then Restart() to fire the burst VFX.

Common patterns

Summing floats (lap times) instead of ints

Same loop, but a []float because race times are fractional. Note int and float never auto-convert, so the accumulator must also be float.

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

lap_tally_device := class(creative_device):

    @editable
    FinishPlate : button_device = button_device{}

    var LapTimes : []float = array{}

    SumFloats(Nums:[]float):float =
        var Total:float = 0.0
        for (N : Nums):
            set Total += N
        Total

    OnBegin<override>()<suspends>:void =
        FinishPlate.InteractedWithEvent.Subscribe(OnLapDone)

    OnLapDone(Agent:agent):void =
        # Pretend each stomp records a 12.5s lap.
        set LapTimes += array{12.5}
        TotalTime := SumFloats(LapTimes)
        Print("Combined race time: {TotalTime}")

Summing point values gathered from tagged props

Use GetCreativeObjectsWithTag to collect every treasure prop on the beach, count them into an array of fixed values, then sum. Here every tagged object is worth one point.

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

treasure_count_device := class(creative_device):

    @editable
    StartPlate : button_device = button_device{}

    Sum(Nums:[]int):int =
        var Total:int = 0
        for (N : Nums):
            set Total += N
        Total

    OnBegin<override>()<suspends>:void =
        StartPlate.InteractedWithEvent.Subscribe(OnCount)

    OnCount(Agent:agent):void =
        Treasures := GetCreativeObjectsWithTag(tag{})
        # Build an array of 1s, one per treasure, then sum to get the count.
        var Points : []int = array{}
        for (_ : Treasures):
            set Points += array{1}
        Print("Treasures on the cove: {Sum(Points)}")

Summing a live scoreboard array and reacting

Keep per-round scores in an array, sum after each round, and toggle a VFX when the beach beats its best.

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

scoreboard_device := class(creative_device):

    @editable
    RoundPlate : button_device = button_device{}

    @editable
    WinVfx : vfx_spawner_device = vfx_spawner_device{}

    var RoundScores : []int = array{}

    Sum(Nums:[]int):int =
        var Total:int = 0
        for (N : Nums):
            set Total += N
        Total

    OnBegin<override>()<suspends>:void =
        WinVfx.Disable()
        RoundPlate.InteractedWithEvent.Subscribe(OnRoundEnd)

    OnRoundEnd(Agent:agent):void =
        set RoundScores += array{7}
        if (Sum(RoundScores) > 20):
            WinVfx.Enable()
            WinVfx.Restart()

Gotchas

  • Empty array{} needs a type. var Scores := array{} fails — Verse can't infer the element type. Always annotate: var Scores : []int = array{}.
  • int and float never auto-convert. If your data is floats, your accumulator must start at 0.0 and your Sum must return float. Mixing them is a compile error.
  • Appending is set Arr += array{x}, not Arr.Add(x). Verse arrays are immutable values; you produce a new array with += on a var.
  • Subscribe in OnBegin, handle at class scope. InteractedWithEvent.Subscribe(OnStompedA) must run inside OnBegin, and OnStompedA must be a method on the class taking (Agent:agent).
  • Devices must be @editable fields. Calling PlateA.InteractedWithEvent only works because PlateA is a field you wired up in UEFN — a bare local device reference gives 'Unknown identifier'.
  • vfx_spawner_device.Restart() re-triggers burst effects. For a one-shot celebration, Enable() then Restart() guarantees the burst plays even if it was already enabled.
  • The last expression in Sum is the return. Don't add a stray return; in Verse the trailing Total on its own line is the returned value.

Guides & scripts that use trigger_device

Step-by-step tutorials that put this object to work.

Build your own lesson with trigger_device

Generate a personalized, step-by-step lesson plan built around this object — grounded in this exact reference and our compile-verified knowledge base.

Build a lesson →