Component in wrong location and not attached

Hello!

I have tried to make a custom component which has funtionalities as flashlight and is attached to a character.

I made the Flashlight Component and Character in C++ class first, and create them as blueprints based on the c++.

The custom component is attached fine in the blueprint editor, but if I start game to test, the component in wrong location, world location 0,0,0, not attached to the character. Also the components are separate…

Could anybody help about this problem, if I code wrong or should add something more?

  1. component.h
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class MYPROJECT_API UFlashlightComponent : public USceneComponent
{
	GENERATED_BODY()

...
public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	TObjectPtr<USpringArmComponent> LightSpringArm;

	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	TObjectPtr<USpotLightComponent> SpotLight;
  1. component.cpp
UFlashlightComponent::UFlashlightComponent()
{
	PrimaryComponentTick.bCanEverTick = true;

	bWantsInitializeComponent = true;

	LightSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("LightSpringArmComponent"));
	LightSpringArm->TargetArmLength = 0.f;
	LightSpringArm->bUsePawnControlRotation = true;
	LightSpringArm->bEnableCameraRotationLag = true;
	LightSpringArm->CameraRotationLagSpeed = 12.f;
	LightSpringArm->SetupAttachment(this);

	SpotLight = CreateDefaultSubobject<USpotLightComponent>(TEXT("SpotLightComponent"));
	SpotLight->AttenuationRadius = 800.f;
	SpotLight->InnerConeAngle = 23.5f;
	SpotLight->OuterConeAngle = 31.f;
	SpotLight->SetVisibility(false);
	SpotLight->SetLightColor(FLinearColor(255, 250, 227));
	SpotLight->SetupAttachment(LightSpringArm);
}

Here I also tried AttachToComponent with RelativeLocation rule instead of SetupAttachment, but it had also same problem.

  1. character.cpp
APlayerCharacter::APlayerCharacter()
{
	PrimaryActorTick.bCanEverTick = false;

...
	FlashlightComponent = CreateDefaultSubobject<UFlashlightComponent>(TEXT("FlashlightComponent"));
       FlashlightComponent->SetupAttachment(GetCapsuleComponent());
...
}
  1. In Blueprint Editor

  2. In Game

Nested components don’t work well when instantiated outside of the main actor. Either spawn all of the components on the main actor or have the flashlight be it’s own actor and attach it to the character.

Unreal’s architecture is a bit stupid in that way. It’s not like game objects in Unity. Each component goes through it’s own registry and initialization process and nested components for some reason don’t work well with this process.

If someone has had better luck with nested components then I’m all ears.

I tried a version where you all the attach code later down the line via a setup command

void UFlashlightComponent::SetupFlashlight(USceneComponent* parent)
{
	SpotLight->SetupAttachment(LightSpringArm);
	LightSpringArm->SetupAttachment(parent);
}

Then later called inside of the character CDO

FlashlightComponent = CreateDefaultSubobject<UFlashlightComponent>(TEXT("FlashlightComponent"));

FlashlightComponent->SetupFlashlight(GetCapsuleComponent());

FlashlightComponent.cpp (1.7 KB)
FlashlightComponent.h (1.0 KB)
FlashLightTestCharacter.cpp (5.6 KB)
FlashLightTestCharacter.h (2.3 KB)

With visibility turned on

1 Like

You saved me, thank you so much!!!

1 Like

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