# Reconnect Parties

## Overview

Party reconnection is a new feature in Kronos that allows you to reconnect parties after returning to the main menu.

The way this system works is basically when the party reconnect is requested, the plugin will check what the previous role of the player was (either party host or client), and based on that it will either recreate the party or find it and reconnect to it. Whether the player was a party host or party client is automatically updated while in a party.

{% hint style="info" %}
This is only the first iteration of this system, and I expect to improve it in the future.
{% endhint %}

## How To Reconnect Parties

Lets go over how you can implement party reconnection.

### 1. Setting Up The Leave Request

To make party re-connection possible, we will need to tell the server that we are leaving the match so that it can notify party clients to leave as well. We will use the `PlayerController` class to request (and receive) leave match calls, and we will use the `GameMode` class to process these requests for our players.

Let's start by creating a custom event in the `GameMode` class. The event should have an object reference parameter for your player controller class.

<figure><img src="/files/IgXrYOWBowIXhOjqNtp6" alt=""><figcaption></figcaption></figure>

Next lets go over to the `PlayerController` class and set up the leave requests. We'll need two custom events for this. When the `RequestLeaveWithParty` is called, we make sure that we are on server side and ask the game mode to handle the leave request for us.

<figure><img src="/files/GjX9H8MlDPxygT1TcwRJ" alt=""><figcaption></figcaption></figure>

Make sure that the `ServerRequestLeaveWithParty` event is set to "Run On Server" and Reliable!

<figure><img src="/files/c6a8tEkxZM8E7iPRANHx" alt=""><figcaption></figcaption></figure>

We are also going to create a `ClientLeaveMatch` event which will be the response from the server that we should leave the match and reconnect the party. Make sure that this event is set to "Run On Owning Client" and Reliable!

<figure><img src="/files/z5vHlcoGS7zw44DRVU98" alt=""><figcaption></figcaption></figure>

### 2. Handling Leave Requests

Since we have joined the session as a party, our reservation includes the unique id of all party members, so we can use the reservation system to figure out which players belonged to our party.

So back in the `GameMode` class, let's start by getting the requesting player's reservation. If he somehow doesn't have a valid reservation then we simply tell him to leave without reconnecting the party. You can treat this as an error basically.&#x20;

<figure><img src="/files/CijCD4O1GNpv0XyI9jK8" alt=""><figcaption></figcaption></figure>

We will loop through the reservation members and check if they are complete, meaning the player has joined the game. We will also have to make sure that the reservation member's player id is not the same as the hosting player's id because we want the server to leave last (if he has to).

<figure><img src="/files/w2xhY2WPmIf8ivi7Au1N" alt=""><figcaption></figcaption></figure>

If the conditions above are met, we can tell the client to leave the match and reconnect the party. The PlayerId of `GetPlayerControllerFromUniqueId` is coming from the `Break KronosReservationMember` node. You will need to cast to your own player controller blueprint to call the `ClientLeaveMatch` function you created earlier.

However, if the reservation is actually the hosting player's reservation then it means the server will have to leave as well. Since we must notify everyone before closing the server, we'll create a bool variable in the `GameMode` and set it to true.

<figure><img src="/files/raWrT9T05QJY2YweY0Qa" alt=""><figcaption></figcaption></figure>

After we looped through all members we will check if the server should leave as well. The logic below is connected to the "Completed" output of the `ForEachLoop` node. Since we want all RPCs to reach the desired players, we'll delay a bit before closing the server.

{% hint style="info" %}
Ideally you should keep checking if all clients have left already (using a timer for example), but to keep things simple I'm just going to use a delay. The engine also does a net flush before closing the server, so the delay might not be necessary.
{% endhint %}

<figure><img src="/files/GuL9Xiwf2QIK3RMQK73i" alt=""><figcaption></figcaption></figure>

### 3. Implementing Client Leave

The `ClientLeaveMatch` event is on client side again, so we are ready to call the `LeaveKronosMatch` function. Before leaving, we set a variable in the `GameInstance` that we'll use later to check if we should attempt party reconnection or not. Create a bool variable in your `GameInstance` class and set it to true before leaving the match.

<figure><img src="/files/cNZzgkaKm1wxobSI3eoC" alt=""><figcaption></figcaption></figure>

After the `LeaveKronosMatch` node, the player will return to the main menu where we will attempt to reconnect to the party.

### 4. Attempt Party Reconnection

After the player has returned to the main menu from the match, we will check if we want to attempt reconnecting the party. You can do this from any blueprint (GameMode, HUD, PlayerController, etc.) but you should make sure that the player is logged in with the Online Subsystem before attempting to reconnect parties!

I'll just use the `BeginPlay` event of the main menu `GameMode`.

<figure><img src="/files/1tTSJmxi8R4kKh3ndGJk" alt=""><figcaption></figcaption></figure>

We can reconnect the party using the `ReconnectKronosParty` node. Before reconnecting, we will also reset the `ShouldAttemptPartyReconnect` to false so that next time when we return to the main menu we don't attempt to reconnect again unless it was requested.

<figure><img src="/files/QCcobTVJQR7gn90anjKf" alt=""><figcaption></figcaption></figure>

If you read the text inside the `PrintString` nodes, you can see what happens at the different outputs of the node. **OnSuccess** means we have recreated or found the party, and we are about to connect to it. **OnFailure** means something went wrong. Either the party session was not found or we failed to join the session.

{% hint style="info" %}
The failure event will not be called if connecting to the party fails. In that case the `KronosPartyClient` beacon already notifies the party subsystem for error events.
{% endhint %}

It is also a good idea to display a widget to the user before attempting to reconnect parties, so that they can tell what is happening. You should also lock any user input while reconnecting to avoid cases where the player presses the create party button while reconnecting.

### 5. Connect Leave Request To The Leave Button

Now we just have to hook the leave requesting logic to a UI button. When the player will press the leave match button, we just have to call the `RequestLeaveWithParty` function that we created above to leave the match and bring all party members back to the main menu.

For this example I made it so that only the party host can request to leave with the party, but the system we set up above works even if a party client requests it.

<figure><img src="/files/t1icY0cfzh8Z0qrqq5Km" alt=""><figcaption></figcaption></figure>

## Edge Cases

Since there is no overarching state machine that handles party reconnects, you must account for cases where the player might try to interact with parties in some way (e.g. creating a new party) while party reconnection is in progress. It is also possible that the player accept a session invite while reconnecting.

### Storing Party Reconnection State

Create a bool variable in your `GameInstance` class that will keep track of whether party reconnection is in progress or not. You can set it to true before starting party reconnection, and reset it to false after either reconnection succeeded or failed. You can use this bool to disable certain UI buttons or game logic that shouldn't be accessible while reconnecting to a party.

### Prevent Accepting Invite While Reconnecting

To prevent players from accepting invites, you can override the `CanAcceptSessionInvite` function in the `KronosOnlineSession` class and simply return false while party reconnection is in progress.

<figure><img src="/files/L21vDNebMRuoLNwBQqff" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://horizongames.gitbook.io/kronos-matchmaking/advanced/reconnect-parties.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
