# Replicated Properties

Replicated properties are variables whose values are automatically synchronized across the network, from the server to all clients. UnrealSharp allows you to easily define which properties should be replicated and under what conditions.

## Declaring Replicated Properties <a href="#declaring-replicated-properties" id="declaring-replicated-properties"></a>

To declare a property as replicated, annotate it with the \[UProperty] attribute and set the PropertyFlags.Replicated flag:

```csharp
[UProperty(PropertyFlags.Replicated)]
public partial int MyReplicatedInteger { get; set; }
```

## Using OnRep Functions <a href="#using-onrep-functions" id="using-onrep-functions"></a>

For more control over replication, you can specify a callback function that the multiplayer system will call when a replicated property changes. This is particularly for updating game state or UI in response to value changes.

```csharp
[UProperty(ReplicatedUsing = nameof(OnRep_MyReplicatedBool))]
public partial bool MyReplicatedBool { get; set; }

[UFunction]
void OnRep_MyReplicatedBool()
{
    PrintString($"MyReplicatedBool has been updated to {MyReplicatedBool}");
}
```

## OnRep Function with Old Value <a href="#onrep-function-with-old-value" id="onrep-function-with-old-value"></a>

If you need to know the previous value of a property, you can define an OnRep function that takes the old value as a parameter:

```csharp
[UFunction]
void OnRep_MyReplicatedBoolWithOldValue(bool oldValue)
{
    PrintString($"MyReplicatedBool has been updated from {oldValue} to {MyReplicatedBool}");
}
```

## Lifetime Conditions <a href="#lifetime-conditions" id="lifetime-conditions"></a>

Sometimes, you might want to replicate a property only under specific conditions, such as only to the owning player. You can achieve this by specifying a LifetimeCondition:

```csharp
[UProperty(PropertyFlags.Replicated, LifetimeCondition = LifetimeCondition.OwnerOnly)]
public partial string MyReplicatedString { get; set; }
```


---

# 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://www.unrealsharp.com/gameplay-systems/multiplayer/replicated-properties.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.
