Trouble with Scene and Actor Components in C++ while in Editor

Hey there,
I am at my wit’s end when it comes to this problem. I have searched everywhere and cannot get a proper solution.

I have a custom Scene Component (and an Actor Component; I have tried both) which I would like to do the following in-editor:

Within the component I need the ability to create a collection of Box Components, each box component would be attached to a bone or socket of a character which the scene/actor component is attached to. On each of these boxes I would like to be able to change their size and relative position as they sit on the character bone/socket.

Each box component is iterated upon by the parent scene/actor component, which then handles some custom collision code I wrote.

My problem is this:
When using a scene component as the parent component I am unable to access the owning AActor from within the editor when I call GetOwner(). It is always empty, so is the GetAttachParent(), GetAttachSocketNam(), GetParentComponents()… everything is just empty until, I assume, the game actually runs. I need these properties in the editor if I am going to configure my custom components efficiently.

When using an Actor Component things are a little better. I am able to create and attach box components to the parent actor’s bones and sockets, but as soon as I do so the variable to the box component within the details panel shows as empty. Even though the pointer to that box component is still valid, I can see the box component rendered in the viewport, and I can change some of the properties programmatically, I am unable to do so in the editor itself. So now I can create these boxes but cannot modify their position or scale.

I am at a complete loss as to how to continue this. I looked into writing my own editor module to try and extend the editor functionality, perhaps handle some custom structs, but that won’t help much if these options don’t render properly in a way I can manipulate them in the first place.

Has anyone worked with custom character collision? When I search for this I only come up with static mesh collision questions and walkthroughs, which is not what I need. Or maybe experience handling multiple sub-components spawned from a single scene or actor component within the editor?

Any help is appreciated…

first anything that exists in the world/level is a SceneComponent even the RootComponent is a USceneComponent. UActorComponents are meant for logic only.

though USceneComponent mostly just means “this has a transform” it is the sub classes of USceneComponent that give you the shape (UStaticMeshComponent) and the abilility to animate (USkeletalMeshComponent) and some of them have additional things about them.

for example if you say want a BoxCollider to just be attached to the say the root it would be a UBoxComponent (to have it modified in the editor either by itself, or in the Actor/Pawn/Character it should be marked either Public, or as Protected with meta = (AllowPrivateAccess = "true") in the UPROPERTY of it), but each of these UBoxComponets is a USceneComponent

Sockets don’t typically exist until the Skeletal Mesh exists, and for the most part Skeletal Mesh is assigned in the Editor to avoid Hard Runtime Resolved string resolution (one person on the project changes the name/location of one of these references, and you might not know until runtime when things either crash, or worst yet you start getting garbage)

you should probably have each of these box components defined in your Pawn/Character class as public or protected with meta = (AllowPrivateAccess = "true") but when you create them in the constructor give them location identity and rotation identity, then in the editor apply the parent socket there which will treat the location/rotation as offsets from the parented socket.

as for these not returning the correct value with GetOwner() a SceneComponent will only return the value of GetOwner() if it has been attached through SetupAttachment(USceneComponent*) because it is possible that you do all the construction then hand the USceneComponent off to something else and then attach it.

are you utilizing

#if WITH_EDITOR
public: void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent);
#endif

to make these changes in your code or something else? (it is inherited from UOBject) there are some things you should avoid there like I had issue trying to Attache USceneComponents as this would cause the Editor to crash.

@gardian206 , Thanks for the response and info. I think my problems are a bit more complicated than I originally thought…

Part of my tests relied on using a Details Customization which added a button that executed a function within the class, and another test relied on spitting out log entries on construct. What I have found is that I have been technically handling multiple instances of my objects while debugging, which is probably the reason I am so confused.

When my object is initialized a series of log entries are created. And when the button in the editor is pressed some more log entries are entered. Each check the GetOwner() return value.

This is the log as soon as I add my custom component to my character and after I press the button only once:

Note that it is initializing four times, and one of those times the scene component doesn’t have an owner. My guess is that I am either dealing with the first instance of this object or a hidden instance, but regardless, I am not actually interacting with the actor represented in the editor.

So that solves the mystery of why I wasn’t seeing any parent or owner objects in certain cases. But I am still facing the issue of creating these subcomponents in-editor and attaching them to the parent actor in a way that I can continue to edit their properties. I have tried using the PostEditChangeProperty but the way I have this built out it isn’t working. The actor is re-initialized every time I modify a value in the details panel, so that function doesn’t seem to get called. I think it is more for modifying instances of an object in the game world rather than the original in the editor details panel. But I may be mistaken and am just using it wrong. I have some old code I will dig up where I played with those post-edit functions.

If you happen to know more about any of this please enlighten me. Otherwise, I will respond again when I get some more information.

Thanks again.

PostEditChangeProperty() is called any time any value is changed in the details window of that class object (instance or archetype, so keep track of your Edit privileges) the PostEditChangeProperty() is the C++ equivalent of the Blueprint_ConstructionScript() with the caveat that technically the Blueprint_ConstructionScript() is both the Class_Constructor() and the PostEditChangeProperty() combined; so it can be called at runtime outside of the Engines Development mode.

when is that message being called in the UActorComponent ? could this be a lifetime thing where the the Owner is being quaried before the post attachment Compontent::Initialize() has been completed?
if the Component is created in the constructor the Component::Initialize() is postponed until after the Actor::PreInitializeComponents() has completed which means that getting the owner would not be “safe” until the Actor::PostInitializeComponents() as the thought process goes if it is being created in the constructor then it will probably be attached in the same constructor.
Anything instantiated outside of the constructor doesn’t have a delay before Component::Initialize() is invoked immediately even before attachment has been performed, and then when the attachment happens Component::Initialize() can be invoked again on the UActorComponent (in theory this should be things that are like setting default values, and registering uniquely to managers) as the Initialization chain of all starting Actors/Pawns/Characters and their components will also happen before the first BeginPlay() is invoked.

I am noticing 2 different names for your SG_BP_ThirdPersonCharacter if you are unsure of if these are “the same” UActorComponents then even UAcotrComponents get “Unique Names” though GetName() (which is defined in UObject) over and above the name you give to them in the hierarchy, and you can also attach say the Visual Studio Debugger and step through as well taking note of memory addresses for these Components in question to see if there are more of them then there should be (please note then when you hit PIE it is expected that those memory address will be different for non-static Actors as a shadow copy is made of the Editor for PIE)

@gardian206,
Again, I appreciate the help. I had to give up on the approach I was going for. With the way the editor behaves it just wasn’t feasible to continue. I split my code, working from two directions instead of one. The character class will now act as the controller for this custom code and it will search itself for any custom box components attached to it. Those custom box components implement the other half of my code. They can be attached to the character’s mesh the way I needed them to originally and run all of their checks and functions locally. The character will then query all attached components listed as active and complete the work needed.

This is what it looks like when it is all said and done.

I can now efficiently query and identify exactly what a player can and cannot see (using ray traces), what part of my creature they can see, how much of it they see, and also know when my creature is just perfectly out of sight… right at the edge of their vision. :smirk:

Thanks again for the help.