x

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"

When is valid time to create Subobjects/Components?

I'm trying to create instances of components and attach them to my object, but I get the following error:

 UE_CLOG(!CurrentInitializer, LogObj, Fatal, TEXT("No object initializer found during construction."));

I tried calling it this way, in various places:

 auto WidgetComponent = CreateDefaultSubobject<UWidgetComponent>(TEXT("WidgetComponent"));


It throws the error if I call it from BeginPlay or PostInitializeComponents.

Is it really impossible to call it outside the constructor?

Product Version: UE 4.12
Tags:
more ▼

asked Aug 15 '16 at 10:46 AM in C++ Programming

avatar image

benhumphreys
118 16 24 33

avatar image XaviFieldsGamer Apr 09 '19 at 12:59 PM

Another incomplete/misleading tutorial here... https://docs.unrealengine.com/en-us/Programming/Tutorials/Components/1

welcome to Unreal, here you rarely find complete documentation with working code

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

1 answer: sort voted first

You can create SubObjects/Components anywhere. You can only call CreateDefaultSubobject from the constructor.

To create a component at runtime you must use ConstructObject().

 UWhateverComponent* NewComponent = ConstructObject<UWhateverComponent>(UWhateverComponent::StaticClass(), this, TEXT("ComponentName"));
 
 NewComponent->RegisterComponent();
 NewComponent->OnComponentCreated(); // Might need this line, might not.
 NewComponent->AttachTo(GetRootComponent(), SocketName /* NAME_None */);

There may be a compiler error with missing args in AttachTo, but this is how you create and attach components at runtime.

more ▼

answered Aug 15 '16 at 05:02 PM

avatar image

Shohei
1.2k 31 8 40

avatar image idbrii Dec 08 '16 at 11:05 PM

As of 4.8, ConstructObject is deprecated and you should use NewObject instead.

(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