Trigger function not working in asynchronous function call?

Hi, I have a code with a simplified version here, with an asynchronous function:

    category_Triggers<public><localizes>:message := "Triggers to/from this device"

    componentTip_endTrigger<public><localizes>:message := "Activates AFTER this device finishes all waves"
    @editable:
        Categories := array{category_Triggers}
        ToolTip := componentTip_endTrigger
    endTrigger : trigger_device = trigger_device{}


    # ------------------------------- Device start -------------------------------
    OnBegin<override>()<suspends>:void=

        # first check if this device is "enabled" by trigger
        enableTrigger.TriggeredEvent.Subscribe(enable_npcRoundManagerDevice) # enableTrigger is an editable variable defined earlier 

    enable_npcRoundManagerDevice(Agent : ?agent):void =
        # function to start spawning NPCs

        startTrigger.TriggeredEvent.Subscribe(start_npcRoundManagerDevice) # startTrigger is an editable variable defined earlier 

    start_npcRoundManagerDevice(Agent : ?agent):void =
        # call asynchronous function
        spawn{npcRoundSpawn_async()}



    npcRoundSpawn_async()<suspends>:void =


        # loops and things happening here, Sleep function called, etc taken out to simplify
        # -----
        # -----


        #end of function - do end trigger signal
        endTrigger.Trigger
        Print("NPC device --- DONE, end trigger activated")

The message “NPC device — DONE, end trigger activated” gets printed in the output log but the trigger device hooked up to this endTrigger does not seem to get activated at all. I double checked the in-game trigger device is enabled while during the game.

Is this a bug or any thoughts what to check?

endTrigger.Trigger(), you’re just keeping a reference to the method otherwise, also you shouldn’t subscribe dynamically like you’re doing, it can lead to infinite subscriptions

Hey thanks for the reply! I thought I did this but it seemed it wasn’t triggering that way too. I’ll try again and confirm.

When we say subscribe dynamically, what part makes it dynamic? Is it for both the enableTrigger and startTrigger lines?

1 Like

You’re subscribing to the startTrigger everytime the enableTrigger is activated, thus leading to potential infinite subscriptions

Ohh I think I see, so ideally we should do all subscriptions one time like in an initialization part of the code, something that would not be repeated potentially?

The way this works now, I only trigger the enableTrigger once during the round (the trigger device can only trigger once and then disables/stops too), and then everything resets from Fortnite’s round system when the round is over (from what I understand). This will be good to know for best practices for sure.

:thinking: hmm maybe I need to rethink what I was doing. My goal was to essentially do “if this creative device IS enabled, and then if it IS triggered, let’s continue and do the code.”

1 Like