UnrealSharp
  • Home
  • FAQ
  • Getting Started
    • Setup
    • Your First Script
    • Debugging
    • Packaging
  • Documentation
    • Classes
      • Properties
        • C++ Functions As C# Properties
        • C++ Properties with Getters/Setters
      • Functions
        • Flags
        • MetaData
      • Default Actor Components
      • Helper Methods
    • Structs
    • Enums
    • Interfaces
    • Delegates
    • Collections
      • TArray
      • TNativeArray
      • TSet
      • TMap
    • Multiplayer
      • Replicated Properties
      • RPCs (Remote Procedure Calls)
      • Replicated UObjects
    • Primary Data Assets
      • Loading Primary Data Assets
    • Loading Soft References
    • Trace Channels
    • Static Variables
      • FWorldStaticVar<T>
      • FGameStaticVar<T>
    • Logging
    • Async
    • Gameplay Tags
      • Gameplay Tag Container
    • Extension / Mixin Methods
    • Subsystems
    • Module Lifecycle
  • Links
    • Github Repository
    • Roadmap
    • Discord
Powered by GitBook
On this page
  • Declaring Replicated Properties
  • Using OnRep Functions
  • OnRep Function with Old Value
  • Lifetime Conditions
  1. Documentation
  2. Multiplayer

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

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

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

Using OnRep Functions

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 useful for updating game state or UI in response to property changes.

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

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

OnRep Function with Old Value

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:

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

Lifetime Conditions

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:

[UProperty(PropertyFlags.Replicated, LifetimeCondition = LifetimeCondition.OwnerOnly)]
public string MyReplicatedString { get; set; }
PreviousMultiplayerNextRPCs (Remote Procedure Calls)

Last updated 8 months ago