How I can attach an actor to a box component?

Hello,

I have created a graph (derived from AActor) class where I want to store all of the graph nodes. The graph has a box component where every node should be attached to. In addition to that I have created a node class which is also derived from AActor. Now I want to spawn the nodes corresponding to the boxcomponent width and depth and saving them in an array inside the graph class. But where/how could I actually attach those actors to my box component ?

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AActor/AttachRootComponentToActor/index.html

You might also try using UChildActorComponent, which allows you to attach actor as a component to other actor. It can be used more like a slots

If I understand it correctly why don’t you create some “node holders” on your box component and spawn your nodes giving them the location/rotation of those holders you have?, like having sockets on a mesh and attaching something to it?

Also AttachRootComponentTo I believe it only works for skeletal meshes, so in this case you just have to either find a way to create a socket on the box component (I have no clue whether this is possible) or set the location of the nodes on the box.

Hope this helps ^^

Okay, well I have corrected my question. I forgot about the fact I am actually want to attach the node to the box component. My fault. But I have tried it with AttachRootComponentTo, everything compiles correctly but the editor is crashing. I am using this code:

ANode* tempNode = CreateAbstractDefaultSubobject<ANode>(TEXT("Node"));

	tempNode->AttachRootComponentTo(this->nodeContainer, NAME_None, EAttachLocation::SnapToTarget);

where the type of nodeContainer is UBoxComponent

I believe abstract default subobjects are null in the constructor until you actually provide a class in a subclass. So the pointer returned from that function is null. Then you’re dereferencing a null pointer. maybe

That or your nodeContainer pointer is null.

Okay so I have also tried your method but it also crashed my editor :frowning: My code I was using :
FVector boxSize = this->nodeContainer->GetScaledBoxExtent();
UChildActorComponent* tempNodeKeaper = CreateAbstractDefaultSubobject(TEXT(“nodeKeaper”));
ANode* tempNode = CreateAbstractDefaultSubobject(TEXT(“node”));

	tempNodeKeaper->AttachTo(this->nodeContainer);

	tempNode->nodeMesh= this->nodeMesh;

	tempNodeKeaper->ChildActor = tempNode;

I have set a breakpoint for checking if one of them is null. And everyone seems to be initialized correctly.

But I have tried it with blueprints and it seems to work, now I only have to figure out what coding mistake I have made :slight_smile: Update when It has succeeded

Good idea in theorie but I couldnt figure it out how to create a socket on a BoxComponent

Then no idea. :frowning:

Cast of Graph /Script/MyProject2.Default__Graph to Level failed this is the only showed error I have also commented out everything except this line: ANode* tempNode = CreateAbstractDefaultSubobject(TEXT("node")); so my node class creates this error. Mh and when I set a breakpoint it seems to inintialized correctly: tempNode = 0x000001da58d61000 {neighbours={AllocatorInstance={Data=0x0000000000000000 } ArrayNum=0 ArrayMax=...} ...}.

It’s probably because you’re trying to create an actor as a default subobject. That doesn’t work. Use a child actor component.

Try debugging and see the logs, therem ight be info in log

yea subclass might be a issue, spawn object on BeginPlay insted of constructor

You can’t use new on UObjects, for actors use

()->SpawnActor<ANode>()

But do it on BeginPlay this wont work in constructor, you will have crash

I believe this is not the way to go since I don’t think it’s possible, I’m no expert but I’d create an empty holder for the nodes to be “attached” to.

I have found the setChildActorComponent(TSubClassOf(AActor)) method on the UChildActorComponent. But I cant really find how I could use setChildActorComponent(new ANode()); is wrong

If I use `

ANode* tempNode =
()->SpawnActor()

it breaks intoUE_CLOG(!CurrentInitializer, LogObj, Fatal, TEXT(“No object initializer found during construction.”)`

PS.: SpawnActorLESSTHANANodeGREATERTHAN()

Im calling this method inside the beginplay method