Relative location broken for C++ created component

Hello,

When I create a component in C++ from my actor, when I spawn a blueprint on the level (in editor as at runtime), relative location is broken and my component is placed at 0 0 0 world location, I need to reset relative location to make it retrieving default location.

It is embarassing when I want to place a component with an offset if created in C++. I saw this issue since UE 4.10 (may be the issue is present in older versions too, I not checked it) and I am currently on 4.12.5.

Is it a bug or I miss something with initialization ?

Thanks

2 Likes

Hey HellSuffering,

Can you provide a code sample where you set the relative location of you component?

Greetings

I do not set relative location in code, I just move my component in blueprint edior viewport. And when I place blueprint on level the component is just placed far away.

Hi HellSuffering,

I have looked into your issue and I am not seeing anything wrong when attaching one component to another, after creating a AActor in C++ and editing the components when creating a Blueprint child class of the AActor.

Here is the code for creating the components and how I am attaching one to another:

[.h]

#pragma once

#include "GameFramework/Actor.h"
#include "RelativeActor.generated.h"

UCLASS( )
class AH453513_API ARelativeActor : public AActor
{
	GENERATED_BODY( )
	
public:	
	ARelativeActor( );

	virtual void BeginPlay( ) override;

	// particle system component
	UPROPERTY( EditAnywhere, Category="Component" )
	UParticleSystemComponent *System;
	
	// static mesh component
	UPROPERTY( EditAnywhere, Category="Component" )
	UStaticMeshComponent *Mesh;
	
};

[.cpp]

#include "RelativeActor.h"    

ARelativeActor::ARelativeActor()
{
	PrimaryActorTick.bCanEverTick = false;

	// create mesh
	Mesh = CreateDefaultSubobject<UStaticMeshComponent>( TEXT("Mesh") );
	if( Mesh )
	{
		// set as root component
		SetRootComponent( Mesh );
		static ConstructorHelpers::FObjectFinder<UStaticMesh> MeshAsset( TEXT( "StaticMesh'/Engine/BasicShapes/Sphere.Sphere'" ) );
		if( MeshAsset.Succeeded( ) )
		{
			// assign sphere as mesh
			Mesh->SetStaticMesh( MeshAsset.Object );
		}
	}

	// create particle system
	System = CreateDefaultSubobject<UParticleSystemComponent>( TEXT("System") );
	if( System )
	{
		static ConstructorHelpers::FObjectFinder<UParticleSystem> SystemAsset( TEXT( "ParticleSystem'/Game/DefaultSystem.DefaultSystem'" ) );
		if( SystemAsset.Succeeded( ) )
		{
			// set default system as particle system
			System->SetTemplate( SystemAsset.Object );
		}
	}
}

void ARelativeActor::BeginPlay( )
{
	// make sure mesh and system exist before attaching.
	if( Mesh && System )
	{
		// Attach the particle system to the static mesh : using snap to target and weld simulated bodies
		System->AttachToComponent(Mesh, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true) );
	}
}

If you are still having issues, please update your post with a code sample so I am able to further investigate.

Hey, , sorry for bothering you, can you possibly take a look at this question? Contents of StaticMeshComponent disappear upon changing ChildActorComponent - Programming & Scripting - Epic Developer Community Forums it has the same symptom, child actor that i’m attempting to use in that question actually was created through c++ and has same symptom as in this question, and using this child actor causes parent pawn to break. I will actually try attaching them in the way you posted here, maybe that was a reason of this havoc, but anyway it doesnt look like expected behaviour… something shady is going on there.

btw, AttachToComponent didnt fixed anything for me, i’m creating component pointers in the same way, except i’m setting their actual mesh and ps data in derived blueprint class. When i drop it in to level particle system still spawns at 0.0.0 while mesh(root) spawns at dragged location. Particle system does move around with mesh though retaining initial distance between them.
And it does not seems to be issue with ps or mesh - if i recreate same thing as pure blueprint class - everything is attached correctly.

hey HellSuffering, do you stil suffer or was able to resolve the issue? I’m experiencing the same hell myself.

(I know that i’m bad at puns)

Hey @, can someone take a close look at this issue and/or create a bug report? Looks like it’s a legitimate bug.

Hey -

Can you explain exactly what you’re doing and the behavior you’re getting? Particularly, what is setup in code and what is setup/changed in blueprints? If you’re able to reproduce this, can you provide the setup steps you’re using?

Sure, the same way as in this answer - i’ve set up UParticleSystemComponent* and UMeshComponent* in .h, initialised them through CreateDefaultSubobject in class constructor in cpp and set . That’s it. I was not setting actual mesh or particles asset in c++, instead, at this point, i’ve created child blueprint from this class and set mesh/ps on components through bp interface. Adding particlesComp->AttachToComponent(meshComp,...) in class constructer and\or in beginPlay did nothing. As soon as i drag bp class in to a view and drag it around (wihthout dropping it yet) i can see that only mesh being dragged around and ps is still spawned at 0.0.0. If i’ll drag it around after i’ve dropped in to view, both components will change their global position but remain the same initial broken relative one.

1 Like

Hey -

Thank you for submitting a bug report. I have reproduced this issue and logged a report for it here Unreal Engine Issues and Bug Tracker (UE-40054) . You can track the report’s status as the issue is reviewed by our development staff. Please be aware that this issue may not be prioritized or fixed soon.

Cheers

Thanks, , glad that it’s at least in bug tracker now.
Also i’ve noticed in description of bug you specifically mentioned " if AttachToComponent / SetupAttachment / etc. is not expressly used from code" so does using AttachToComponent actually fixes issue for you? Cos for me with this setup AttachToComponent has no effect.

That’s correct. When I add TestParticle->AttachToComponent(RootMesh, FAttachmentTransformRules(EAttachmentRule::[AnyRule], true)); to my constructor and compile, new instances of the class/child blueprint will have the particle system attached at the root actor’s origin. Without this line of code, I saw the same behavior you described.

huh, pretty sure it had no effect for me, will recheck today.

Yep seems to be working just fine if I use SetupAttachment in constructor.

Hey Omar-

Can you elaborate on what is happening in your case? Can you provide the code used that shows this behavior? Are you using SetupAttachment to attach your component to the root component of the actor?

I’ve the same issue with no solution till now

Hi, I’m having the same problem right now.
I overrode OnConstruction() and I’m calling a function there, which creates ChildActors, attaches them and SetRelativeTransform().

Relevant Code Snippets:

ChildActor = NewObject<UChildActorComponent>(this, UChildActorComponent::StaticClass(), "ChildActorTile");
			ChildActor->SetupAttachment();
			ChildActor->SetRelativeTransform(FinalTransform);

ChildActor->SetChildActorClass(SingleTile);

			ChildActor->RegisterComponent();
			ChildActor->CreateChildActor();

Unfortunately, the ChildActors always spawn at the World Origin and don’t move when I move the Parent Actor. They are set to movable.

Hey blueshifted-

It appears you’re adding a component to the actor as it’s placed in the level / spawned. If the component will always be created as part of the actor, it would probably work better to add the component in the actor’s Constructor rather than OnConstruction().

I have the same problem as blueshifted, I can’t add the component in constructor because I need to create it dynamically. What should I do?