How to replicate components spawned at runtime?

Hi, I’m trying to add components to an actor at runtime and then have them replicate.

The actor’s constructor:

ABuildingBlockGrid::ABuildingBlockGrid()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	SetReplicates(true);
}

And a function that is called on the server:

void ABuildingBlockGrid::SpawnBlock()
{
	UBuildingBlockComponent* block = NewObject<UBuildingBlockComponent>(this, UBuildingBlockComponent::StaticClass());
	block->RegisterComponent();
	block->SetMobility(EComponentMobility::Movable);
	block->AttachToComponent(RootComponent, FAttachmentTransformRules::KeepRelativeTransform);
	block->SetWorldTransform(GetActorTransform());
	block->SetMobility(EComponentMobility::Static);
	block->SetNetAddressable();
	block->SetIsReplicated(true);	// <- Causes disconnection
}

This works fine in singleplayer, but it causes my clients to disconnect when “block->SetIsReplicated(true);” is called. If I don’t call it, my clients will be able to collide with it at the actor’s location, but not see it. What would be the proper way to do this?

EDIT:
I found the solution on the forum. Closing thread.

1 Like

Anyone?

I am not an expert on Component replication, but if it works just like Actors, then you have to just spawn the component on the server, it will replicate automatically to all clients.

My main issue is that clients disconnect when the component calls SetIsReplicated(true); on the server.