Hi. I’ve been struggling to find a clean and quick workflow for building blueprints using generic components and I’d like to know how experienced devs would tackle the problems I’m having. I think the best way to illustrate the issues I’m facing is to explain what I’m trying to do and how I’ve approached it so far.
I’m making a simple aeroplane which has some control surfaces. The class which receives input and adds thrust forces to the plane is AAircraft
.
I add a static mesh component to my aircraft blueprint in the constructor, like this:
AAircraft::AAircraft()
{
PrimaryActorTick.bCanEverTick = true;
AircraftMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Aircraft Mesh"));
}
Then I set the mesh I want my blueprint to use in the blueprint editor. This works fine.
Next, I want to create a component which is a ‘UControlSurface’. The control surface will add torque to the mesh depending on the velocity of the mesh and the angle of the surface relative to the meshes velocity. The way I would approach this problem is to add instances of the control surface to the aircraft blueprint and then get references to these surfaces from the ‘AAircraft’ class.
There are a few ways to get references to the control surfaces:
1 - Use FindComponentByClass
. This has the obvious problem that if I add multiple instances of the component, I can only get the first one.
2 - Use GetComponentByIndex
. This has the problem that if the components are re-ordered, the code needs to be changed.
3 - Use FindComponentByName
. This has the problem that if any component is renamed, the code needs to be changed.
Not wanting to impose any of this on myself, I decided to declare instances of my component in the constructor in the same way that I assigned the UStaticMeshComponent
; something like this:
YawSurfaceComponent = CreateDefaultSubobject<UControlSurface>(TEXT("Yaw Surface"));
YawSurfaceComponent->AttachToComponent(AircraftMesh, FAttachmentTransformRules::KeepRelativeTransform);
This adds the component and I can set the position of the component in the blueprint. I can add a UStaticMeshComponent
as a child of the UControlSurface
in order to represent the control surface. This all seems to be going great but the UControlSurface
components I add keep either getting their position reset to (0, 0, 0) or their gizmo disappears in the blueprint editor and I cant see where they are or change their position/rotation/scale. I have to remove code in the constructor and recompile, then add the code back in and recompile, then reposition my scene component.
As well as having this issue, I cant help but think that there must be an easier way to execute this. Is there no way I can just declare a pointer to a component, make it EditAnywhere
and assign a component to that reference in the blueprint? Or is there an even easier way?