How do I get a UI Button in verse to communicate with a function passing an agent?

how does one get an Agent from someone pressing a button in a custom UI? I’ve been working on some verse coding and am trying to increase a player’s health with persistence. I’ve been using a trigger to communicate between UI and the Function.

HealthBought(MaybeAgent:?agent):void=
        if(Agent:=MaybeAgent?, FortChar := Agent.GetFortCharacter[]):

That is what I have been attempting to do and that seems to temporarily work, but not save to persistence between sessions

Hello! It looks like there are potentially two issues you’re having, so let’s tackle them one at a time.

First, have you had a chance to review the persistence documentation and make sure you’re reading and writing to the weak_map correctly? You can find that here.

Here is a very minimal example of attaching a callback to a button in the world, and upping an integer that is stored in a persistent weak map. Hopefully this helps!

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

var MySavedPlayerData:weak_map(player, int) = map{}

hello_world_device := class(creative_device):

    @editable
    Button:button_device = button_device{}

    InteractedWithEventHandler(Agent:agent):void=
        if:
            Player := player[Agent]
            Player.IsActive[]
            Num := MySavedPlayerData[Player]
            set MySavedPlayerData[Player] = Num + 1
        then:
            Print("Num {Num}")

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

        for (Player : GetPlayspace().GetPlayers()):
            if:
                Player.IsActive[]
                not MySavedPlayerData[Player]
                set MySavedPlayerData[Player] = 0
    

        Button.InteractedWithEvent.Subscribe(InteractedWithEventHandler)
1 Like

Hey I understand this isn’t a full fletched out code but shouldn’t there be an Player.IsActive[] check before doing anything with player weak_maps to prevent a verse runtime error?
Was just wondering if its safe to not include it because button events should be instantaneous

1 Like

Thank you! Added :smiley:

1 Like