Enable component replication in C++

Hello Sam,

At 6:30 we use the function SetIsReplicated(true); in the constructor of our UGoKartMovementReplicator Class.
However in the document you linked on Component Replication it states “If your component is a default subobject, this should be done in the class constructor after spawning the component.”

So will it be cleaner to do this in the AGoKart constructor?

ReplicationComponent = CreateDefaultSubobject<UGoKartMovementReplicator>(TEXT("Movement Replication Component"));
	if (ReplicationComponent)
	{	
		//Required for property replication to work with the component
		ReplicationComponent->SetIsReplicated(true);
	}

Also if I do SetIsReplicated(true); in the constructor of the component I always trigger an ensure when building and starting the editor, I can continue without issues though:
“ensureMsgf(!NeedsInitialization(), TEXT(“SetIsReplicatedByDefault is preferred during Component Construction.”));”
This is on Line 1977 of the ActorComponent.cpp file.

3 Likes

I also met this issue today. It says “SetIsReplicatedByDefault is preferred during Component Construction.” So I tried SetIsReplicatedByDefault instead of Set is Replicated, and it works.

5 Likes

Privacy & Terms