The original tutorial is based on UE 4.18, I am based on UE 4.25.

English original Address

In this tutorial, I need to make sure that the first person Character is pure, that is, there is no code in the UE 4 C++ tutorial, otherwise the switch that should trigger the light should conflict.

In this tutorial, we will create a light that turns on or off when the user enters the Actor’s USphereComponent. Create a new C++ role class and name it LightSwitchTrigger. In the header file, we will define our PointLight, USphereComponent, Overlap function, and ToggleLight function.

 LightSwitchTrigger.h

#pragma once

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

UCLASS(a)class UNREALCPP_API ALightSwitchTrigger : public AActor
{
	GENERATED_BODY(a)public:	
	// Sets default values for this actor's properties
	ALightSwitchTrigger(a);protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay(a) override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// declare point light comp
	UPROPERTY(VisibleAnywhere, Category = "Light Switch")
	class UPointLightComponent* PointLight;

	// declare sphere comp
	UPROPERTY(VisibleAnywhere, Category = "Light Switch")
	class USphereComponent* LightSphere;

	// declare light intensity variable
	UPROPERTY(VisibleAnywhere, Category = "Light Switch")
	float LightIntensity;
	
	// declare overlap begin function
	UFUNCTION(a)void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);

	// declare overlap end function
	UFUNCTION(a)void OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex);

	// declare ToggleLight function
	UFUNCTION(a)void ToggleLight(a);
};
Copy the code

Next, in.cpp, we use #include drawdebuhelpers. h to help us visualize the collision sphere.

#include "LightSwitchTrigger.h"
#include "Components/PointLightComponent.h"
#include "Components/SphereComponent.h"
// include draw debu helpers header file
#include "DrawDebugHelpers.h"
Copy the code

In the LightSwitchTrigger initialization function, we will set the light intensity to 3000.0F. Next, we’ll add a PointLight as our RootComponent. We will then add a USphereComponent (LightSphere) to the trigger sphere’s actor and attach it to the RootComponent. Then connect USphereComponent to the Overlap function we will create later.

Set the lights in the trigger

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;

	LightIntensity = 3000.0 f;

	PointLight = CreateDefaultSubobject<UPointLightComponent>(TEXT("Point Light"));
	PointLight->Intensity = LightIntensity;
	PointLight->bVisible = true;
	RootComponent = PointLight;

	LightSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Light Sphere Component"));
	LightSphere->InitSphereRadius(300.0 f);
	LightSphere->SetCollisionProfileName(TEXT("Trigger"));
	LightSphere->SetupAttachment(RootComponent);

	LightSphere->OnComponentBeginOverlap.AddDynamic(this, &ALightSwitchTrigger::OnOverlapBegin);
	LightSphere->OnComponentEndOverlap.AddDynamic(this, &ALightSwitchTrigger::OnOverlapEnd); 

}
Copy the code

Add a debug sphere in BeginPlay() with the same radius as the LightSphere.

void ALightSwitchTrigger::BeginPlay(a)
{
	Super::BeginPlay(a);DrawDebugSphere(GetWorld(), GetActorLocation(), 300.f.50, FColor::Green, true.- 1.0.2);
	
}
Copy the code

Create a function called ToggleLight() that toggles the visibility of PointLight.

void ALightSwitchTrigger::ToggleLight(a)
{
    PointLight->ToggleVisibility(a); }Copy the code

Next, we’ll create two overlapping functions that both call the ToggleLight function to ToggleLight visibility.

void ALightSwitchTrigger::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    if(OtherActor && (OtherActor ! =this) && OtherComp)
    {
        ToggleLight();
    }
}

void ALightSwitchTrigger::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
    if(OtherActor && (OtherActor ! =this) && OtherComp)
    {
        ToggleLight();
    }
}
Copy the code

The complete CPP code is shown below

#include "LightSwitchTrigger.h"
#include "Components/PointLightComponent.h"
#include "Components/SphereComponent.h"
// include draw debu helpers header file
#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;

	LightIntensity = 3000.0 f;

	PointLight = CreateDefaultSubobject<UPointLightComponent>(TEXT("Point Light"));
	PointLight->Intensity = LightIntensity;
	PointLight->bVisible = true;
	RootComponent = PointLight;

	LightSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Light Sphere Component"));
	LightSphere->InitSphereRadius(300.0 f);
	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(a)
{
	Super::BeginPlay(a);DrawDebugSphere(GetWorld(), GetActorLocation(), 300.f.50, FColor::Green, true.- 1.0.2);
	
}

// Called every frame
void ALightSwitchTrigger::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

void ALightSwitchTrigger::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    if(OtherActor && (OtherActor ! =this) && OtherComp)
    {
        ToggleLight();
    }
}

void ALightSwitchTrigger::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
    if(OtherActor && (OtherActor ! =this) && OtherComp)
    {
        ToggleLight();
    }
}

void ALightSwitchTrigger::ToggleLight(a)
{
    PointLight->ToggleVisibility(a); }Copy the code

Finally, compile the code and drag the LightSwitchTrigger into the scene. Lights will now switch on when the player enters the sphere.

Place the trigger region into the scene

 

Game operation renderings: enter the trigger area to turn off the light, leave the trigger area to turn on the light

In and out of the trigger area to switch the light on state

 

\