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

English original Address

To follow up the tutorial, create a new C++ Actor subclass and name it LightSwitchPushButton. We’ll define four things in the header file — we’ll define a UPointLightComponent, USphereComponent, float variable, and void function.

Here is the final header code.

LightSwitchPushButton.h

#pragma once

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

UCLASS(a)class UNREALCPP_API ALightSwitchPushButton : public AActor
{
	GENERATED_BODY(a)public:	
	// Sets default values for this actor's properties
	ALightSwitchPushButton(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 ToggleLight function
	UFUNCTION(BlueprintCallable, Category = "Light Switch")
	void ToggleLight(a);
	
};
Copy the code

Next, in our.cpp file, let’s first #include the necessary scripts we’ll use in our code. Includes Components/ PointlightComponent. h and Components/ spherecomComponent. h files.

#include "LightSwitchPushButton.h"
// include these header files
#include "Components/PointLightComponent.h"
#include "Components/SphereComponent.h"
Copy the code

We’ll set all the default properties for actor in the constructor. First let’s set our float variable LightIntensity to 3000.0F, which will make the light look bright enough relative to other objects. Next, we’ll create our UPointLightComponent and set it to our RootComponent. After that, we will create the USphereComponent, which will act as a collision sphere when our player is within the radius. We’ll then create a simple ToggleLight() function to toggle the light’s visibility state. We’ll call this function later from the player code. Here is the last.cpp file for the LightSwitchPushButton role.

LightSwitchPushButton.cpp

#include "LightSwitchPushButton.h"
#include "Components/PointLightComponent.h"
#include "Components/SphereComponent.h"

// Sets default values
ALightSwitchPushButton::ALightSwitchPushButton()
{
 	// 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; / / / < out of date
    //PointLight->SetVisibleFlag(true);
    PointLight->SetVisibility(true);
	RootComponent = PointLight;

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

// Called when the game starts or when spawned
void ALightSwitchPushButton::BeginPlay(a)
{
	Super::BeginPlay(a); }void ALightSwitchPushButton::ToggleLight(a)
{
    PointLight->ToggleVisibility(a); }Copy the code

Next, like in the previous section, let’s add an Action input to the project. In this case, we will bind the Action input to the F key on the keyboard. Go to Edit > Project Settings. Then select the Input option. Click the plus sign next to Action Mappings. Call the new input Action and select F from the drop-down menu.

 

 

Xxxcharacter.h /.cpp

In the xxxcharacter.h file, add the OnAction method under the OnFire method.

protected:
	
	/** Fires a projectile. */
	void OnFire(a);

	// on action 
	void OnAction(a);
Copy the code

In addition, we must include the LightSwitchPushButton header file so that our role can access its functionality.

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
// include our new LightSwitchPushButton header file
#include "LightSwitchPushButton/LightSwitchPushButton.h"
#include "UnrealCPPCharacter.generated.h"
Copy the code

Then declare a variable CurrentLightSwitch for the player’s currently overlapping light switch. In addition, we need to declare overlapping events that trigger the function we want to run when the player is within the radius of the light sphere component.

// 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 current light switch
class ALightSwitchPushButton* CurrentLightSwitch;
Copy the code

It also declares UCapsuleComponent to handle our trigger event

UPROPERTY(VisibleAnywhere, Category = "Trigger Capsule")
class UCapsuleComponent* TriggerCapsule;
Copy the code

Add trigger capsules to the constructor and bind them to overlapping events. Then set the variable CurrentLightSwitch to NULL.

AUnrealCPPCharacter::AUnrealCPPCharacter() {...// create trigger capsule
    TriggerCapsule = CreateDefaultSubobject<UCapsuleComponent>(TEXT("Trigger Capsule"));
    TriggerCapsule->InitCapsuleSize(55.f.96.0 f);;
    TriggerCapsule->SetCollisionProfileName(TEXT("Trigger"));
    TriggerCapsule->SetupAttachment(RootComponent);

    // bind trigger events
    TriggerCapsule->OnComponentBeginOverlap.AddDynamic(this, &AUnrealCPPCharacter::OnOverlapBegin); 
    TriggerCapsule->OnComponentEndOverlap.AddDynamic(this, &AUnrealCPPCharacter::OnOverlapEnd); 

     // set current light switch to null
     CurrentLightSwitch = NULL;
}
Copy the code

 

Further, attach the Action input binding to the player

void AUnrealCPPCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{...// Bind action event
    PlayerInputComponent->BindAction("Action", IE_Pressed, this, &AUnrealCPPCharacter::OnAction);

}
Copy the code

Add the OnAction() function to the player script. This function checks if CurrentLightSwitch is NULL. If CurrentLightSwitch is not NULL, the light visibility will be toggled when the player presses the action key F (on and off lights). Then, add an overlap function to set and cancel CurrentLightSwitch

 

void AUnrealCPPCharacter::OnAction(a) 
{
	if(CurrentLightSwitch) 
	{
		CurrentLightSwitch->ToggleLight();
	}
}

// overlap on begin function
void AUnrealCPPCharacter::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
	if(OtherActor && (OtherActor ! =this) && OtherComp && OtherActor->GetClass() - >IsChildOf(ALightSwitchPushButton::StaticClass())) { CurrentLightSwitch = Cast<ALightSwitchPushButton>(OtherActor); }}// overlap on end function
void AUnrealCPPCharacter::OnOverlapEnd(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	if(OtherActor && (OtherActor ! =this) && OtherComp) 
	{
		CurrentLightSwitch = NULL; }}Copy the code

Compile the code. Drag and drop the actor (LightSwitchPushButton) into the scene, and when the player enters the sphere trigger area, hit the F key to turn the lights on and off.

The final rendering is shown below

\