Download

how to replicate components added at runtime

Hello,

I am about to create the spawn logic for our game, where every player spawns with customized vehicles.

In order to customize the vehicles based on the configuration stored in the database we have to add all the components at runtime (different static and skeletal meshes, different movement components, …).

So the approach we took is to override the SpawnDefaultPawnFor_Implementation function of the AGameMode class in order to implement out custom spawn logic.
Notice the InitializeVehicle functions which takes the vehicleDefinition from the database as argument.


APawn* ResultPawn = GetWorld()->SpawnActor<APawn>(PawnClass, StartLocation, StartRotation, SpawnInfo);
	if (ResultPawn == NULL)
	{
		UE_LOG(LogTemp, Warning, TEXT("Couldn't spawn Pawn of type %s at %s"), *GetNameSafe(PawnClass), *StartSpot->GetName());
	}

	AAVVehicle* vehiclePawn = Cast<AAVVehicle>(ResultPawn);
	if (vehiclePawn != NULL)
	{
		vehiclePawn->InitializeVehicle(vehicleDefinition);
		vehiclePawn->SetActorLocation(StartSpot->GetActorLocation());
}

InitalizeVehicle calls several member functions like InitializeVehicleChassis, which in turn creates a static mesh component at runtime.


bool AAVVehicle::InitializeVehicleChassis()
{
	UStaticMesh* staticMesh = ACAssetHelper::LoadStaticMesh(TEXT("/Game/VehicleAdvCPP/vehiclebase.vehiclebase"));

	if (staticMesh == NULL) return false;

	UStaticMeshComponent* staticMeshComp = NewObject<UStaticMeshComponent>(this,TEXT("CHASSISMESH"));


	if (GetWorld() && GetWorld()->IsGameWorld())
	{
		staticMeshComp->SetLinearDamping(1.0f);
		staticMeshComp->SetAngularDamping(4.0f);

		staticMeshComp->GetBodyInstance()->PositionSolverIterationCount = 32;
		staticMeshComp->GetBodyInstance()->VelocitySolverIterationCount = 32;
	}



	staticMeshComp->RegisterComponent();
	staticMeshComp->SetStaticMesh(staticMesh);
	staticMeshComp->SetSimulatePhysics(true);
	staticMeshComp->SetCollisionProfileName("Vehicle");
	staticMeshComp->SetVisibility(false);
	SetRootComponent(staticMeshComp);

	return true;
}

As far as I understand AActors are replicated by the engine itself when bIsReplicated is set, but how to I make sure this replicated actor contains all the components created on the server side?
I guess I can not call any remote procedures at the moment the actor spawns because he doesn t exist on the client side yet.

Any suggestions or information on this topic are welcome.

Best regards

Eduard

So long as you call SetIsReplicated in the components contructor, and it’s owning actor also replicates - It will work.

By “it will work”, I mean whatever properties you have in the component that are told to replicate will replicate, and you will be able to call RPC’s etc. You’re setting a mesh there on the Server, that won’t copy over to clients because the mesh asset isn’t set to replicate by default.

Thank you for your help, that was really helpful. Now I know what to look for in the engine code.

Best regards

Eduard