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
  • Actor Example
  • UActorComponent Example
  1. Documentation
  2. Multiplayer

Replicated UObjects

To replicate UObjects you have to make a new UObjectclass inheriting from UCSReplicatedObject.

[UClass]
public class UMyReplicatedUObject : UCSReplicatedObject
{
    
}

This object can now replicate variables and send RPCs.

To use your replicated UObject in Unreal's multiplayer system it needs to be registered through an AActor or UActorComponent.

Actor Example

[UClass]
public class AMyReplicatedActor : AActor
{
    [UProperty(PropertyFlags.Replicated)]
    public UMyReplicatedUObject ReplicatedObject { get; set; }

    protected override void BeginPlay()
    {
        base.BeginPlay();

        if (HasAuthority())
        {
            ReplicatedObject = NewObject<UMyReplicatedUObject>(this);
            
            // Register the new replicated UObject
            AddReplicatedSubObject(ReplicatedObject);
        }
    }
}

UActorComponent Example

[UClass]
public class UMyReplicatedComponent : UActorComponent
{
    [UProperty(PropertyFlags.Replicated)]
    public UMyReplicatedUObject ReplicatedObject { get; set; }

    public override void BeginPlay()
    {
        base.BeginPlay();

        if (Owner.HasAuthority())
        {
            ReplicatedObject = NewObject<UMyReplicatedUObject>(this);
            
            // Register the new replicated UObject
            AddReplicatedSubObject(ReplicatedObject);
        }
    }
}
PreviousRPCs (Remote Procedure Calls)NextPrimary Data Assets

Last updated 5 months ago