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);

}
相关推荐
浆果02077 小时前
NanoTrack C++ — RK3588 实时目标跟踪
c++·目标跟踪·rk3588
ysa0510308 小时前
【并查集】判环
c++·笔记·算法
持力行8 小时前
C/C++ 中的 char*:它标识数组吗?为什么能用下标访问?
c语言·c++
汉克老师10 小时前
GESP2026年6月认证C++六级( 第三部分编程题(2、满二叉树))精讲
c++·深度优先·树形dp·满二叉树·gesp六级·树形dfs
踮起脚看烟花10 小时前
多人聊天室实现v2.0
c++·信息与通信
梦帮科技10 小时前
UE5 GAS 实战:用 Gameplay Ability System 搭建「赛博修真」境界与技能体系
c++·人工智能·python·ue5·c#
旖-旎10 小时前
QT系统篇(5)(下)
开发语言·c++·qt
99乘法口诀万物皆可变11 小时前
PcanToVectorXL_V01:打通 Vector 与 PCAN 的双向 CAN/CAN‑FD 桥梁
c++·学习
liulun11 小时前
C++ WinRT中的事件
开发语言·c++
whitelbwwww11 小时前
c++运行onnx模型
开发语言·c++