関数ポインタのような関数ラッパの一種
デリゲートには静的/動的、シングルキャスト/マルチキャストの分類がありBlueprint関数を格納できるのは動的マルチキャストのみ
以下違い
以下例
MyVallbackExample.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyCallbackExample.generated.h"
//デリゲートの宣言はグローバル領域に書くのが多いらしい
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FZeroInputDelegate);
//パラメータは最大で8個、法則としてTwoがEightになり、引数はTypeName,VariableNameのように増やしていく
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FTwoInputDelegate, float, Val1, int32, Val2);
UCLASS()
class AMyCallbackExample : public AActor
{
GENERATED_BODY()
protected:
virtual void BeginPlay()override;
public:
//BPからデリゲートにアサインできるようにするにはBlueprintAsignableをつける
UPROPERTY(BlueprintAssignable, Category = "UECppBook")
FZeroInputDelegate TheZeroInputDelegate;
UPROPERTY(BlueprintAssignable, Category = "UECppBook")
FTwoInputDelegate TheTwoInputDelegate;
UFUNCTION(BlueprintCallable, Category = "UECppBook")
void ExecuteDelegate(const float FloatVal, const int32 IntVal);
};
MyCallbackExample.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyCallbackExample.h"
void AMyCallbackExample::ExecuteDelegate(const float FloatVal, const int32 IntVal)
{
//ブロードキャストで登録した関数をすべて実行
TheZeroInputDelegate.Broadcast();
TheTwoInputDelegate.Broadcast(FloatVal, IntVal);
}
void AMyCallbackExample::BeginPlay()
{
Super::BeginPlay();
UE_LOG(LogTemp, Display, TEXT("MyCallbackExample Beginplay start"));
}