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?
- 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;
- 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.
- character.cpp
APlayerCharacter::APlayerCharacter()
{
PrimaryActorTick.bCanEverTick = false;
...
FlashlightComponent = CreateDefaultSubobject<UFlashlightComponent>(TEXT("FlashlightComponent"));
FlashlightComponent->SetupAttachment(GetCapsuleComponent());
...
}
-
In Blueprint Editor
-
In Game



