How to create an actor inside a component?

Hi,

so basically I’m in the position where I have defined an AActor derived class. That class needs a member instance of another class derived from AActor which will do some stuff the parent shouldn’t do itself. That sub-actor needs a few components of its own to do its job.

Alright now lets say I want to be able to give an instance of that sub-actor to another class via blueprint. The idea was to create a custom component class holding an instance of that sub-actor class. Thing is I can’t have an asset representing an instance of that sub-actor and just want to create a default one and give it to the component. I tried all the different ways of actor creation and it always crashes the editor as soon as it starts (error points to “Casts.cpp” in the engine).

If this is not the proper way of doing this how should I do it ?

Thanks in advance.

Well thanks for taking the time but I already knew all that. I think you missed the point of my question. I didn’t ask how to create an actor by code. I asked how to give an actor to a component without crashing the editor at start.

See here is the chain I wanted to end up with, if you want to access the SubActor:

ParentActor->SubActorComponent->SubActor->SetBleh(true);

Or lets say the component would take charge or giving access methods to its SubActor but that’s so you see the chain.

So I have already tried all the possible ways to create a new actor within the component. I tried it in the CTOR but also in a function called from the OnConstruction of the ParentActor:

  • use the ObjectInitializer to create it in the component’s constructor
  • use NewNamedObject elsewhere
  • use ConstructObject elsewhere
  • also tried spawning actor which I already knew was a bad idea as this must be available in the editor at construct time. Called from the constructor gives an access violation (which is more understandable)

I know that SubActor’s class works because I can drop some (of a BP derived from it) on the map. They just don’t work inside a UPrimitiveComponent.

By the way I can’t use the default object here because it needs to be different depending on its parent. I thought about having the component do the SubActor’s job and not having a SubActor. But the component would need components itself to work and things just don’t fit. Mostly because the SubActor is overriding Tick and OnConstruction.

you probably already looked into this, but have you read through ChildActorComponent.h? it seems to be a component that has a reference to an actor class.

im not sure why you need the subActor to be a property of the subActorComponent, instead of having the ParentActor have a pointer to the subActor. it may be that what your trying to do is impossible for components.

if you explain more specifically what you want the code to do, rather than how you want to organize it, maybe there is another hierarchy you can set up. maybe this can be refactored into multiple objects run by some kind of manager class, or maybe this can be refactored into a single actor with some kind of state machine for the variety of behaviors you need, but without knowing what you want this to do, its hard to understand how you should organize it.

Well I will explain again in a simpler way. I need to be able to drop an actor into a level that will work as is. And I also need to be able to give an instance of that actor to a blueprint and be able to configure it through that blueprint. That’s it.


UChildActorComponent ends up acting the same as my custom component. I can’t initialize the ChildActor object directly. Of course if you don’t you can always select an asset in the editor. But in my case it’s not gonna be an asset.

However I found out that getting the default object of the actor’s class doesn’t give any trouble (ActorClass->GetDefaultObject()). Just hoping it is using it as a template… Though UChildActorComponent doesn’t act like my custom component and gives me a “Template Mismatch during attachment. Attaching template component to instanced component”.

if its only 1 type of actor you need to configure, the blueprint could contain a member variable pointer to that 1 type. your use case doesn’t explain why you would need any components to be involved at all.

if you need to configure alot of different types of actors, from a single blueprint, you should probably tell those actors to configure themselves with an Init function that you can maybe pass some arguments into, storing a separate pointer member variable for each type.

The actor which would receive the sub-actor doesn’t necessarily want to receive anything at all. The c++ class won’t have it. It’s just that a derived blueprint could need it. That’s where components get involved. I don’t see in what other way you would allow a designer to give it to this or that blueprint other than via a component. If you do please enlighten me.

Ok so practical case than. Fire object can be spawned anywhere alone. Flammable object gets in contact with a Fire object. Flammable object is set on fire via its very own Fire object and can keep burning because he owns his very own Fire object. Both interact with it.
I could just spawn another Fire object and attach it to the Flammable. But I could also need a Fire object to rely on its owner if he has one. I could than give the Flammable to the Fire object. But that Fire object will follow rules base on its Flammable and not set on directly. So as it will have to live the entire Flammable’s life even though it’s not active it doesn’t make sense to spawn it at runtime.

I’m open to any suggestions. I would be able to set this straight if I was able to fully understand the engine relationships between the different types of objects I guess.

the flammable actor could have a weak pointer to a fire actor. if the fire goes out or the flammable actor gets destroyed, you can have the fire actor destroy itself. as long as the weak pointer is valid, it can ask the flammable actor to override any rules, like some substances burning faster. once the pointer becomes invalid, you can destroy the fire actor.

i don’t see why an inactive fire actor would have to live as long as the flammable actor, as long as there are no strong pointers to the object, it can be destroyed whenever you want.

Well I decided I would go for a FireComponent doing the stuff and being a member of a FireSource actor. Flammable is derived from FireSource and can take fire damage as opposed to the default of its base class. FlammableStaticMesh gives a mesh in addition. UFireComponent derives from USphereComponent so it already has what it needs for overlapping actors checks (at least that’s what I was hoping).

Now I was trying to create blueprints out of those. So an F_Ball BP derived from AFlammableStaticMesh. And an F_Projectile BP to replace the default projectile in the FPS template. Some things worked fine for a time. Than stopped working without a change. I just don’t get it how sometimes things work and after one more compile they no longer do.

I had a TickComponent working fine in F_Ball and no it no longer does. bCanEverTick is enabled on the PrimaryComponentTick. Same for the projectile. Also I always set bAutoActivate to false for the projectile and it always spawns with true… So I explicitly set it to false in the ctor and even further events, also checked in the BP. But when debugging the SpawnActor sets it to true… I’m just getting so heavily bored of all that nonsense. Can’t get a ■■■■ stupid thing to work right.