UE5 C++ 动态单播放

一.声明

这个是非动态的声明方式

动态单播,需要把参数的名字也声明上,不仅仅只写参数类型。

cpp 复制代码
//动态单播 不支持载荷 函数签名到蓝图已经写好无法动态修改
DECLARE_DYNAMIC_DELEGATE_TwoParams(FDynamicTwo, FString, InName, int32, InMoney); //第一个也是代理的名字F开头

并且不支持载荷这种操作。

二.动态单播无返回值实例演示

1.声明需要带 分号 ";"

cpp 复制代码
DECLARE_DYNAMIC_DELEGATE_RetVal_TwoParams(int32 ,FWXDynamicRetTwoParam, FString, inName, int32, inNum);

2.在类里面声明这个类型

复制代码
	FWXDynamicTwoParam TestDyEvent;
cpp 复制代码
	ADynamicCast();
	UFUNCTION(BlueprintCallable)
	void InitXGDynamic(FWXDynamicTwoParam InDelegate);
	UFUNCTION(BlueprintCallable)
	void RealseXGDynamic();
	UFUNCTION(BlueprintCallable)
	void CallXGDynamic(FString InName,int32 InNum);

三个函数,暴露给蓝图。分别赋值给类,并执行 发报

3.绑定的方式

1.C++ 里绑定,使用BindFuntion()

强调一下,绑定的函数一点要参数一致,且加入反射系统(有UFUNCTION 宏)

委托变量,F开头的。可加可不加(有UPROPERTY宏),有时UE抽风就加,说不定就绑上了

2.蓝图里绑定就是用蓝图连上 CustomEvent

并在Init 里传入赋值。这个是在继承的蓝图类里绑定

4.最后都能 广播成功

cpp 复制代码
void ADynamicCast::CallXGDynamic(FString InName, int32 InNum)
{
	//UE_LOG(LogTemp, Warning, TEXT("%d"), InNum);
	if (TestDyEvent.IsBound())
	{
		bool ifHas = TestDyEvent.ExecuteIfBound(InName, InNum);
		int32 a = 0;
	}
	
}

关卡蓝图里测试即可

三.动态单播有返回值实例演示

1.声明和上面类似,只是加了Ret 字段

cpp 复制代码
DECLARE_DYNAMIC_DELEGATE_RetVal_TwoParams(int32 ,FWXDynamicRetTwoParam, FString, inName, int32, inNum);
cpp 复制代码
	FWXDynamicRetTwoParam TestEvent2;

2.实现

cpp 复制代码
	UFUNCTION(BlueprintCallable)
	void InitXGRetDynamic(FWXDynamicRetTwoParam InDelegate);
	UFUNCTION(BlueprintCallable)
	void RealseXGRetDynamic();
	UFUNCTION(BlueprintCallable)
	void CallXGRetDynamic(FString InName,int32 InNum);
cpp 复制代码
void ADynamicCast::InitXGRetDynamic(FWXDynamicRetTwoParam InDelegate)
{
	TestEvent2 = InDelegate;
}

void ADynamicCast::RealseXGRetDynamic()
{
	TestEvent2.Clear();
}

void ADynamicCast::CallXGRetDynamic(FString InName, int32 InNum)
{
	int32 retValue = TestEvent2.Execute(InName, InNum);
}

3.在蓝图里就要多一些步骤,不是直接CustormEvent连上不行,因为默认是没有返回值的。要用CreateEvent

并重写函数

4.在执行时可以有返回值,但这个在蓝图里看不到.

cpp 复制代码
void ADynamicCast::CallXGRetDynamic(FString InName, int32 InNum)
{
	int32 retValue = TestEvent2.Execute(InName, InNum);
}

5.测试

四.完整代码如下

cpp 复制代码
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "DynamicCast.generated.h"
DECLARE_DYNAMIC_DELEGATE_TwoParams(FWXDynamicTwoParam,FString,inName,int32,inNum);
DECLARE_DYNAMIC_DELEGATE_RetVal_TwoParams(int32 ,FWXDynamicRetTwoParam, FString, inName, int32, inNum);

UCLASS()
class THIRDLEARN_API ADynamicCast : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ADynamicCast();
	UFUNCTION(BlueprintCallable)
	void InitXGDynamic(FWXDynamicTwoParam InDelegate);
	UFUNCTION(BlueprintCallable)
	void RealseXGDynamic();
	UFUNCTION(BlueprintCallable)
	void CallXGDynamic(FString InName,int32 InNum);
	UFUNCTION(BlueprintCallable)
	void GWXBindFunciotn();
	UFUNCTION(BlueprintCallable)
	void GWXPrint(FString InName, int32 InNum);
	


	UFUNCTION(BlueprintCallable)
	void InitXGRetDynamic(FWXDynamicRetTwoParam InDelegate);
	UFUNCTION(BlueprintCallable)
	void RealseXGRetDynamic();
	UFUNCTION(BlueprintCallable)
	void CallXGRetDynamic(FString InName,int32 InNum);

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

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


#include "DynamicCast.h"

// Sets default values
ADynamicCast::ADynamicCast()
{
 	// 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 ADynamicCast::InitXGDynamic(FWXDynamicTwoParam InDelegate)
{
	TestDyEvent = InDelegate;
}

void ADynamicCast::RealseXGDynamic()
{
	TestDyEvent.Clear();   //解绑
}

void ADynamicCast::CallXGDynamic(FString InName, int32 InNum)
{
	//UE_LOG(LogTemp, Warning, TEXT("%d"), InNum);
	if (TestDyEvent.IsBound())
	{
		bool ifHas = TestDyEvent.ExecuteIfBound(InName, InNum);
		int32 a = 0;
	}
	
}

void ADynamicCast::GWXBindFunciotn()
{
	//TestDyEvent.BindUFunction(this,FName(GWXPrint));
	TestDyEvent.BindUFunction(this,FName("GWXPrint"));
}

void ADynamicCast::GWXPrint(FString InName, int32 InNum)
{
	UE_LOG(LogTemp, Warning, TEXT("%s %d"), *InName,InNum);
}

void ADynamicCast::InitXGRetDynamic(FWXDynamicRetTwoParam InDelegate)
{
	TestEvent2 = InDelegate;
}

void ADynamicCast::RealseXGRetDynamic()
{
	TestEvent2.Clear();
}

void ADynamicCast::CallXGRetDynamic(FString InName, int32 InNum)
{
	int32 retValue = TestEvent2.Execute(InName, InNum);
}



// Called when the game starts or when spawned
void ADynamicCast::BeginPlay()
{
	Super::BeginPlay();
	//TestDyEvent.BindUFunction();
}

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

}
相关推荐
日月云棠1 天前
UE5源码分析之Editor——ConfigEditor模块全面分析
ue5
码匠许师傅1 天前
【设计模式精讲】14.外观模式(Facade)
c++·设计模式·uml·外观模式
日月云棠1 天前
UE5源码分析之Editor——CommonMenuExtensions插件全面分析
ue5
xxwxx__1 天前
C++ list 深度解析:从使用原理到模拟实现
开发语言·c++·list
Chester_19991 天前
CSP202303C.LDAP
开发语言·c++·蓝桥杯·stl
日月云棠1 天前
UE5源码分析之Editor——ClothingSystemEditorInterface模块全面分析
ue5
j7~1 天前
【C++】《unordered系列关联式容器以及哈希底层、哈希冲突、闭散列与开散列完整解析》
c++·哈希算法·开散列·闭散列·unordered_map·unordered_set·哈希底层结构
日月云棠1 天前
UE5源码分析之Editor——ClothPainter插件全面分析
ue5
2402_882893861 天前
封装红黑树实现map和set —— 从源码到模拟实现
c++·set·map
神仙别闹1 天前
基于C++ MFC 实现的智慧公交系统
开发语言·c++·mfc