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"

UChildActorComponent problem in Construction Script

Hello! I'm trying to add a child actor component in my actor's contruction script. I could easily do this in blueprints but simply cannot get the same exact behaviour in C++. I want the child actor to be attached to the parent object, and be cleared every time the construction script is run (like it does in BP's).

Currently, every time the construction script is run it creates a new Room in the root hierarchy of the World Outliner (although it appears attached to the actor since I can't select them individually). Previously in blueprints, the room would appear nested under the parent actor in the World Outliner and the child actor components are recreated, instead of remaining in the world as a new one is created.

Basically, previously in blueprints when the construction script was executed;

  • Child actor component is recreated

  • Child actor component appears nested under the parent object in the World Outliner

The C++ version with my current code;

  • A new child actor component is created every time, resulting in for example 10 child components if the construction script is ran 10 times.

  • Child actor components appear in the root hierarchy of the World Outliner, although I cannot select or move them

Do I need to manually delete all the child components in C++? This is my current code, any help would be greatly appreciated!

 void ARandomLevelGenerator::OnConstruction(const FTransform& Transform)
 {
         Super::OnConstruction(Transform);
         
         UChildActorComponent* newRoom = NULL;
         newRoom = NewObject<UChildActorComponent>(this);
         newRoom->SetChildActorClass(ARandomLevelGenerator_Room_Base::StaticClass());
         newRoom->RegisterComponent();
         newRoom->AttachToComponent(this->RootComponent, FAttachmentTransformRules(EAttachmentRule::KeepRelative, true));
 }
Product Version: UE 4.20
Tags:
more ▼

asked Oct 25 '18 at 11:29 PM in C++ Programming

avatar image

theNewGfactor
1 1 2

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

1 answer: sort voted first

If you use the line:

 newRoom->bCreatedByConstructionScript = true;

It should behave like in blueprints.

more ▼

answered Oct 26 '18 at 08:44 AM

avatar image

Quexlaw
89 6 12 17

avatar image theNewGfactor Oct 26 '18 at 10:05 AM

Don't have access to UE4 at the moment, but I think I tried this but the property has a _DEPRECATED suffix on it, so I assumed I should not use it? As in, the property is now called;

bCreatedByConstructionScript_DEPRECATED

avatar image Quexlaw Oct 26 '18 at 10:12 AM

Might be, I haven't tried it myself.

If there is no updated equivalent, then consider having the child component as an actual member of your class. Right now, the pointer only exists inside the construction script. If you have the pointer as a class variable, you can perform a null check before creating the object. The construction script gets run pretty frequently, and without destroying older created objects it is no wonder that they will accumulate. Either delete those newly created actors or find the updated equivalent of the bCreatedByConstructionScript variable.

If you are interested, you can still try to use the deprecated variable and see if it behaves correctly. I do not know why your object is not parented to the actual parent though. It might be because you are registering the component before attaching it?

avatar image theNewGfactor Oct 26 '18 at 12:18 PM

In my actual code, the Child Component IS a member class variable, but I simplified it for this post. Unfortunately it doesn't change anything. I should mention that I can get around the multiple copies by manually deleting all children of the parent actor before running the construction script, but I hoped that the engine would automatically do this for me as it does for blueprints.

I've tried putting RegisterComponent() before, after, and in between all those other lines of code and it doesn't make a different.

I will try bCreatedByConstructionScript and see if it fixes the issue, although I'm not comfortable using it even if it works if it's deprecated.

Cheers!

(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