I wish to instantiate at runtime a bunch of actors and parent them properly, in C++.
Problem: my code crashes UnrealEngine with an
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000148
Let’s say I create a Pawn that’s a robotic arm. To keep it very simple:
RobotPawn:
Base (Actor, probably just a cube)
HipPivot (Actor, just for the socket/anchor/transform)
Torso (Actor, prolly a cylinder)
ShoulderPivot (Actor, just for the socket/anchor/transform))
Arm (Actor, another cylinder)
The hip pivot turns left-right, the shoulder pivot turns up/down.
How do I create this hierarchy of actors at runtime? What I tried crashed everything.
So far so good:
On my Pawn, I defined ChildActorComponents for the three body parts (base torso arm) and the two pivots (hip and shoulder).
RobotBase = CreateDefaultSubobject<UChildActorComponent>("Base");
RobotHip = CreateDefaultSubobject<UChildActorComponent>("Hip");
RobotTorso = CreateDefaultSubobject<UChildActorComponent>("Torso");
RobotArm = CreateDefaultSubobject<UChildActorComponent>("Arm");
RobotShoulder = CreateDefaultSubobject<UChildActorComponent>("Shoulder");
I understand that these are all components, not actors. The Hip and Shoulders are empty actors, just used as points/transforms where the body parts are attached. However, the Base, Torso and Arm have their own C++ class because I want to build several robots by subclassing these parts.
RobotBase->SetChildActorClass(ARobotBase::StaticClass());
RobotHip->SetChildActorClass(AActor::StaticClass());
RobotTorso->SetChildActorClass(ARobotTorso::StaticClass());
RobotShoulder->SetChildActorClass(AActor::StaticClass());
RobotArm->SetChildActorClass(ARobotArm::StaticClass());
And then of course, call ->CreateChildActor()
on each component.
So far so good: when I bring my pawn into the world, it creates five objects of the right class. Problem: they’re not parented to each other, they’re all created at the top-level of the world. I want to attach the actors.
Now, the hard part: properly attaching the actors.
I want to attach the actors to each other, so I thought of doing this:
RobotHip->GetChildActor()->AttachToActor(RobotBase->GetChildActor(), FAttachmentTransformRules::KeepRelativeTransform);
Boom, that crashes the editor.
So, I am doing it wrong, am I not?
I’m new, I just can’t find the tutorials or documentation to get this to work.