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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

How can I procedurally add components?

I am trying to create an Actor which is composed of randomly selected/generated components at runtime.

The high level goal is to have a machine which will spawn an Actor each time you press a button which can have random properties. For exmaple they would look different by using different meshes and matierials, but also one may shoot, or move, or float, or have a light or particle effect.

The idea later on being that I could take 2 actors and based on some rules merge them together and exchange properties, like evolution/mutation.

I haven't managed to find any way to add components in code that aren't already declared at compile time, or construct components outside of the constructor because you need the PCIP. However in blueprint is it possible to add create and add components at runtime in the EventGraph.

How is this possible in code?

Product Version: Not Selected
more ▼

asked Jul 10 '14 at 09:15 AM in C++ Programming

avatar image

26 4 5 8

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

1 answer: sort voted first

Hi Straits,

You can just use NewNamedObject with the Actor as an Outer parameter. What you can't use is TSubobjectPtr to store the pointer to the newly created component inside of an actor and you should just use normal pointers, like so:

 UActorComponent* MyDynamicallySpawnedComponent;

or, store them inside of an array:

 TArray<UActorComponent*> MyDynamicallySpawnedComponents;

or just don't bother because they should get added to AActor::OwnedComponents array (at least in latest version)

more ▼

answered Jul 10 '14 at 09:26 AM

avatar image

robert.manuszewski STAFF
318 8 3 9

(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