Best practice for waiting until a session is full before joining it?

I have a two-player online game, and I want matchmaking to connect two players when they hit “quickplay.” But I want both players to remain in the menu in a “searching for opponent” screen until BOTH players are found.

What I’m doing now is having my game instance search for available sessions and create a new one if none are found. Then, when a player connects to a session, it checks how many players are in that session. If there are two players connected, it sends both players to the correct map to start the game.

There are two problems with this. First, I’m using a listen server, so one player needs to be sent to the map with ServerTravel() before the other one can be sent with ClientTravel(). Second, I don’t know how to tell the first player that a second player has joined. I tried using OnSessionParticipantsChange, but it never fires.

Does anyone know how to solve this problem, or if there is a better way to accomplish this?

Thanks in advance!

You can use a dedicated game server to handle matchmaking and player communication. The dedicated server can keep track of all available sessions, and players can connect to it to find a match. When two players have joined a session, the server can send a message to both players to start the game, and they can then connect to the dedicated server for the actual gameplay. To notify players about the second player joining, you can use server-to-client or client-to-client communication, such as Remote Procedure Calls (RPCs), to send messages between players.

I personally like the way you handled everything so far. I would have done the same if I were using a listened server.

Do you know if the same thing is possible with a listen server instead?

Hmm, yes. You can however do it differently using a dedicated server. Using a dedicated server you can have all clients join the one server and then be matched by the dedicated server, causing one to host and the other to join as a client.

You can have both players hosting a server when the game starts. Then run a check to see if there is another server available. If there is one you can simply join the one hosted as a client. I think you have to adjust the timing of the code to not have both clients to join each others server at the same time.

The problem is that I don’t know how to communicate to the players when the game session starts. What I’m doing now is connecting the player to the session, then if the session has two players (meaning it’s full), then the player who just joined can start the session. Then, with an OnStartSessionComplete delegate, that player connects to the game. The problem is that OnStartSessionComplete only triggers for the player who locally started the game, meaning that the other player (the host) does not know when the game starts, so they can’t travel to the correct map to start it.

The Game Mode and Game State has many different events which can handle the game hosting and joining. Game Mode and Game State | Unreal Engine 4.27 Documentation

As for server travel, I am a bit unsure. You should be able to get the number of players on the server and travel to a new map once both have connected.