Creating my own await function

In OnBegin I first want to be sure the map is filled with a lot of players, preferably maxed out. So what I do is

  1. wait until a 60s timer is done
    OR
  2. The playercount = 4

The problem here is my CheckIfMaxPlayers(). The loop causes an issue where the game never starts. Correct me if I am wrong but a good solution would be to turn this CheckIfMaxPlayers function into something so that I can say CheckIfMaxPlayers.Await(), similar to how I wait for the timer.

How would I solve this and get rid of this loop?

image

image

You could potentially make your loop solution work by adding sleep inside of it, but it’s optimal. I looked around in the digest for a way to trigger an await, and it doesn’t seem to be possible to do this directly. For now, you could achieve this by moving the logic into a separate StartGame function that guards itself to only run once. Here’s an example I made:


using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /Verse.org/Concurrency }
using { /EpicGames.com/Temporary/Diagnostics }

# If you want 
game_start_cause := enum:
    Timer
    PlayerCount

ToString(Cause: game_start_cause): []char=
    case(Cause):
        game_start_cause.Timer => "timer running out"
        game_start_cause.PlayerCount => "max player count hit"

my_device := class(creative_device):
    @editable
    WaitingForPlayersTimer: timer_device = timer_device{}

    var GameStarted: logic = false
    PlayerStartCount: int = 4

    OnBegin<override>()<suspends>:void=
        Playspace := Self.GetPlayspace()

        Playspace.PlayerAddedEvent().Subscribe(OnPlayerAdded)
        WaitingForPlayersTimer.SuccessEvent.Subscribe(OnTimerComplete)
        
    OnPlayerAdded(Agent: agent):void=
        if (Self.GetPlayspace().GetPlayers().Length = PlayerStartCount):
            StartGame(game_start_cause.PlayerCount)

    OnTimerComplete(Agent: agent):void=
        StartGame(game_start_cause.Timer)

    StartGame(Cause: game_start_cause):void=
        if (GameStarted?) {
            return
        }

        set GameStarted = true

        Print("Game started by {Cause}")

        # Put your logic here
    
1 Like

Thanks I also had something that looked like this at first. But I did not like having the GameStarted variable. I also did not want to go into a new function (StartGame) but I want to stay in OnBegin

As I understand your request you are mostly using CheckIfMaxPlayers() the right way for it to function like the Await you want. Like ProfessionalBot says though you need to include a Sleep in that function for time to pass, as you have it now it is just infinitely looping and getting the same player count over and over. Changing your code to this should work fine?

CheckIfMaxPlayers()<suspends>:void=
    loop:
        Players := GetPlayspace().GetPlayers()
        if (Players.Length >= MaxPlayerCount){
            break
        }
        Sleep(0.0)

Thanks, I use this code with a Sleep of 1.0s. I though setting it super low like you did causes performance issues.

You can also use an instance of an event class. That is more efficient than polling with a Sleep() - though sometimes polling is the way to go depending on the situation.

  • call Await() on the event in any of the places that you want to wait for it
  • then call Signal() on it to wake it up.

An example of it being used is in the Space Inside / Escape Room’s cinematic_character_device.

2 Likes

What do you mean with “Space Inside / Escape Room’s cinematic_character_device”. Having trouble understanding where to find this example

I’m not currently at my computer - though it should be under “Feature Experiences” (or something like that) in the “Project Browser” when you start UEFN. Then it is in the Verse files for that project called - cinematic_character_device.verse which you should find in the Verse Explorer.

Could it be that you have some unpublished examples?
I checked all available projects in the “Feature Examples” and there’s nothing close to what you describe.

Hey there Conan,

I could have sworn we had more example projects on day 1 of UEFN such as a prop hunt project but as of right now when you go to Feature Examples you get

-Verse Detonation Template (Plant/Defuse the bomb)
-Verse Elimination Template (Gun game?)
-Verse Devices Starter
-Verse Parkour Template
-Animation 101

I know in the creative hub there are tons of games published by Epic Labs that have plenty of features that most of us would love to inspect. If you are able to recommend some of that be added to the Feature Examples internally that would be so awesome.

1 Like