Trying to understand issues with Component Attachment when mixing C++ and Blueprints

Hello, I’m another Unity guy trying things out in Unreal.

I’m trying to understand some of my issues, and it feels like I’m missing something very fundamental that my experimentation hasn’t yielded to me yet.

In this example, I have a C++ class called RtUnitBase. It’s a very simple class that inherits from ACharacter. In my constructor I create a default sub object for the Root.

ARtUnitBase::ARtUnitBase() : Super()
{
	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("Root"));
}

Then I create a derived Blueprint so that I can play with visuals, like the skeletal mesh and capsule that is automatically created by ACharacter. My understanding is that because I set the Root component, all the derived Components would be attached to it as well, and Blueprint seems to back that.

image

vs when I don’t create the RootComponent in my class:

image

Now when I start the game in editor, however, the derived components from ACharacter, (The Capsule, Mesh, and Movement components), are NOT attached to the Root component anymore.

This leads to the Mesh, Capsule, and Movement components being at the origin in world space, while my Actor is at whatever location I’ve specified. If I remove the RootComponent creation in my class constructor, they will all be appropriately attached to the actor’s root component and will look correct, though the Root Component for the Blueprint will be the Capsule component, and I can’t attach any other components to it.

NOTE: I have been doing all the changes to the C++ classes when the editor is closed to avoid as many of the CDO corruption bugs as I can, (how do people live with these issues?! I must be missing something, but it seems like if you change the C++ class at all while the editor is running my Blueprints get corrupted and I have to recreate them from scratch.)

So my question is: What the hell am I supposed to do? I want the root component to be a SceneComponent so I can attach other components to my units, (weapons, particle effects, etc), and attach other actors, (my custom camera). Should I be doing something else entirely? Should my unit have a “has-a” relationship with the character instead of a “is-a” relationship?

Any and all feedback/criticism is welcome, treat me like an idiot.

This is because Character already has a working tree of components. The root is supposed to be the collision capsule, which is moved by the CharacterMovementComponent, which handles all movement and networking.

If you want to replace everything you need to at the very least replace those variables (inherited from Character.h)

And also make sure the movement component updates your root component like this (Character.cpp)

use SetRootComponent() to set the root, instead RootComponent =

1 Like

Hmm, this seems like it would be a common problem, do people regularly need to overload the properties of the parent class? It seems like I’d be fighting the engine.

How do people usually do something like this? Do their higher level classes wrap ACharacter instead of inheriting from it? Or do they just not add new components to their derived classes?

You can already attach other components and actors to the Capsule Component or the Skeletal Mesh Component since they’re inherited from Scene Component. So I don’t see why you need a separate Scene Component as the root to do this.

Why can’t you attach components to the root component? Does this not work?

SpringArmComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComponent"));
SpringArmComponent->SetupAttachment(RootComponent);

image

Sorry for the late reply; day job has been crazy.

This does work, but it doesn’t seem like it’s scalable. My hope is to be able to add an attached actor to this unit that contains my custom camera instead of putting all the components directly in the blueprint. Mostly for re-usability’s sake.

It also didn’t feel right to child every component to the capsule component. I suppose in this case it would be okay, but from what I can tell the standard practice is to put a USceneComponent as the root component which can then have several sibling components. (Happy to be wrong here, though).

If you have any guiding principles, I’m happy to listen to them. I’m just groping my way around the code at the moment.