How to add blueprint component to APawn during play

Hello,

Currently in my project I have the need to add a UStaticMeshComponent blueprint to an APawn during gameplay via C++.

For context: As the player moves their mouse across an APawn in the form of a rock, I’d like moss UStaticMeshComponents to spawn on the rock, execute their BeginPlays and remain on the rock as it rolls away.

I haven’t had much success adding components outside of the constructor before so any advice here would be appreciated. In particular, I’ve found it difficult to uncover anything about adding blueprint components via C++. Is it even possible?

Cheers.

If you need further help besides that show me what you got so far. (pastebin.com)

Cheers :stuck_out_tongue_winking_eye:

Hey there, like Nachtmahr mentioned you need to use NewObject, here is an example:

UStaticMeshComponent * Mesh = NewObject<UStaticMeshComponent >(this);
Mesh ->RegisterComponent();

Wow!

Thanks for the quick answers. I will try to apply this advice on the weekend and report back with my progress.

Cheers.

Hi @xlar8or,

I’ve been puzzling over this example this week in preparation for tackling the implementation this weekend.

Would something like this work?

UInstancedStaticMeshComponent * Moss = NewObject<UInstancedStaticMeshComponent >(this, bpInstancedMossMesh);
 Moss->RegisterComponent();

bpInstancedMossMesh is the name of the blueprint I’m trying to add to my actor as a component. It inherits from a custom C++ class that inherits from UInstancedStaticMesh. I’m concerned that the compiler won’t know that bpInstancedMossMesh exists. Is there anything specific I have to do to inform it of the blueprint’s existence?

Hello,

I’ve tried creating the following UProperty in the header file, setting it to the desired static mesh blueprint in the editor, and trying to have it appear during runtime but with no luck. Can anyone see what I’m missing?

.H

UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="MyBPForCPP")
TSubclassOf<class UMossyStaticMesh01> bpMossyStaticMesh;

I then call the following function, passing it “bpMossyStaticMesh” for its “ComponentClassToSpawn”.

.CPP

UMossyStaticMesh01* AMossyRock01::SpawnNewComponent(UClass* ComponentClassToSpawn, FTransform& SpawnLocation)
    {
        check(ComponentClassToSpawn->IsChildOf(UMossyStaticMesh01::StaticClass()));
        
        UMossyStaticMesh01* SpawnedMoss = NewObject<UMossyStaticMesh01>(GetTransientPackage(), ComponentClassToSpawn);
        
        SpawnedMoss->RegisterComponentWithWorld(GetWorld());
        SpawnedMoss->SetupAttachment(Rock, FAttachmentTransformRules::KeepRelativeTransform);
        SpawnedMoss->SetWorldTransform(SpawnLocation);
        
        UE_LOG(LogTemp, Warning, TEXT("Spawning moss."));
        
        return SpawnedMoss;
    }

Alas, although the game runs fine, and the log prints, no new objects ever appear either in the viewport or in the hierarchy of the Rock actor.

Cheers.

Hi @Nachtmahr,

I pasted the relevant code below in a comment. Any advice you could offer on the issue would be appreciated. Thank you.