How-to: a replicated actor
Build a networked door step by step - replicated state, a Server RPC any player may call, authority on the host, and a multiplayer PIE test.
This guide builds a door that works in a co-op session. Any player can open or close it, the host decides the result, and every machine sees the same state. It follows the pattern of the engine's own sandbox example, ANetShooterActor, which wires its networking in BeginPlay so no host code has to change.
You need a game module with C++ source and a scene you can run with Play As Client.
What you will build
- Player presses Use
- RequestToggle
- ServerToggle RPC
- Host flips open
- Reliable state update
- Every peer sees open
| Piece | Job |
|---|---|
DoorStateComponent | Holds the replicated open flag and the ServerToggle RPC |
NetworkObjectComponent | Makes the door's entity a networked object |
ANetDoor | The actor. Adds both components, registers with replication, turns the door on the host. |
- Create the replicated component
The component carries the state and the request.
openis abool, a discrete type, so it replicates on the reliable stream: a dropped packet can never leave a client with the wrong answer.DoorStateComponent.h#pragma once #include "Core/Platform.h" #include "ECS/Component.h" #include "Reflection/Reflect.h" namespace Mowo { struct MOWO_REFLECTED_NAMED("DoorState") MCATEGORY("Gameplay") DoorStateComponent { MOWO_COMPONENT(DoorStateComponent) MOWO_KEY("open") MOWO_DISPLAY("Open") MREPLICATED bool open = false; MFUNCTION(Server) void ServerToggle() { open = !open; } }; }MFUNCTION(Server)declares a client-to-host request. The body runs on the host. - Declare the actorANetDoor.h
#pragma once #include "Core/Platform.h" #include "Game/AActor.h" #include "Game/MSceneComponent.h" #include "Game/MStaticMeshComponent.h" namespace Mowo { MCLASS() class ANetDoor : public AActor { public: MGENERATED_BODY() ANetDoor() { auto* root = CreateDefaultSubobject<MSceneComponent>("Root"); SetRootComponent(root); auto* mesh = CreateDefaultSubobject<MStaticMeshComponent>("Mesh"); mesh->meshType = "cube"; mesh->AttachTo(root); } void BeginPlay() override; void Tick(float deltaSeconds) override; void EndPlay() override; void RequestToggle(); private: bool m_netWired = false; bool m_shownOpen = false; }; }Keep one reflected type per header, named after the file. The header tool generates reflection only for the type whose name matches the header.
- Wire networking in BeginPlay
BeginPlayputs both components on the actor's backing entity, registers the actor withReplicationManager, and sets the RPC policy.ANetDoor.cpp (part 1)#include "ANetDoor.h" #include "DoorStateComponent.h" #include "Networking/Components/NetworkObjectComponent.h" #include "ECS/World.h" #include "Game/MWorld.h" #include "Math/FRotator.h" #include "Networking/Replication/ReplicationManager.h" #include "Networking/Replication/RpcDispatcher.h" #include "Networking/Session/FNetSession.h" #include "Reflection/ReflectionRegistry.h" #include "Reflection/TypeDescriptor.h" #include <cstdint> #include <vector> namespace Mowo { static World* DoorWorld(const AActor& actor) { MWorld* world = actor.GetWorld(); return world ? world->GetECSWorld() : nullptr; } void ANetDoor::BeginPlay() { AActor::BeginPlay(); World* ecs = DoorWorld(*this); const EntityID entity = GetBackingEntity(); ReplicationManager* replication = ecs ? ecs->GetSystem<ReplicationManager>() : nullptr; if (!replication || entity == INVALID_ENTITY) { return; } if (!ecs->HasComponent<NetworkObjectComponent>(entity)) { NetworkObjectComponent& netObject = ecs->AddComponent<NetworkObjectComponent>(entity); netObject.replicateTransform = true; } if (!ecs->HasComponent<DoorStateComponent>(entity)) { ecs->AddComponent<DoorStateComponent>(entity); } replication->RegisterReplicatedActor(this, entity); replication->InstallAnnotatedRpcPolicies(); const TypeDescriptor* desc = ReflectionRegistry::Get().FindByDisplayName("DoorState"); const int index = desc ? FRpcDispatcher::FindFunctionIndex(*desc, "ServerToggle") : -1; if (index >= 0) { replication->AllowClientRpc(static_cast<uint16_t>(desc->componentId), static_cast<uint16_t>(index), ReplicationManager::ENetRpcAuthority::AnyClient); } m_netWired = true; }Three things matter here:
- The guard.
GetSystem<ReplicationManager>()returns null in a scene with no replication system, so the same actor also works in single player. This is the sandbox's convention. - The owner.
ownerPeerIdstays0, so the host owns the door. - The policy.
InstallAnnotatedRpcPolicieswould allowServerTogglefor the door's owner only, which is the host, so clients would be refused. The explicitAllowClientRpc(..., AnyClient)afterwards overrides that default for this one method ReplicationManager::AllowClientRpc. UseAnyClientonly for shared verbs like this, and keep the RPC body safe to call by anyone.
- The guard.
- Send the request
On the host, call the method directly. On a client, marshal it and send it to the host.
ANetDoor.cpp (part 2)void ANetDoor::RequestToggle() { World* ecs = DoorWorld(*this); const EntityID entity = GetBackingEntity(); DoorStateComponent* state = ecs ? ecs->GetComponent<DoorStateComponent>(entity) : nullptr; if (!state) { return; } FNetSession& session = FNetSession::Get(); if (!session.IsClient()) { state->ServerToggle(); return; } ReplicationManager* replication = ecs->GetSystem<ReplicationManager>(); NetworkObjectComponent* netObject = ecs->GetComponent<NetworkObjectComponent>(entity); const TypeDescriptor* desc = ReflectionRegistry::Get().FindByDisplayName("DoorState"); if (!replication || !netObject || !desc) { return; } const int index = FRpcDispatcher::FindFunctionIndex(*desc, "ServerToggle"); std::vector<uint8_t> payload; if (index < 0 || !FRpcDispatcher::BuildArgsPayload(*desc, static_cast<uint16_t>(index), nullptr, payload)) { return; } replication->SendRpcToServer(netObject->networkId, static_cast<uint16_t>(desc->componentId), static_cast<uint16_t>(index), payload.data(), payload.size()); }You do not install an RPC receiver. While a session runs,
FNetSessionresolves the entity and invokesServerToggleon the host's copy of the component. - React to the state
Only the host moves the door. Its transform replicates, so clients see the door turn without doing anything. Every peer can still read
open, for example to show "Open" or "Close" in an interaction prompt.ANetDoor.cpp (part 3)void ANetDoor::Tick(float deltaSeconds) { AActor::Tick(deltaSeconds); World* ecs = DoorWorld(*this); DoorStateComponent* state = ecs ? ecs->GetComponent<DoorStateComponent>(GetBackingEntity()) : nullptr; if (!state || state->open == m_shownOpen || FNetSession::Get().IsClient()) { return; } m_shownOpen = state->open; SetRotation(FRotator(0.0f, m_shownOpen ? 90.0f : 0.0f, 0.0f).ToQuat()); } void ANetDoor::EndPlay() { World* ecs = DoorWorld(*this); ReplicationManager* replication = ecs ? ecs->GetSystem<ReplicationManager>() : nullptr; if (m_netWired && replication) { replication->UnregisterReplicatedActor(this); } m_netWired = false; AActor::EndPlay(); } } - Test it with two clients
Build the game module, place an
ANetDoorin your scene and save the scene. In the Play dropdown, set Multiplayer Clients to 2, pick a Net Mode, and choose Play As Client. CallRequestTogglefrom your interaction code in one client window and watch the door turn in both.
The door open on both clients.
The door adds its NetworkObjectComponent at runtime, so it has no placed-object id stamped at save time. The client then matches the host's spawn to its own copy of the door by prefab path and nearest pose NetworkObjectComponent. Identical doors that start in the same place can pair up the wrong way round.
Checklist
- The backing entity has a
NetworkObjectComponent. - Synchronized state is in
MREPLICATEDfields on a reflected component, or in the replicated transform. - Requests are
MFUNCTION(Server)methods, allowed for the right callers. - Only the host changes authoritative state.
RegisterReplicatedActorinBeginPlayandUnregisterReplicatedActorinEndPlay.- Every machine runs the same build. A changed replicated field changes the schema id, and mismatched peers refuse each other.
Related
- Replication and RPCs and events for the mechanics used here.
- Sessions and hosting to host and join outside the editor.
- API:
NetworkObjectComponent,ReplicationManager,FRpcDispatcher,AActor.
Engine sources
This page describes behavior implemented in these files of the engine repository, checked against commit 5922e6e5e.
- Engine/Source/Runtime/Networking/Public/Networking/Components/NetworkObjectComponent.h
- Engine/Source/Runtime/Networking/Public/Networking/Replication/ReplicationManager.h
- Engine/Source/Runtime/Networking/Public/Networking/Replication/RpcDispatcher.h
- Engine/Source/Runtime/Networking/Public/Networking/Session/FNetSession.h
- Engine/Source/Runtime/Game/Public/Game/AActor.h
- Engine/Source/Runtime/Math/Public/Math/FRotator.h