UE5 C++ 多播绑定执行演示

一.定义 与 思路

一对对的响应,这里就创建一个发送多播的Actor,和多个响应多播的Actor。这里我学到一个牛逼的,只要你遵循反射规则写好宏,哪怕你在一个cpp里面也可以创建,两个类。并且引擎里会显示出来。AXGMultiDelegateActor作为发播类, AXGMultiExecuteActor 作为执行函数响应类。

二.代码

AXGMultiDelegateActor发播类

1.首先声明多播,多播无返回值。多了FString的载荷

cpp 复制代码
DECLARE_MULTICAST_DELEGATE_OneParam(FXGMultiDelegate, FString);  //多播不存在返回值

2.让后在发报的类里面,添加多播。CallSimpleMuti里面就会进行发报Broadcast。

因为声明里有FString的参数所以,在发报的时候添加进去。Broadcast(TEXT("This is simple Multi"));

cpp 复制代码
UCLASS()
class GWXPJ_API AXGMultiDelegateActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AXGMultiDelegateActor();
	UFUNCTION(BlueprintCallable)
	void CallSimpleMulti();
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	FXGMultiDelegate multidele;
};
cpp 复制代码
// Sets default values
AXGMultiDelegateActor::AXGMultiDelegateActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("LocationRoot"));
	
}

void AXGMultiDelegateActor::CallSimpleMulti()
{
	//multidele.IsBound();
	//multidele.IsBoundToObject();
	multidele.Broadcast(TEXT("This is simple Multi"));
}

AXGMultiExecuteActor 执行函数响应类。

1.Work作为响应函数,参数和 声明的一致

cpp 复制代码
UCLASS()
class GWXPJ_API AXGMultiExecuteActor : public AActor
{
	GENERATED_BODY()
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
//	virtual void EndPlay() override;
public:
	void Work(FString InString,int32 InHealth);  //载荷对应的绑定函数也要多加
	AXGMultiExecuteActor();
public:
	FGuid MyActorID;
};

2.BeginPlay里找到场景里的发报类,并且用AddUObject 进行绑定,这里是只放一个发报的。实现响应函数

响应函数就是,自己的ID号,和血量。

cpp 复制代码
//执行函数

void AXGMultiExecuteActor::BeginPlay()
{
	
	AActor* ActorPtr = UGameplayStatics::GetActorOfClass(this, AXGMultiDelegateActor::StaticClass());  //你需要先拿到,那个类。才能绑定到它那个广播
	if (AXGMultiDelegateActor* MultiDelePtr = Cast<AXGMultiDelegateActor>(ActorPtr))  //LocationActor会向单播的对象报道,我已经绑定好函数准备(注册),响应你的 呼叫
	{
		int32 Health = 100;
		MultiDelePtr->multidele.AddUObject(this, &AXGMultiExecuteActor::Work, Health);
	}
}

//void AXGMultiExecuteActor::EndPlay()
//{
//
//}

void AXGMultiExecuteActor::Work(FString InString, int32 InHealth)
{
	UE_LOG(LogTemp, Warning, TEXT("ID:[%s],InStr:[%s],Health:[%d]"), *MyActorID.ToString(),*InString,InHealth);
}

AXGMultiExecuteActor::AXGMultiExecuteActor()
{
	PrimaryActorTick.bCanEverTick = false;
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("LocationRoot"));
	MyActorID = FGuid::NewGuid();
}

测试结果,用法和单播类似。只是API变了,能有多个同时响应。这里就是三个

三.头文件和源文件

cpp 复制代码
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "XGMultiDelegateActor.generated.h"

DECLARE_MULTICAST_DELEGATE_OneParam(FXGMultiDelegate, FString);  //多播不存在返回值

UCLASS()
class GWXPJ_API AXGMultiExecuteActor : public AActor
{
	GENERATED_BODY()
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
//	virtual void EndPlay() override;
public:
	void Work(FString InString,int32 InHealth);  //载荷对应的绑定函数也要多加
	AXGMultiExecuteActor();
public:
	FGuid MyActorID;
};

UCLASS()
class GWXPJ_API AXGMultiDelegateActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AXGMultiDelegateActor();
	UFUNCTION(BlueprintCallable)
	void CallSimpleMulti();
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	FXGMultiDelegate multidele;
};
cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.


#include "XGMultiDelegateActor.h"
#include "Kismet/GameplayStatics.h"

// Sets default values
AXGMultiDelegateActor::AXGMultiDelegateActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("LocationRoot"));
	
}

void AXGMultiDelegateActor::CallSimpleMulti()
{
	//multidele.IsBound();
	//multidele.IsBoundToObject();
	multidele.Broadcast(TEXT("This is simple Multi"));
}

// Called when the game starts or when spawned
void AXGMultiDelegateActor::BeginPlay()
{
	Super::BeginPlay();
}

// Called every frame
void AXGMultiDelegateActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

//执行函数

void AXGMultiExecuteActor::BeginPlay()
{
	
	AActor* ActorPtr = UGameplayStatics::GetActorOfClass(this, AXGMultiDelegateActor::StaticClass());  //你需要先拿到,那个类。才能绑定到它那个广播
	if (AXGMultiDelegateActor* MultiDelePtr = Cast<AXGMultiDelegateActor>(ActorPtr))  //LocationActor会向单播的对象报道,我已经绑定好函数准备(注册),响应你的 呼叫
	{
		int32 Health = 100;
		MultiDelePtr->multidele.AddUObject(this, &AXGMultiExecuteActor::Work, Health);
	}
}

//void AXGMultiExecuteActor::EndPlay()
//{
//
//}

void AXGMultiExecuteActor::Work(FString InString, int32 InHealth)
{
	UE_LOG(LogTemp, Warning, TEXT("ID:[%s],InStr:[%s],Health:[%d]"), *MyActorID.ToString(),*InString,InHealth);
}

AXGMultiExecuteActor::AXGMultiExecuteActor()
{
	PrimaryActorTick.bCanEverTick = false;
	RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("LocationRoot"));
	MyActorID = FGuid::NewGuid();
}
相关推荐
Once_day1 天前
C++之《程序员自我修养》读书总结(1)
c语言·开发语言·c++·程序员自我修养
Trouvaille ~1 天前
【Linux】TCP Socket编程实战(一):API详解与单连接Echo Server
linux·运维·服务器·网络·c++·tcp/ip·socket
坚果派·白晓明1 天前
在鸿蒙设备上快速验证由lycium工具快速交叉编译的C/C++三方库
c语言·c++·harmonyos·鸿蒙·编程语言·openharmony·三方库
小镇敲码人1 天前
深入剖析华为CANN框架下的Ops-CV仓库:从入门到实战指南
c++·python·华为·cann
张张努力变强1 天前
C++ STL string 类:常用接口 + auto + 范围 for全攻略,字符串操作效率拉满
开发语言·数据结构·c++·算法·stl
小镇敲码人1 天前
探索CANN框架中TBE仓库:张量加速引擎的优化之道
c++·华为·acl·cann·ops-nn
平安的平安1 天前
面向大模型算子开发的高效编程范式PyPTO深度解析
c++·mfc
June`1 天前
muduo项目排查错误+测试
linux·c++·github·muduo网络库
C++ 老炮儿的技术栈1 天前
VS2015 + Qt 实现图形化Hello World(详细步骤)
c语言·开发语言·c++·windows·qt
Once_day1 天前
C++之《Effective C++》读书总结(4)
c语言·c++·effective c++