1.게임모드 헤더에 변경된 코드를 추가한다.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "GameModeBase_05.generated.h"
/**
*
*/
DECLARE_MULTICAST_DELEGATE(FMulitiCastDelegatsSignature)
UCLASS()
class WILLIAM_API AGameModeBase_05 : public AGameModeBase
{
GENERATED_BODY()
public:
FMulitiCastDelegatsSignature MyMulticastDelegate;
};
2.새로운 Actor C++ 클래스를 만든다.
#pragma once
#include "CoreMinimal.h"
#include "Components/PointLightComponent.h"
#include "GameFramework/Actor.h"
#include "MuliticasDelegateListener.generated.h"
UCLASS()
class WILLIAM_API AMuliticasDelegateListener : public AActor
{
GENERATED_BODY()
public:
UFUNCTION()
void ToggleLight();
UFUNCTION()
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
UPROPERTY()
UPointLightComponent* PointLight;
FDelegateHandle MyDelegateHandle;
public:
// Sets default values for this actor's properties
AMuliticasDelegateListener();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
// Fill out your copyright notice in the Description page of Project Settings.
Cpp
#include "MuliticasDelegateListener.h"
#include "GameModeBase_05.h"
#include "Kismet/GameplayStatics.h"
AMuliticasDelegateListener::AMuliticasDelegateListener()
{
PrimaryActorTick.bCanEverTick = true;
PointLight = CreateDefaultSubobject<UPointLightComponent>("PointLight");
RootComponent = PointLight;
PointLight->SetLightColor(FLinearColor::Green);
}
void AMuliticasDelegateListener::BeginPlay()
{
Super::BeginPlay();
UWorld* TheWorld = GetWorld();
if(TheWorld!=nullptr)
{
AGameModeBase* GameMode = UGameplayStatics::GetGameMode(TheWorld);
AGameModeBase_05* MYGameMode = Cast<AGameModeBase_05>(GameMode);
if(MYGameMode!=nullptr)
{
MyDelegateHandle = MYGameMode->MyMulticastDelegate.AddUObject(this, &AMuliticasDelegateListener::ToggleLight);
}
}
}
void AMuliticasDelegateListener::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AMuliticasDelegateListener::ToggleLight()
{
PointLight->ToggleVisibility();
}
void AMuliticasDelegateListener::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
Super::EndPlay(EndPlayReason);
UWorld* TheWorld = GetWorld();
if (TheWorld != nullptr)
{
AGameModeBase* GameMode = UGameplayStatics::GetGameMode(TheWorld);
AGameModeBase_05* MYGameMode = Cast<AGameModeBase_05>(GameMode);
if (MYGameMode != nullptr)
{
MYGameMode->MyMulticastDelegate.Remove(MyDelegateHandle);
}
}
}
MyTrigger.Cpp

- 실행결과



기존 표준 단일 바인딩보다는 멀티캐스트 델리게이트는 명시적으로 선언해야한다.
새로만든 Actor클래스는 원래의 DelegateListener과 매우 비슷하지만 큰 차이점은 FDelegateHandler 사용에 있다.
또한 호출해야하는 함수 부분에서도 BrodCast()함수를 사용한다.
'UnraealEngine' 카테고리의 다른 글
| Unreal4 이벤트와 델리게이트 처리하기 5)TimeofDay 핸들러 생성 (0) | 2021.08.25 |
|---|---|
| Unreal4 이벤트와 델리게이트 처리하기 5)사용자 정의 이벤트 생성 (0) | 2021.08.24 |
| Unreal4 이벤트와 델리게이트 처리하기 3)입력 파라미터를 사용하는 델리게이트 생성 (0) | 2021.08.24 |
| Unreal4 이벤트와 델리게이트 처리하기 2)델리게이트 등록 및 해제 (0) | 2021.08.24 |
| Unreal4 이벤트와 델리게이트 처리하기 1)가상 함수로 구현된 이벤트 핸들링 (1) | 2021.08.24 |