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

}
相关推荐
j_xxx404_26 分钟前
Linux进程信号捕捉与操作系统运行本质深度解析
linux·运维·服务器·开发语言·c++·人工智能·ai
vx-程序开发1 小时前
基于机器学习的动漫可视化系统的设计与实现-计算机毕业设计源码08339
java·c++·spring boot·python·spring·django·php
啊董dong2 小时前
noi-2026年5月12号小测验
数据结构·c++·算法
咩咦3 小时前
C++学习笔记24:构造函数初始化列表
c++·学习笔记·类和对象·构造函数·初始化列表·const引用
计算机安禾3 小时前
【c++面向对象编程】第43篇:可变参数模板(C++11):优雅处理不定长参数
java·开发语言·c++
10岁的博客3 小时前
C++ 进制转换:通用 a 进制转 b 进制(2-36进制)题解
开发语言·c++
小贾要学习4 小时前
【Linux】基于自定义TCP协议的日期计算器
linux·网络·c++·网络协议·tcp/ip
YsyaaabB4 小时前
ACM 模式通用代码模板
java·c++·python·算法
我命由我123454 小时前
C++ - 面向对象 - 析构函数
android·c语言·开发语言·c++·visualstudio·visual studio·android runtime
代码村新手5 小时前
C++-多态
开发语言·c++