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

 

  • 실행결과

 

 

처음 모습 조명을 5개 배치하였다.

 

 

지나가면 모두 보이지 않는다. ToggleSetVisible함수
다시 지나간다면 다시 활성화된다.

 

기존 표준 단일 바인딩보다는 멀티캐스트 델리게이트는 명시적으로 선언해야한다.

 

새로만든 Actor클래스는 원래의 DelegateListener과  매우 비슷하지만 큰 차이점은 FDelegateHandler 사용에 있다.

또한 호출해야하는 함수 부분에서도 BrodCast()함수를 사용한다.

+ Recent posts