C++ Components with sub components

Hiya! I’m trying to make a system for a building game where I can place modular sockets and ports on objects throughout the world. You can imagine this concept as similar to data ports and power ports on a PC, where you have ports of varying types, pin counts, etc that conform to a standard.

My initial idea for doing this was to use scene components that contained all the logic, data, interactions, and graphics for each type of port, and then place them on the user placeable object actors. The main issue I’m running into is that I think I’ll need sub-components or something similar to achieve this. I’ll need two static meshes, multiple colliders, and perhaps particles and sounds to boot, and that’s just my first thoughts so I’m sure I’ll need more features as I move on. Is there any way to achieve something like that? I am using C++, but will likely put these port components on blueprint actors as well as C++ actors.

1 Like

Yes, make scene component. One of features of scene components if fact that it can contain other scene components, this action is called attaching.

Make that Port component and that port component code will attach static mesh component to it self to define there apperance.

Alternatively you can extend from UStaticMeshComponent and just set mesh and add game play code to it, it should display mesh normally and execute that extra code. But first option let developer to modify appearance of components more smoothly as they can attach more components to it themselves in bluepritnt editor

there also option to use Child Actor Component which allows to use any actor which contains components defining there appearance as a component in other actor, this also allows to use port out side of the actor.

1 Like

Lol I was actually in the middle typing up my own response saying basically the same thing. I reworded my Google search and found some threads saying that was possible. After making some custom Blueprint Scene components in the past, I was under the impression they couldn’t actually have sub-components at all!
For more context for people visiting this later, you can in fact instantiate components from a SceneComponent’s constructor and attach and manage them from that SceneComponent. This allows you to have whatever component features you need on any custom C++ SceneComponent.

i just added other possible option for you if you interested :slight_smile:

Tried adding subcomponents out. It kind of works. The sub components don’t show up in the blueprint editor in the hierarchy. Also if you have a SceneComponent and add another one as a subcomponent, the editor duplicates all properties in one window instead of treating it as a separate component.

That plus there is no viewport if you create a blueprint of this component. Even if its a static mesh component. These issues tell me that the idea of subcomponents just happens to kind of work but wasn’t really a requirement of their current implementation.

Since I have ran into these issues I feel like my only option is making a separate actor and using the Child Actor component to spawn it. You essentially get the same result, but some annoyances of interactions and references between the main parent actor and its “child actor”.

Thanks bro, you help me a lot too. :+1:

Find a weird thing : When use CreateDefaultSubobject(TEXT(“CaptureCamera”));, the camera can’t follow parent component’s transform. But when add the camera to a Arrow in SceneComponent’s constructor, the camera can follow parent component’s transform. So weird…

UAAAComponent::UAAAComponent():
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don’t need them.
PrimaryComponentTick.bCanEverTick = true;
FakeRoot = CreateDefaultSubobject(TEXT(“Arrow”));
FakeRoot->SetupAttachment(this);

Camera = CreateDefaultSubobject(TEXT(“Camera”));
Camera->AttachToComponent(Arrow, FAttachmentTransformRules::KeepRelativeTransform);
}

1 Like

I think Unreal may have difficulty serializing components with component outers, so if attaching components to other components, I recommend that an actor still be set as the outer.
I recently had to pass a hit result via a TargetData struct (Gameplay Ability System), and every time a component of a component is the TargetComponent of the hit result, I get the following error:

LogTemp: LoadPackage: BottomBoxComponent
LogPackageName: SearchForPackageOnDisk took   0.541s, but failed to resolve BottomBoxComponent.
LogUObjectGlobals: Warning: LoadPackage can't find package BottomBoxComponent.
LogNetPackageMap: Warning: UPackageMapClient::InternalLoadObject: Unable to resolve default guid from client: PathName: BottomBoxComponent, ObjOuter: NULL 
LogNetPackageMap: Warning: Long net serialize: 541.614319ms, Serialized Object None
1 Like