Hi, I want to create an AFlickeringLight class from the ALight class. I tried to do it, but because the light component is private, I can not access it, if I add another light component in my class, then it defeats the whole purpose of inheritance, especially when I need to use this class with my light switch class to on/off the light by accessing its light component.
This class works with the already defined light types, but I can’t use it with my class because I use a new light component instead of the one from the base class.
This is the code for my flickering light class:
FlickeringLight.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Engine/Light.h"
#include "FlickeringLight.generated.h"
class UPointLightComponent;
UCLASS()
class UDEMYHORRORGAME_API AFlickeringLight : public ALight
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFlickeringLight();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
TObjectPtr<UPointLightComponent>PointLight;
UPROPERTY(EditAnywhere, Category = "Flicker")
float MaxFlickerIntersity{ 5000.0f };
UPROPERTY(EditAnywhere, Category = "Flicker")
float MinFlickerIntersity{ 2000.0f };
UPROPERTY(EditAnywhere, Category = "Flicker")
float MinDelayTime{ 0.8 };
UPROPERTY(EditAnywhere, Category = "Flicker")
float MaxDelayTime{ 0.2 };
FLatentActionInfo FlickerActionInfo;
UFUNCTION()
void FlickerDelayCallback();
void TriggerDelay();
};
Not sure why is this happening (maybe, because ALight class is marked as Abstract? idk).
I had hard time stepping into the CreateDefaultSubobject function and debugging it.
I’m also not really sure how it is supposed to work.
However,
every Light class seems to be also implementing additional sub-class of ULightComponent and calling ALight::ALight manually, in the following way:
Create new dummy component class derived from ULightComponent (you wanted to use UPointLightComponent in your code, so u can use it instead).
In .h of your Light Actor class, declare constructor that takes const FObjectInitializer& ObjectInitializer as parameter…
… and when defining said constructor in .cpp, call Super(ObjectInitializer.SetDefaultSubobjectClass<UMyLightComponent>(TEXT("LightComponent0"))), where SetDefaultSubobjectClass function should take your dummy component class as template parameter.
Make sure to include all headers and modules
MyLight.h
#pragma once
#include "CoreMinimal.h"
#include "Engine/Light.h"
#include "Components/LightComponent.h"
#include "MyLight.generated.h"
/* step 1 - dummy component class */
UCLASS(Blueprintable)
class MYPROJECT_API UMyLightComponent : public ULightComponent
{
GENERATED_BODY()
};
UCLASS(Blueprintable)
class MYPROJECT_API AMyLight : public ALight
{
GENERATED_BODY()
/* step 2 - constructor that takes FObjectInitializer */
AMyLight(const FObjectInitializer& ObjectInitializer);
};
MyLight.cpp
#include "MyLight.h"
#include "Components/LightComponent.h"
/* step 3 - call to Super constructor */
AMyLight::AMyLight(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer.SetDefaultSubobjectClass<UMyLightComponent>(TEXT("LightComponent0")))
{}
I was really interested in this one and spend some time figuring it out.
I hope this will help you
Let me know if this works. if not, I’ll be really really annoyed…