- Add the following event declaration macro to the header of your MyTriggerVolume class:
DECLARE_EVENT(AMyTriggerVolume, FPlayerEntered)
- Add an instance of the declared event signature to the class:
FPlayerEntered OnPlayerEntered;
- In AMyTriggerVolume::NotifyActorBeginOverlap, add the following code:
OnPlayerEntered.Broadcast();
- Create a new Actor class called TriggerVolEventListener:
- Add the following class members to its declaration:
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Components/PointLightComponent.h"
#include "MyTriggerVolume.h"
#include "TriggerVolEventListener.generated.h"
UCLASS()
class CHAPTER_05_API ATriggerVolEventListener : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ATriggerVolEventListener();
UPROPERTY()
UPointLightComponent* PointLight;
UPROPERTY(EditAnywhere)
AMyTriggerVolume* TriggerEventSource;
UFUNCTION()
void OnTriggerEvent();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
- Initialize PointLight in the class constructor:
// Sets default values
ATriggerVolEventListener::ATriggerVolEventListener()
{
// 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;
PointLight = CreateDefaultSubobject<UPointLightComponent>
("PointLight");
RootComponent = PointLight;
}
- Inside BeginPlay, add the following code:
// Called when the game starts or when spawned
void ATriggerVolEventListener::BeginPlay()
{
Super::BeginPlay();
if (TriggerEventSource != nullptr)
{
TriggerEventSource->OnPlayerEntered.AddUObject(this,
&ATriggerVolEventListener::OnTriggerEvent);
}
}
- Lastly, implement OnTriggerEvent():
void ATriggerVolEventListener::OnTriggerEvent() { PointLight->SetLightColor(FLinearColor(0, 1, 0, 1)); }
- Compile your project and launch the editor. Create a level with the game mode set to our Chapter_05GameModeBase, and then drag an instance of ATriggerVolEventListener and AMyTriggerVolume out into the level.
- Select TriggerVolEventListener, and you'll see TriggerVolEventListener listed as a category in the Details panel, with the property asĀ Trigger Event Source:
The Trigger Vol Event Listener category
- Use the drop-down menu to select your instance of AMyTriggerVolume so that the Listener knows which event to bind to:
- Play your game and enter the trigger volume's zone of effect. Verify that the color of your EventListener changes to green: