多播委托
1、可以绑定多个回调函数,所有绑定的回调函数都会执行,实质是维持了一个单播委托的数组没有返回值支持参数不支持反射以及序列化
绑定多播委托
Add:将函数委托添加 到该多播委托的调用列表中。
AddStatic:添加原始C++指针全局函数委托
AddRaw:添加原始C++指针委托。原始指针不使用任何类型的引用,因此如果从委托下面删除了对象,则调用此函数可能不安全。调用Execute()时请小心!
AddSP:添加基于共享指针的(快速、非线程安全)成员函数委托。共享指针委托保留对对象的弱引用。
AddUObject:添加基于UObject的成员函数委托。UObject委托保留对对象的弱引用。
Remove:从该多播委托的调用列表中删除函数(性能为O(N))。请注意,委托的顺序可能不会被保留!
RemoveAll:从该多播委托的调用列表中删除绑定到指定UserObject的所有函数。请注意,委托的顺序可能不会被保留!
TestCharacter.h:
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "TestCharacter.generated.h"
UCLASS(config=Game)
class ATestCharacter : public ACharacter
{
GENERATED_BODY()
public:
ATestCharacter();
virtual void EventBegin();
void output();
void output1();
};
TestCharacter.cpp:
// Copyright Epic Games, Inc. All Rights Reserved.
#include "TestCharacter.h"
#include "HeadMountedDisplayFunctionLibrary.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"
//
// ATestCharacter
ATestCharacter::ATestCharacter()
{
// Set size for collision capsule
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
// set our turn rates for input
BaseTurnRate = 45.f;
BaseLookUpRate = 45.f;
// Don't rotate when the controller rotates. Let that just affect the camera.
bUseControllerRotationPitch = false;
bUseControllerRotationYaw = false;
bUseControllerRotationRoll = false;
// Configure character movement
GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
GetCharacterMovement()->JumpZVelocity = 600.f;
GetCharacterMovement()->AirControl = 0.2f;
// Create a camera boom (pulls in towards the player if there is a collision)
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
CameraBoom->SetupAttachment(RootComponent);
CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character
CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
// Create a follow camera
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
// Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
// are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
}
void ATestCharacter::output() {
GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Green, TEXT("Sccessful"));
}
void ATestCharacter::output1()
{
GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Green, TEXT("Sccessful Again"));
}
Delegator.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TestCharacter.h"
#include "Delegator.generated.h"
DECLARE_MULTICAST_DELEGATE(DeleLoader);
UCLASS()
class TEST_API ADelegator : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ADelegator();
DeleLoader Binding();
private:
DeleLoader Dele;
class ATestCharacter* Testor = nullptr;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
Delegator.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "Delegator.h"
#include "TestCharacter.h"
// Sets default values
ADelegator::ADelegator()
{
// 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;
}
DeleLoader ADelegator::Binding()
{
if (Testor == nullptr) Testor = NewObject<ATestCharacter>();
Dele.AddUObject(Testor, &ATestCharacter::output);
Dele.AddUObject(Testor, &ATestCharacter::output1);
return Dele;
}
// Called when the game starts or when spawned
void ADelegator::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ADelegator::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
.DelegateInvoker.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Delegator.h"
#include "DelegateInvokor.generated.h"
UCLASS()
class TEST_API ADelegateInvokor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ADelegateInvokor();
UFUNCTION(BlueprintCallable)
void Invoker();
private:
ADelegator* Dele = nullptr;
class ATestCharacter* Testor = nullptr;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
DelegateInvoker.cpp:
// Fill out your copyright notice in the Description page of Project Settings.
#include "DelegateInvokor.h"
#include "Delegator.h"
// Sets default values
ADelegateInvokor::ADelegateInvokor()
{
// 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;
}
void ADelegateInvokor::Invoker()
{
if (Dele == nullptr) Dele = NewObject<ADelegator>();
Dele->Binding().Broadcast();
}
// Called when the game starts or when spawned
void ADelegateInvokor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ADelegateInvokor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
效果图如下:
总结:可以对多个函数进行绑定执行