Download

How to create scene component that the base c++ class can access and the blueprint can modify freely

Hello Unreal Community,

I am having an issue with a fairly basic (I believe) setup and I can’t figure out what the proper way of implementing is.

I want to create an actor in C++ and attach a capsule component that the C++ code can query for overlaps (think - flamethrower and its active zone).

So I created the component in c++ constructor and saved its reference for future use. Something like this in the actor constructor:

FlameThrowerZone = CreateDefaultSubobject<UCapsuleComponent>( TEXT( "FlameThrowerZone" ) );
FlameThrowerZone->SetupAttachment( GetMesh( ) );

A blueprint then inherits from the C++ class, and the component is visible in the tree. I can select it there, change its position, orientation, size and so on. The problem is that I need the capsule to be attached to an animation joint (socket), and for some weird reason the blueprint editor doesn’t allow me to change it (it says “Cannot change socket on inherited components”)

How I am supposed to do that. I’ve tried several options, but none feels right.

  1. I tried hard coding the slot name in the SetupAttachment( ) call, something like:
    FlameThrowerZone->SetupAttachment( GetMesh( ) , "WeaponSocket" );
    it doesn’t really do anything it seems. I assume the problem is that the sockets are being created when the character mesh in the blueprint gets its mesh attached, but this is after the base class constructor call, so the socket name here is undefined.

  2. I tried doing the attachment in the blueprint construction script using AttachComponentToComponent method. This one almost works - the capsule gets attached to the animation. The details panel doesn’t show the socket name at all - it stays blank. In the viewport when I select the capsule and try moving it around, it may happen that it doesn’t move at all. I can’t reproduce it reliably, but it happens every now and then. Once stuck, it is stuck forever. The only way to unstuck it seems to be disconnecting the execution pin in the construction script, saving the blueprint, restarting the editor, recompiling the blueprint, reattaching the execution pin and compiling it again. It is really, really annoying.

  3. Another option I tried was to expose a UCapsuleComponent * variable in the c++ class, but leave it to the blueprint to actually create it. In this way I can create the component wherever I want in the object tree which is great, but there doesn’t seem to be an easy way to set the variable. The details panel shows it, but doesn’t allow me to select from the existing actor components. It seems the only way to set it is in either EventGraph or ConstructionScript of the blueprint. Both works, but very often after changing something in the c++ class, the blueprint code becomes invalid. The “Set FlameThrowerZone” box used to link the component to the variable is drawn with red border, and I have to create a new “Set” funciton, which looks exactly the same as the old one, reattach the pins and recompile.

So, with apologies for the probably unnecessarily long post, what is the proper way of adding a scene component to a blueprint, that can be used in its base c++ class in addition to being able to be edited in the blueprint, with having an option to change its parent socket.

Thank you!