How to create a PointLight at runtime use C++?

I have tried several way to create PointLight use C++, but they all don’t work.

1,

APointLight* PL = NewObject<APointLight>();
if (PL)
{
	PL->AttachToActor(SelectedHero, FAttachmentTransformRules::KeepRelativeTransform);
	//FVector Loc = SelectedHero->GetActorLocation();// FRotator(90.f, 0.f, 0.f).Vector() * 1000.f;

	PL->SetActorRelativeLocation(FVector(0.f, 0.f, 1000.f));
	ULightComponent* LC = PL->GetLightComponent();
	if (LC)
	{
		LC->SetCastShadows(false);
		LC->InverseSquaredFalloff_DEPRECATED = false;
		LC->SetIntensity(20000.f);
		LC->SetLightColor(FLinearColor(199, 125, 108));
		PL->PointLightComponent->AttenuationRadius = 10000.f;
	}
}

2,

UPointLightComponent* PLC = NewObject<UPointLightComponent>();
//GetWorld()->SpawnActor<UPointLightComponent>(UPointLightComponent::StaticClass(), Loc, FRotator(0.f, 0.f, 0.f));
//PLC->AttachTo(SelectedHero->GetMesh());
//PLC->RegisterComponent();

PLC->SetWorldLocation(FVector(1200.f, 1200.f, 500.f));
//PLC->SetRelativeLocation(FVector(0.f, 0.f, 1200.f));

PLC->SetCastShadows(false);
PLC->bUseInverseSquaredFalloff = false;
PLC->SetIntensity(20000.f);
PLC->SetLightColor(FLinearColor(199, 125, 108));
PLC->SetAttenuationRadius(10000.f);

How to achieve this?

Hey-

What class are you trying to spawn your light from? I was able to use the following code in my actor class constructor:

MyLight = CreateDefaultSubobject<UPointLightComponent>(TEXT("MyLight"));
	RootComponent = MyLight;

Then in the editor when I created a blueprint based on this class, the point light was already a part of the hierarchy and appeared when the blueprint instance is placed in the level. Let me know if doing this works for you.

Cheers

Thx very much for your help! I found the reason, blueprint is need to be created, I thought PointLight can be create without blueprint at runtime.