# Custom Party Player Variables

Sooner or later you will probably want to add custom variables to party players instead of using the built-in [**Player Data**](/kronos-matchmaking/usage/party/managing-party-players.md#party-player-data) feature. This guide will walk you through the process of creating custom replicated variables inside the party player state.

## Setting Up Custom Player Variables

### 1. Creating the Party Player State class

First, create a new blueprint from the `KronosPartyPlayerState` class.

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

Now go to **Project Settings -> Kronos Matchmaking** and change the "Party Player State Class" to your custom blueprints.

### 2. Setting up Custom Variables

I'm going to create a new string variable simply called `MyCustomString`. Make sure that the variable's Replication is set to RepNotify! This will automatically create an `OnRep` function for us as well.

I'm also going to create a new event dispatcher for when the variable is changed. Since my variable is a string, I'm going to add a string param to the event dispatcher so that it can broadcast the new value.

<figure><img src="/files/6VjbBygpGNlu2Dag3vaE" alt=""><figcaption></figcaption></figure>

### 3. Implementing the OnRep Function

Next we need to implement the `OnRep` function of the variable. This is called automatically by Unreal when the value of the replicated variable is changed. Here I simply want to call the event dispatcher created earlier.

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

{% hint style="success" %}
Each party client now has `MyCustomString` replicated in their player states.
{% endhint %}

## Modifying Custom Player Variables

Now that each party client has `MyCustomString` replicated in their player states, we just need a way to change the value of this variable. For this we are going to use the `KronosPartyClient` class, and I quickly want to explain why.

{% hint style="warning" %}
The `KronosPartyPlayerState` can replicate variables but it does not have RPC capabilities!

This is due to it being stored in a `FastArraySerializer` inside the `KronosPartyState`.
{% endhint %}

This means that the `KronosPartyPlayerState` should be used for data storage only, while the transition process from client to server should be done in the `KronosPartyClient` class instead.

### 1. Creating the Party Client class

First, create a new blueprint from the `KronosPartyClient` class.

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

Now go to **Project Settings -> Kronos Matchmaking** and change the "Party Client Class" to your custom blueprints.

### 2. Creating the Set Value Function

Create a new custom event called `SetMyCustomString`. Make sure that this event is set to "Run On Server" and Reliable! This will be the function to call when we want to change the value of the variable.

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

Now to actually change the value of `MyCustomString`, we simply need to get the player state of the party client, cast it to our custom blueprint, and set the string's value. Since the value is replicated it will now replicate down to the client and all other party members.

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

{% hint style="success" %}
Calling `SetMyCustomValue` will now change the variable value of the party client.
{% endhint %}

## Reading Custom Player Variables

In order to get the value of `MyCustomString` all we need to do is get a reference to the `KronosPartyPlayerState`, cast it to our custom blueprint, and get the current value. How we get the reference highly depends on the context. Here are some examples:

* **From party client**: Use `GetPlayerState` function.
* **From party player actor**: Use `GetOwningPartyPlayerState` function.
* **From party player widget**: Use `GetOwningPartyPlayerState` function.

{% hint style="info" %}
Don't forget that we have also created the `OnMyCustomStringChanged` callback event, so you can assign an event to it and get notified when the value is changed!
{% endhint %}

## Initializing Custom Player Variables

Use the built-in `ServerInitPlayer` and `ClientInitPlayer` functions of the `KronosPartyClient`. Please refer to [**Initializing Party Players**](/kronos-matchmaking/usage/party/managing-party-players.md#initializing-players) section of the documentation for more details.


---

# 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/custom-party-player-variables.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.
