So I’m trying to add components in C++, but in OnConstruction(), which is equivalent to the construction script in BP. I want to display temporary stuff by spawning some components whenever the user moves the BP actor in the scene.
Inside OnConstruction(), I have the following:
child = NewObject<UChildActorComponent>(this);
if (child)
{
child->ChildActorClass = ATargetPoint::StaticClass();
**child->CreateChildActor(); //<-------- problem here**
child->AttachTo(RootComponent);
child->RegisterComponent();
child->bCreatedByConstructionScript = true;
}
When I build, it throws this error:
error LNK2019: unresolved external symbol “public: void __cdecl UChildActorComponent::CreateChildActor(void)” (?CreateChildActor@UChildActorComponent@@QEAAXXZ) referenced in function “public: virtual void __cdecl ADilmunSplineSystem::OnConstruction(class FTransform const &)” (?OnConstruction@ADilmunSplineSystem@@UEAAXAEBVFTransform@@@Z)
I’m guessing that CreateChildActor() is not implemented, just declared in its .h file. Also, it’s not defined as a virtual void (so that I should implement it somewhere). My other guess is that this function is deprecated.
I want to basically acquire the functionality of " add ChildComponentActor" blueprint node, in C++.
Did you manage to figure this out? I’m in the same boat as you. There seem to be a few threads on this topic but it’s not clear what the recommended C++ equivalent of AddChildComponentActor is.
Instead of CreateChildActor (which doesn’t seem accessible with the default build modules) manually call OnComponentCreated like this childActorComponent->OnComponentCreated();
There seems to be a bug where this particular function is not automatically called in C++ alone (the blueprint node AddChildActorComponent works fine) and the ChildActor within your component ends up never being created unless you use the manual workaround.