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

}
相关推荐
一个不知名程序员www4 小时前
算法学习入门 --- 哈希表和unordered_map、unordered_set(C++)
c++·算法
C++ 老炮儿的技术栈5 小时前
在C++ 程序中调用被 C编译器编译后的函数,为什么要加 extern “C”声明?
c语言·c++·windows·git·vscode·visual studio
%xiao Q5 小时前
GESP C++五级-202406
android·开发语言·c++
Sarvartha5 小时前
C++ STL 栈的便捷使用
c++·算法
Aevget6 小时前
MFC扩展库BCGControlBar Pro v37.2 - 全新的VS 2026可视化管理器
c++·mfc·bcg·界面控件·ui开发
C+-C资深大佬6 小时前
C++类型判断
开发语言·c++
Yu_Lijing6 小时前
基于C++的《Head First设计模式》笔记——模式合作
c++·笔记·设计模式
zmzb01036 小时前
C++课后习题训练记录Day74
开发语言·c++
cdut_suye7 小时前
解锁函数的魔力:Python 中的多值传递、灵活参数与无名之美
java·数据库·c++·人工智能·python·机器学习·热榜
txinyu的博客8 小时前
前置声明与 extern
linux·c++