Forcing component hierarchy attachment

Hi all,
I’ve been searching for an answer for days, but every time I get to a promising keyword, it turns out to not work.
What I’m trying to do is to create a custom USphereComponent, such that when I add it to an actor in blueprint through the green [+ Add Component] button, it will show up with a child component (lets say, a UStaticMeshComponent) attached to it by default.
Generally, I would like to know how to do this with any 2 USceneComponents.
Just for example, the component constructor looks like this:
UMySphere::UMySphere()
{
SphereComponent = CreateDefaultSubobject(TEXT(“TestChild”));
SphereComponent->SetupAttachment(this);
// or:
// SphereComponent->AttachToComponent(this, FAttachmentTransformRules::KeepRelativeTransform);
}
But neither (and many more) don’t seem to work. I would like to see it so:

285912-capture.jpg

But it shows up just as “MySphere” without anything attached.
Many thanks!

It would be easier to help you out if you used a real, compiling source code example. The one above definitely does not compile (CreateDefaultSubobject needs a template parameter). Also would help if you used the code formatting option in your question so the code doesn’t read a prose.

Oh Geez I havent even noticed that the entire code went berserk. Fixing, thanks!

Could not find the “Edit” button here. Apologies for posting an answer as an edit.

I’ll also try to simplify what was written beforehand: I’m trying to get to a state where I can create a custom USceneComponent - derived class, that when instantiated in an actor tree, will already create its own sub-components automatically. The following example is for a USphereComponent with a static mesh on top of it, but this could be later generalized for a more complex hierarchy. Code for header:

#pragma once

#include "CoreMinimal.h"
#include "Components/SphereComponent.h"
#include "Components/StaticMeshComponent.h"
#include "MySphereComponent.generated.h"

/**
 For now, only a constructor and intended subobject exists.
 */
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class ROBOTTEST_API UMySphereComponent : public USphereComponent
{
	GENERATED_BODY()

public:
	UMySphereComponent();
	UStaticMeshComponent* SphereComponent;	
};

and the implementation:

#include "MySphereComponent.h"

UMySphereComponent::UMySphereComponent()
{
	// None of the following (and more attempts) work:
	SphereComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("fuckingTest"));
	//SphereComponent->SetupAttachment(this);
	//SphereComponent->AttachTo(this);
	SphereComponent->AttachToComponent(this, FAttachmentTransformRules::KeepRelativeTransform);
}

when i add it to the tree, what I get is:

285954-capture2.jpg

and what i’m trying to get (automatically when adding the component!) is:

285955-capture.jpg

Hope this makes more sense than the mess I’ve had before.
Cheers :slight_smile:

Thanks, I didn’t notice the message had gone all ruined because I didnt use code formatting.
I actually already commented and reposted my question here but its gone again. I’ll try to figure out whats going on here…

On the bottom of your original post you should see a small edit button if you are signed in and it is one of your posts. Here’s an example of one of my posts.

Yeah, that’s where these things are usually found. my post doesn’t have that, only the “more” arrow.
I would attach a photo but this also is buggy right now. Well, nvm, at least I know it should be there somehow.