Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to add blueprint component to APawn during play.


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?


Product Version: UE 4.18
more ▼

asked Jan 07 '18 at 07:25 PM in C++ Programming

avatar image

5 2 3 5

avatar image MC1RScorpion Jan 10 '18 at 09:14 PM


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


(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first


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

Cheers ;P

more ▼

answered Jan 08 '18 at 12:59 AM

avatar image

7k 143 28 172

avatar image MC1RScorpion Jan 14 '18 at 05:25 PM

Hi @Nachtmahr,

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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

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

more ▼

answered Jan 08 '18 at 12:18 PM

avatar image

6k 161 129 304

avatar image MC1RScorpion Jan 11 '18 at 08:02 PM

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);

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?

avatar image MC1RScorpion Jan 13 '18 at 06:58 PM


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?


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

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


 UMossyStaticMesh01* AMossyRock01::SpawnNewComponent(UClass* ComponentClassToSpawn, FTransform& SpawnLocation)
         UMossyStaticMesh01* SpawnedMoss = NewObject<UMossyStaticMesh01>(GetTransientPackage(), ComponentClassToSpawn);
         SpawnedMoss->SetupAttachment(Rock, FAttachmentTransformRules::KeepRelativeTransform);
         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.


(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question