Create light in editor using c++?

I am trying to write a function in an in-editor plugin that creates a point light. This is what i have:

.h

#pragma once

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

UCLASS()
class  AvpCreate : public AActor
{
	GENERATED_BODY()
public:

	UPROPERTY()
	class UPointLightComponent* PointLight1;

	AvpCreate(const FObjectInitializer& ObjectInitializer);

	UFUNCTION()
	static void MakePLight(FString PLightName, FVector location);

};

.cpp

#include "m2uPluginPrivatePCH.h"
#include "vpCreate.h"

AvpCreate::AvpCreate(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	PointLight1 = ObjectInitializer.CreateDefaultSubobject<UPointLightComponent>(this, "PointLight1");

}

void AvpCreate::MakePLight(FString PLightName, FVector location)
{
	AvpCreate create;
	UClass* ClassRef = create.PointLight1->GetClass();

	FTransform ActorTransform = FTransform::Identity;
	GEditor->AddActor(GEditor->GetEditorWorldContext().World()->GetCurrentLevel(), ClassRef, ActorTransform);

}

Is this the right way to call the constructor? ‘GetClass’ is wrong, what is the correct way to get the class of a light for the ‘AddActor’ function?
Since I am using regular lights, do i even need to create a custom class, or can I just use the existing editor PointLight Class somehow?

Thanks!

Hey -

Without knowing how you’re planning on using the point light it’s difficult to answer the question exactly. It is possible to create a point light in code using a UPointLightComponent pointer. As with other components, adding the pointer in the header and then creating it inside the constructor of the class with CreateDefaultSubobject will add a pointlight to the class and any blueprint created from the class. Let me know if this information helps or if you are trying to do something else.

Cheers

Hi , Thanks for getting back to me. What I actually need to do is create a light inside another function, in an in-editor plugin.
No blueprints involved.
Is my code above mostly valid? What do I need to change in the MakePLight function to make it work?

Solved!

I just needed the editor code:

APointLight* PointLight = Cast<APointLight>(GEditor->AddActor(GEditor->GetEditorWorldContext().World()->GetCurrentLevel(), APointLight::StaticClass(), Transform));

Solved by using the editor code. Thanks again.