C++ only gameplay tutorial not working on 4.21

Trying to learn c++ programming for Unreal. Thought I’d give the documentations a try. Tried to follow the the c++ only light gameplay tutorial and I keep getting errors:

Errors:

This is my code

.h code


#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "LightSwitchOverlapCodeOnly.generated.h"

UCLASS()
class CPPTRIGGERLEVEL_API ALightSwitchOverlapCodeOnly : public AActor
{
    GENERATED_BODY()

public:    
    //Point Light Component
    UPROPERTY(VisibleAnywhere, Category = "Switch Components")
        class UPointLightComponent* PointLight1;

    //Box Component
    UPROPERTY(VisibleAnywhere, Category = "Switch Components")
        class UBoxComponent* Box1;

    // Sets default values for this actor's properties
    ALightSwitchOverlapCodeOnly();

    //Called when something enters the box component
    UFUNCTION()
        void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

    //Called when something leaves the box component
    UFUNCTION()
        void OnOverlapEnd(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

    //Toggles the light component's visibility
    UFUNCTION()
        void ToggleLight();

    //The desired intensity for the light
    UPROPERTY(VisibleAnywhere, Category = "Switch Variables")
        float DesiredIntensity;

};

.cpp code
​​​​​​​


#include "LightSwitchOverlapCodeOnly.h"


// Sets default values
ALightSwitchOverlapCodeOnly::ALightSwitchOverlapCodeOnly()
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

    DesiredIntensity = 3000.0f;

    PointLight1 = CreateDefaultSubobject<UPointLightComponent>(TEXT("PointLight1"));
    PointLight1->Intensity = DesiredIntensity;
    PointLight1->bVisible = true;
    RootComponent = PointLight1;

    Box1 = CreateDefaultSubobject<UBoxComponent>(TEXT("Box1"));
    Box1->InitBoxExtent(FVector(10.0f, 20.f, 5.f));
    Box1->SetupAttachment(RootComponent);
    Box1->OnComponentBeginOverlap.AddDynamic(this, &ALightSwitchOverlapCodeOnly::OnOverlapBegin);
    Box1->OnComponentEndOverlap.AddDynamic(this, &ALightSwitchOverlapCodeOnly::OnOverlapEnd);

}

void ALightSwitchOverlapCodeOnly::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    //GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, FString::Printf(TEXT("%s has Overlapped"),OtherActor->GetName()));

}

void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
//    GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("I have exited"));
}

Can’t understand where to look. Please help me figure this out.
Thanks,
Regards,

Before you can use a function from another class you need to include it.

Eg. In the .h (header) file you do what is known as “Forward declaration” with the “class” keyword before “UPointLightComponent* PointLight1;”

Forward declaration only give the compiler information about how much memory to allocate but it still has no knowledge of all the functions within the UPointLightComponent class.

At the top of the .cpp file you should add


#include "Components/PointLightComponent.h"

to include the PointLightComponent.

You can use the Unreal API documentation to find the header you need to include for a particular class.

You are also missing


#include "Components/BoxComponent.h"

2 Likes

Thanks for the help. My issue has been resolved… Appreciate the time.