I have a function that I have been working on for the last week or so, trying to debug (I am new to coding with C++ in Unreal Engine 4), and have been following a tutorial on Epic’s website to get this to work, to no avail. This is my current code:
#include "LightSwitchTrigger.h"
#include "Components/PointLightComponent.h"
#include "Components/SphereComponent.h"
#include "DrawDebugHelpers.h"
// Sets default values
ALightSwitchTrigger::ALightSwitchTrigger()
{
// 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;
//Setting light intesity.
LightIntensity = 3000.0f;
//Creating point light.
PointLight = CreateDefaultSubobject<UPointLightComponent>(TEXT("Point Light"));
PointLight->Intensity = LightIntensity;
PointLight->bVisible = true;
RootComponent = PointLight;
//Creating collision sphere.
LightSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Light Sphere Comp"));
LightSphere->InitSphereRadius(300.0f);
LightSphere->SetCollisionProfileName(TEXT("Trigger"));
LightSphere->SetupAttachment(RootComponent);
LightSphere->OnComponentBeginOverlap.AddDynamic(this, &ALightSwitchTrigger::OnOverlapBegin);
LightSphere->OnComponentEndOverlap.AddDynamic(this, &ALightSwitchTrigger::OnOverlapEnd);
}
// Called when the game starts or when spawned
void ALightSwitchTrigger::BeginPlay()
{
Super::BeginPlay();
DrawDebugSphere(GetWorld(), GetActorLocation(), 300.0f, 50, FColor::Purple, true, -1, 0, 2);
}
// Called every frame
void ALightSwitchTrigger::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ALightSwitchTrigger::ToggleLight() {
PointLight->ToggleVisibility();
}
//Function for usr overlapping with light collision to toggle light.
void ALightSwitchTrigger::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* PtherComp, int32
OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) {
if (OtherActor && (OtherActor != this) && OtherComp)
{
ToggleLight();
}
}
//Toggles light again when overlap ends.
void ALightSwitchTrigger::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32
OtherBodyIndex) {
if (OtherActor && (OtherActor != this) && OtherComp)
{
ToggleLight();
}
}
My problems exist in line 18:
PointLight->bVisible = true;
and in line 51 if
(OtherActor && (OtherActor != this) && OtherComp)
.
For the error in line 18, my error is error C2248: ‘USceneComponent::bVisible’: cannot access private member declared in class ‘USceneComponent’. I take this to mean I need to declare my constructor as public, but when I do so, I get more errors about a declaration being expected.
For line 51, my error is error C2065: ‘OtherComp’: undeclared identifier. I’m not sure what I’m supposed to do with this one, especially since the function immediately after this one, which contains the same bit of code, is not pinging for an error.
Just in case, I’ll include my header code as well:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Engine.h"
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "LightSwitchTrigger.generated.h"
UCLASS()
class GAM312_PROJECT_API ALightSwitchTrigger : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ALightSwitchTrigger();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
//Establishing UPROPERTY variables for UE4.
UPROPERTY(VisibleAnywhere, Category = "Light Switch")
class UPointLightComponent* PointLight;
UPROPERTY(VisibleAnywhere, Category = "Light Switch")
class USphereComponent* LightSphere;
UPROPERTY(VisibleAnywhere, Category = "Light Switch")
float LightIntensity;
//Establishing UFUNCTIONS for UE4.
UFUNCTION()
void ToggleLight();
UFUNCTION()
void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* PtherComp, int32
OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
UFUNCTION()
void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32
OtherBodyIndex);
};
Any help in understanding how I can debug this and get it to work as intended while providing insight on the fix would be much appreciated, especially since I feel like I am missing something extremely basic.