I am trying to wrap my head around replication (networking) in ue4 but it is just not clicking. I have created yet another basic voxel world and want to replicate each chunk as it is changed by clients. My first attempt was to replicate only the UProceduralMeshComponent in the hopes that it was somehow serialized and would just work.
AChunk.h
UCLASS()
class AChunk : public AActor
{
GENERATED_BODY()
public:
AChunk(const FObjectInitializer& objectInitializer);
void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const;
void CreateSurface();
private:
UPROPERTY(Replicated, meta = (AllowPrivateAccess = "true"))
UBoxComponent* boundary;
UPROPERTY(Replicated, meta = (AllowPrivateAccess = "true"))
UProceduralMeshComponent* surface;
};
AChunk.cpp Constructor
AChunk::AChunk(const FObjectInitializer& objectInitializer)
: Super(objectInitializer)
{
bReplicates = true;
bReplicateMovement = true;
boundary = objectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("Boundary"));
boundary->SetNetAddressable();
boundary->SetIsReplicated(true);
RootComponent = boundary;
surface = objectInitializer.CreateDefaultSubobject<UProceduralMeshComponent>(this, TEXT("Surface"));
surface->SetNetAddressable();
surface->SetIsReplicated(true);
surface->AttachTo(boundary);
}
void AChunk::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
DOREPLIFETIME(AChunk, boundary);
DOREPLIFETIME(AChunk, surface);
}
void AChunk::CreateSurface()
{
if (!HasAuthority())
return;
// code to update the procedural mesh
}
This did not work as I had hoped and I cannot find enough information to know why. Both of the two major resources I have found (linked below) only explain the general concepts and emphasize how things work like magic, rather than explaining the gritty details and limitations you must consider.
For example:
- Does the actor that spawned this actor need to be replicated for the spawning of this actor to replicated to each client?
- Do replicated properties need to be public?
- Does UPROPERTY(Replicated) only work on primitive types?
I am really confused by this whole replication process and it is made even more difficult by the fact that it is invisible and so much stuff happens automatically that can either be true replication or just look like it (simultaneous object creation on client & server).
The direction I am thinking about trying next is to just try and replicate a “TMap<FIntVector, int32> voxelMap” for each AChunk and rebuild UProceduralMeshComponent on each client when AChunk is replicated but I don’t know if this is a work around or the correct way to approach this.
Thanks in advance for anyone who at least read this :).
Resources: