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

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.

1 Like

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.

1 Like