How to call a function on the beginning of each round

I am trying to make some code run on the beginning of each round. I tried adding a round settings device and subscribe to the RoundBeginEvent, but I can’t seem to get it to work.

It works when called without the event, but that defeats its purpose.

    RoundManager:round_settings_device = round_settings_device{}

    OnBegin<override>()<suspends>:void=
        Print("Creative Device started")
        HandleRoundBegin()
        RoundManager.RoundBeginEvent.Subscribe(HandleRoundBegin)
    
    HandleRoundBegin():void=
        Print("Round Begin")

Hey, you could use await() instead. In the code below you call WaitRoundBegin() in OnBegin(). You then run a loop where at the start it stop until the RoundBeginEvent is activated/gives signal. It will then run the rest of the code.

I have not used round_settings_device before so i dont know the best way to do it. I found that if you have a code before await in the loop it will run at the start of the round and that the code still works without the loop. Hopefully you can make it work.

round_begin_device := class(creative_device):

    @editable
    RoundManager:round_settings_device = round_settings_device{}

    RoundLoopWait<private>:float = 1.0

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

    WaitRoundBegin<private>()<suspends>:void=
        loop:
            RoundManager.RoundBeginEvent.Await()
            Print("Round begin event received!")
            OnRoundBegin()
            Sleep(RoundLoopWait)
            Print("Waiting for next round begin event...")

    OnRoundBegin<private>():void=
        Print("Round Begin")

Output:
LogValkyrieSummary: Server Summary - Successfully activated content on all platforms
LogVerse: : Round begin event received!
LogVerse: : Round Begin
LogVerse: : Waiting for next round begin event…
LogVerse: : Round begin event received!
LogVerse: : Round Begin
LogVerse: : Waiting for next round begin event…
LogVerse: : Round begin event received!
LogVerse: : Round Begin
LogVerse: : Waiting for next round begin event…

1 Like

That’s good to know! In the beginning I though the onBegin function only runs once per game, but someone let me know that it runs once per round. Afterwards I just called the function I needed in the beginning of the onBegin function directly, because for me it was only necessary that it runs in the first round in the beginning and afterwards it didn’t matter if it were the end of round 1 or the beginning of round 2 that the code runs again. If you need the code to run in the beginning 100% of the time, I am sure your code will work. Thanks for the reply :smiley:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.