CourseInfo
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "CourseInfo.generated.h"
DECLARE_MULTICAST_DELEGATE_TwoParams(FCourseInfoOnChangedSignature, const FString&, const FString&);
/**
 * 
 */
UCLASS()
class UNREALDELEGATE_API UCourseInfo : public UObject
{
	GENERATED_BODY()
	
public:
	UCourseInfo();
	FCourseInfoOnChangedSignature OnChanged;
	void ChangeCourseInfo(const FString& InSchoolName, const FString& InNewContents);
private:
	FString Contents;
};DECLARE_MULTICAST_DELEGATE_TwoParams 으로 델리게이트를 만드는데 인자 이름은 언리얼에서 Signature를 붙여서 만들기 때문에 똑같이 만들어 준다.
그리고, FCourseInfoOnChangedSignature 변수를 선언하고 외부에서 학사 정보를 번경 할때 호출하는 함수인 ChangeCourseInfo 함수도 선언 해준다.
// Fill out your copyright notice in the Description page of Project Settings.
#include "CourseInfo.h"
UCourseInfo::UCourseInfo()
{
	Contents = TEXT("기존 학사 정보");
}
void UCourseInfo::ChangeCourseInfo(const FString& InSchoolName, const FString& InNewContents)
{
	Contents = InNewContents;
	UE_LOG(LogTemp, Log, TEXT("[CourseInfo] 학사 정보가 변경되어 알림을 발송합니다."));
	OnChanged.Broadcast(InSchoolName, Contents);
}학사 정보를 갱신 해주고, 로그를 찍어준다. 그리고 OnChanged에 연결된 모든 함수를 호출해준다.
Student
UCLASS()
class UNREALDELEGATE_API UStudent : public UPerson, public ILessonInterface
{
	GENERATED_BODY()
	
public:
	UStudent();
	virtual void DoLesson() override;
	void GetNotification(const FString& School, const FString& NewCourseInfo);
};콜백을 위한 GetNotification 함수만 새로 만들어준다.
UStudent::UStudent()
{
	Name = TEXT("학생");
	Card->SetCardType(ECardType::Student);
}
void UStudent::DoLesson()
{
	ILessonInterface::DoLesson();
	UE_LOG(LogTemp, Log, TEXT("%s님은 가르칩니다."), *Name);
}
void UStudent::GetNotification(const FString& School, const FString& NewCourseInfo)
{
	UE_LOG(LogTemp, Log, TEXT("[Student] %s님이 %s님으로 부터 받은 메시지 : %s"), *Name, *School, *NewCourseInfo);
}그리고, 호출시 출력할 로그만 만들어준다.
MyInstance
그리고 이 델리게이트를 컨트롤할 중간 관리를 MyInstance에 추가해준다.
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "MyGameInstance.generated.h"
/**
 * 
 */
UCLASS()
class UNREALDELEGATE_API UMyGameInstance : public UGameInstance
{
	GENERATED_BODY()
public:
	UMyGameInstance();
	virtual void Init() override;
private:
	UPROPERTY()
	TObjectPtr<class UCourseInfo> CourseInfo;
	UPROPERTY()
	FString SchoolName;
};오브젝트 포인터로 만들어준다.
void UMyGameInstance::Init()
{
	Super::Init();
	CourseInfo = NewObject<UCourseInfo>(this);
	UE_LOG(LogTemp, Log, TEXT("================================="));
	UStudent* Student1 = NewObject<UStudent>();
	Student1->SetName(TEXT("학생1"));
	UStudent* Student2 = NewObject<UStudent>();
	Student2->SetName(TEXT("학생2"));
	UStudent* Student3 = NewObject<UStudent>();
	Student3->SetName(TEXT("학생3"));
	CourseInfo->OnChanged.AddUObject(Student1, &UStudent::GetNotification);
	CourseInfo->OnChanged.AddUObject(Student2, &UStudent::GetNotification);
	CourseInfo->OnChanged.AddUObject(Student3, &UStudent::GetNotification);
	CourseInfo->ChangeCourseInfo(SchoolName, TEXT("변경된 학사 정보"));
	UE_LOG(LogTemp, Log, TEXT("================================="));
}학사 정보를 선언 해주고, 각 학생들의 콜백과 오브젝트를 AddUObject를 사용해서 추가 해준다.
마지막으로 델리 게이트를 호출해주면,
LogTemp: =================================
LogTemp: [CourseInfo] 학사 정보가 변경되어 알림을 발송합니다.
LogTemp: [Student] 학생3님이 학교님으로 부터 받은 메시지 : 변경된 학사 정보
LogTemp: [Student] 학생2님이 학교님으로 부터 받은 메시지 : 변경된 학사 정보
LogTemp: [Student] 학생1님이 학교님으로 부터 받은 메시지 : 변경된 학사 정보
LogTemp: =================================이렇게 출력이 된다.
'Unreal' 카테고리의 다른 글
| 7-2. 언리얼 컨테이너 라이브러리 - TSet (1) | 2023.10.16 | 
|---|---|
| 7-1. 언리얼 컨테이너 라이브러리 - TArray (1) | 2023.10.11 | 
| 6. Delegate - 1 (0) | 2023.09.20 | 
| 5. 컴포지션 (0) | 2023.09.11 | 
| 4. 인터페이스 (0) | 2023.08.31 |