How to use TriggeredEvent

Hello I am trying to add a way to equip a pet if the player triggers an trigger, (so not by pressing a button) but i do not have enough knowledge in verse to understand what the problem is.


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


agent_data := class:
    var AgentPets : []pet_data = array{}
    var CurrentPetProp : ?creative_prop = false
    
huds_data := class<concrete>:
    @editable
    SuccessHud : hud_message_device = hud_message_device{}
    @editable
    WrongHud : hud_message_device = hud_message_device{}
    @editable
    TriggerHud : trigger_device = trigger_device{}
    @editable
    TriggerSwitch : trigger_device = trigger_device{}

   

pet_data := class<concrete>:
    @editable
    PetName : string = ""
    @editable
    PetPrice : int = 0
    @editable
    ConditionalButton : conditional_button_device = conditional_button_device{}
    @editable
    PetPropAsset : creative_prop_asset = DefaultCreativePropAsset
    @editable
    PetPurchaseButtons : []button_device = array{}
    @editable
    PetEquipButtons : []button_device = array{}

    var HudData : huds_data = huds_data{}
    var MaybeMainDevice : ?pet_system = false


    OnBegin():void=
        for(PetPurchaseButton : PetPurchaseButtons):
            PetPurchaseButton.InteractedWithEvent.Subscribe(OnPurchaseButtonClicked)

        for(PetEquipButton : PetEquipButtons):
            PetEquipButton.InteractedWithEvent.Subscribe(OnEquipButtonClicked)
        HudData.TriggerSwitch.TriggeredEvent.Subscribe(OnTriggerSwitchTriggered)
            
    


    OnPurchaseButtonClicked(Agent:agent):void=
        if(MainDevice := MaybeMainDevice?, AgentDataMap := MainDevice.AgentDataMap[Agent]):
            CanBuy := CheckIfPlayerCanBuy(Agent)
            HaveIt := CheckIfPlayerHavePet(Agent)
            if(HaveIt = false):
                if(CanBuy = true):
                    ConditionalButton.SetItemCountRequired(0, PetPrice)
                    ConditionalButton.Activate(Agent)
                    set AgentDataMap.AgentPets += array{Self}
                    HudData.SuccessHud.Show(Agent)
                    HudData.TriggerHud.Trigger(Agent)
                    EquipPet(Agent)
                else:
                    HudData.WrongHud.Show(Agent)
            else:
                HudData.SuccessHud.Show(Agent)
                EquipPet(Agent)
                
                



    OnEquipButtonClicked(Agent:agent):void=
        HudData.SuccessHud.Show(Agent)
        EquipPet(Agent)    
    OnTriggerSwitchTriggered(MaybeAgent:?agent):void=
        HudData.SuccessHud.Show(Agent)
        EquipPet(Agent) 
    
    CheckIfPlayerCanBuy(Agent:agent):logic=
        AgentGold := ConditionalButton.GetItemCount(Agent, 0)
        if(AgentGold >= PetPrice):
            return true

        return false


    CheckIfPlayerHavePet(Agent:agent):logic=
        if(MainDevice := MaybeMainDevice?, AgentDataMap := MainDevice.AgentDataMap[Agent]):
            for(AgentPet : AgentDataMap.AgentPets):
                if(AgentPet.PetName = PetName):
                    return true
        return false

    
    EquipPet(Agent:agent):void=
        if(MainDevice := MaybeMainDevice?, AgentDataMap := MainDevice.AgentDataMap[Agent]):
            RemovePet(Agent)
            if(FC := Agent.GetFortCharacter[]):
                var PlayerPos : vector3 = FC.GetTransform().Translation
                set PlayerPos.X += 100.0

                if(CreativeProp := SpawnProp(PetPropAsset, PlayerPos, IdentityRotation())(0)?):
                    set AgentDataMap.CurrentPetProp = option{CreativeProp}


    RemovePet(Agent:agent):void=
        if(MainDevice := MaybeMainDevice?, AgentDataMap := MainDevice.AgentDataMap[Agent]):
            if(PetProp := AgentDataMap.CurrentPetProp?):
                PetProp.Dispose()
                set AgentDataMap.CurrentPetProp = false


pet_system := class(creative_device):

    @editable
    PetsData : []pet_data = array{}
    @editable
    HudsData : huds_data = huds_data{}

    var AgentDataMap : [agent]agent_data = map{}

    OnBegin<override>()<suspends>:void=
        for(PetData : PetsData):
            set PetData.HudData = HudsData
            set PetData.MaybeMainDevice = option{Self}
            PetData.OnBegin()

        for(AllPlayers : GetPlayspace().GetPlayers(), Player := player[AllPlayers]):
            OnPlayerJoined(Player)

        GetPlayspace().PlayerAddedEvent().Subscribe(OnPlayerJoined)

        spawn{AsyncPetFollow()}




    OnPlayerJoined(Agent:agent):void=
        if:
            not AgentDataMap[Agent]
            set AgentDataMap[Agent] = agent_data{}



    AsyncPetFollow()<suspends>:void=
        FollowDistance : float = 75.0
        WaitDistance : float = 50.0
        SleepTime : float = 0.2

        loop:
            Sleep(0.0)
            for(AllPlayers : GetPlayspace().GetPlayers(), Player := player[AllPlayers], FC := Player.GetFortCharacter[], AgentData := AgentDataMap[Player], PetProp := AgentData.CurrentPetProp?):
                PetLoc : vector3 = PetProp.GetTransform().Translation
                PlayerLoc : vector3 = FC.GetTransform().Translation
                DistanceToPlayer : float = Distance(PetLoc, PlayerLoc)
                PlayerBack : vector3 = FC.GetTransform().Rotation.GetLocalForward() * -1.0
                PlayerRight : vector3 = FC.GetTransform().Rotation.GetLocalRight()

                if(Direction := (PlayerLoc - PetLoc).MakeUnitVector[]):
                    DistanceToTravel : float = DistanceToPlayer - WaitDistance
                    var LocationToTravel : vector3 = PetLoc + (Direction * DistanceToTravel)
                    var NewPos : vector3 = vector3{X:=PlayerLoc.X + (PlayerRight.X * FollowDistance) + (PlayerBack.X * FollowDistance) + 20.0, Y := PlayerLoc.Y + (PlayerRight.Y * FollowDistance) + (PlayerBack.Y * FollowDistance), Z := PlayerLoc.Z - 40.0}

                    PlayerRotation := FC.GetTransform().Rotation

                    spawn{PetProp.MoveTo(NewPos, PlayerRotation, SleepTime)}

The TriggerHud is used to turn a switch on and the TriggerSwitch would be used with the switch to equip the pet when player rejoins so that they shouldn’t press the button again.

You need to confirm that the optional MaybeAgent is valid and then you need to access the agent from the optional. Once you do this, then you can use that agent as the variable for the functions. eg:

OnTrggerSwitchTriggered(MaybeAgent : ?Agent): void = 

if (Agent := MaybeAgent?):
    HudData.SeccessHud.Show(Agent)
    EquipPet(Agent)

Also, I might add, when you are new to Verse, it might be helpful not to rely so much on the colon equal operator. That operator is a shortcut and hides the data type you are working with. In the beginning, it helps to spell out the data type to lessen confusion about what types you are actually working with.

1 Like