C++ Attaching Components

image
I was wondering how i would go about transferring this into code. So far I have declared the two components and have them visible in the editor. However i am struggling with attaching the two, i have tried using - SetUpAttachment and AttachToComponent

What’s the issue you are seeing? Assuming this is a static mesh component, in your header file you would have something like:

class MyClass : AActor
{
public:
    MyClass();

protected:
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    class UStaticMeshComponent* MyStaticMeshComponent;
}

And in your cpp file:

MyClass::MyClass()
{
    MyStaticMeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("name you want for your component in blueprints"));
    MyStaticMeshComponent->SetupAttachment(RootComponent);
}
1 Like

Hi, im trying to get it so the two are attached and share a location. I can do this easily in blueprint just by dragging the climb indicator onto the blink particle system but im trying to set this up in c++.

Whenever i set up an attachment in the constructor, etiher to the component root or the blink particle sytem component it doesnt seem to change anything.

Also in case its relevant - the blink particle system is not the default scene root component.

You can try trouble shooting it by calling the attachment at PostRegisterAllComponents, or at BeginPlay if you don’t need to edit the position in the Blueprints editor (in some cases where things are generated more dynamically this can help), might be worth sharing the code you are using.

Additional note, you probably always need to restart the editor to compile constructor changes or the attachment may not work properly.

1 Like

This contructor works, there is one condition. You particle system has to work in local space
localSpace

AMainActor::AMainActor()
{

	PrimaryActorTick.bCanEverTick = false;

	BlinkParticleSystem = CreateDefaultSubobject<UNiagaraComponent>(TEXT("Blink Particle System"));
	BlinkParticleSystem->SetMobility(EComponentMobility::Movable);
	SetRootComponent(BlinkParticleSystem);
		
	ClimbIndicator = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Climb Indicator"));		
	ClimbIndicator->SetupAttachment(BlinkParticleSystem);		
		
}


1 Like

Thanks for the help guys. Thats it sorted. Think SJ had solved it for me but seeing ravens helped put it together. :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.