U++学习笔记 ------ 多播委托

news2024/9/21 18:14:56

多播委托

1、可以绑定多个回调函数,所有绑定的回调函数都会执行,实质是维持了一个单播委托的数组没有返回值支持参数不支持反射以及序列化

绑定多播委托
Add:将函数委托添加 到该多播委托的调用列表中。
AddStatic:添加原始C++指针全局函数委托
AddRaw:添加原始C++指针委托。原始指针不使用任何类型的引用,因此如果从委托下面删除了对象,则调用此函数可能不安全。调用Execute()时请小心!
AddSP:添加基于共享指针的(快速、非线程安全)成员函数委托。共享指针委托保留对对象的弱引用。
AddUObject:添加基于UObject的成员函数委托。UObject委托保留对对象的弱引用。
Remove:从该多播委托的调用列表中删除函数(性能为O(N))。请注意,委托的顺序可能不会被保留!
RemoveAll:从该多播委托的调用列表中删除绑定到指定UserObject的所有函数。请注意,委托的顺序可能不会被保留!

TestCharacter.h:

// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "TestCharacter.generated.h"

UCLASS(config=Game)
class ATestCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	ATestCharacter();

	virtual void EventBegin();

	void output();

	void output1();


};

TestCharacter.cpp:

// Copyright Epic Games, Inc. All Rights Reserved.

#include "TestCharacter.h"
#include "HeadMountedDisplayFunctionLibrary.h"
#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"
#include "Components/InputComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/Controller.h"
#include "GameFramework/SpringArmComponent.h"

//
// ATestCharacter

ATestCharacter::ATestCharacter()
{
	// Set size for collision capsule
	GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);

	// set our turn rates for input
	BaseTurnRate = 45.f;
	BaseLookUpRate = 45.f;

	// Don't rotate when the controller rotates. Let that just affect the camera.
	bUseControllerRotationPitch = false;
	bUseControllerRotationYaw = false;
	bUseControllerRotationRoll = false;

	// Configure character movement
	GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...	
	GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
	GetCharacterMovement()->JumpZVelocity = 600.f;
	GetCharacterMovement()->AirControl = 0.2f;

	// Create a camera boom (pulls in towards the player if there is a collision)
	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
	CameraBoom->SetupAttachment(RootComponent);
	CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character	
	CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller

	// Create a follow camera
	FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
	FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm

	// Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character) 
	// are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
}


void ATestCharacter::output() {
	GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Green, TEXT("Sccessful"));
}

void ATestCharacter::output1()
{
	GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Green, TEXT("Sccessful Again"));
}

Delegator.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "TestCharacter.h"
#include "Delegator.generated.h"

DECLARE_MULTICAST_DELEGATE(DeleLoader);

UCLASS()
class TEST_API ADelegator : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ADelegator();

	DeleLoader Binding();

private:
	DeleLoader Dele;

	class ATestCharacter* Testor = nullptr;

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

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

Delegator.cpp:

// Fill out your copyright notice in the Description page of Project Settings.


#include "Delegator.h"
#include "TestCharacter.h"

// Sets default values
ADelegator::ADelegator()
{
 	// 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;

}

DeleLoader ADelegator::Binding()
{
	if (Testor == nullptr)	Testor = NewObject<ATestCharacter>();
	Dele.AddUObject(Testor, &ATestCharacter::output);
	Dele.AddUObject(Testor, &ATestCharacter::output1);

	return Dele;
}

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

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

}

.DelegateInvoker.h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Delegator.h"
#include "DelegateInvokor.generated.h"



UCLASS()
class TEST_API ADelegateInvokor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ADelegateInvokor();

	UFUNCTION(BlueprintCallable)
	void Invoker();

private:
	ADelegator* Dele = nullptr;

	class ATestCharacter* Testor = nullptr;

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

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};

DelegateInvoker.cpp:

// Fill out your copyright notice in the Description page of Project Settings.


#include "DelegateInvokor.h"
#include "Delegator.h"

// Sets default values
ADelegateInvokor::ADelegateInvokor()
{
 	// 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 ADelegateInvokor::Invoker()
{
	if (Dele == nullptr)	Dele = NewObject<ADelegator>();
	Dele->Binding().Broadcast();
}

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

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

}

效果图如下:

总结:可以对多个函数进行绑定执行

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/12994.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

类和对象(下)

关于类和对象依旧有许多难点&#xff0c;这篇博客将会讲解关于类的构造函数的初始化列表&#xff0c;静态成员&#xff0c;友元&#xff0c;内部类&#xff0c;以及匿名对象等一些比较复杂的东西。 初始化列表 我们之前就已经学过类和对象的构造函数&#xff0c;但是实际上那并…

[小技巧]C# 反射

文章目录定义Namespace场景示例简单反射一个对象进行操作反射一个有参构造函数的对象反射一个泛型类型的对象简单反射一个方法进行操作反射一个泛型方法进行操作反射一个静态&#xff08;Static&#xff09;方法定义 反射提供描述程序集、模块和类型的对象&#xff08;Type 类…

设计模式----工厂模式

设计模式----工厂模式 文章目录设计模式----工厂模式一.简介1. 什么是工厂模式&#xff1f;2. 工厂模式的类型&#xff1f;3. 工厂模式的使用场景&#xff1f;二. 使用1. 简单工厂模式2. 工厂方法模式3. 抽象工厂模式一.简介 1. 什么是工厂模式&#xff1f; 工厂模式&#xff…

【SpringBoot项目】SpringBoot项目-瑞吉外卖【day03】分类管理

文章目录前言公共字段自动填充问题分析代码实现功能测试功能完善新增分类需求分析模型代码开发功能测试分类信息分页查询需求分析代码开发功能测试删除分类需求分析代码开发功能完善修改分类需求分析代码实现结尾&#x1f315;博客x主页&#xff1a;己不由心王道长&#x1f315…

11.17 - 每日一题 - 408

每日一句&#xff1a; 世上没有侥幸的成功&#xff0c;只有加倍的努力。 数据结构 1 一棵左右子树均不空的二叉树在先序线索化后&#xff0c;其中空的链域的个数是______ A. 0B. 1C 2D.不确定答案&#xff1a;B 解析&#xff1a;线索二叉树利用了二叉链表中的空的左右孩子指…

高通导航器软件开发包使用指南(3)

高通导航器软件开发包使用指南&#xff08;3&#xff09;3.2 实时数据查看3.3 日志分析3.4 其他日志记录系统信息3.4.1查看数据记录选项3.4.2确保日志存储3.4.3获取snav_vector版本3.2 实时数据查看 snav_sinspector控制台应用程序允许以人工方式查看日志文件中的二进制数据 …

java项目-第142期ssm美食推荐系统-ssm毕业设计_计算机毕业设计

java项目-第142期ssm美食推荐系统-ssm毕业设计_计算机毕业设计 【源码请到资源专栏下载】 今天分享的项目是《ssm美食推荐系统》 该项目分为2个角色&#xff0c;管理员和用户。 用户可以浏览前台,包含功能有&#xff1a; 首页、热门美食、美食教程、美食店铺 、美食社区、美食资…

Arthas教程

Linux环境安装 下载地址&#xff1a;https://alibaba.github.io/arthas/arthas-boot.jar java -jar arthas-boot.jar 运行 quit 退出 stop 停止Arthas快速入门 一.执行一个jar包 二.通过arthas来attach&#xff08;黏附&#xff09; 三.常用命令操作 诊断demo下载http…

Oracle Primavera Unifier活动管理器(Activity Manager)

目录 一、简要介绍 二、其他相关 一、简要介绍 Oracle Primavera Unifier Activity“活动”被定义为必须按计划完成的工作或事件的一部分。 Activity也就是以上的活动&#xff0c;它从映射的 P6 项目中捕获计划数据&#xff0c;从公司级主费率表&#xff08;默认&#xff0…

大数据必学Java基础(一百零二):连接池的使用

文章目录 连接池的使用 一、连接池基础知识扩展 二、代码实战 1、定义连接池

信道划分介质访问控制ALOHA协议CSMA协议CSMA/CD协议轮询访问MAC协议

注&#xff1a;最后有面试挑战&#xff0c;看看自己掌握了吗 文章目录传输数据两种链路点对点链路广播式链路介质访问控制静态划分信道动态划分信道轮询访问介质访问控制随机访问介质访问控制---所有用户都可以随机发送信息ALOHA协议------想说就说CSMA协议------先听再说1-坚持…

【保姆级】新机器部署RabbitMQ

1、登录服务器&#xff0c;如果非root用户则切root用户 sudo su - 2、在/usr/tmp目录上传erlang、rabbitmq安装包 3、将安装包移到/usr/local/目录 mv /usr/tmp/erlang-21.3.8.2-1.el7.x86_64.rpm /usr/local/ mv /usr/tmp/rabbitmq-server-3.7.15-1.el7.noarch.rpm /usr/lo…

基础知识:临界阻尼

任何一个振动系统&#xff0c;当阻尼增加到一定程度时&#xff0c;物体的运动是非周期性的&#xff0c;物体振动连一次都不能完成&#xff0c;只是慢慢地回到平衡位置就停止了。当阻力使振动物体刚好能不作周期性振动而又能最快地回到平衡位置的情况&#xff0c;称为“临界阻尼…

JS高级(三):严格模式、闭包、递归、深拷贝和浅拷贝

JavaScript高级&#xff08;三&#xff09;一、严格模式1.开启严格模式&#xff08;1&#xff09;为脚本开启严格模式&#xff08;2&#xff09;为某个函数开启严格模式2.严格模式的一些规定&#xff08;1&#xff09;禁止变量未声明就赋值&#xff08;2&#xff09;禁止删除已…

AMD发布22.11.1驱动,支持《使命召唤:战区2.0》

他来了他来了&#xff0c;带着迷人的脚步走来了&#xff01; 《使命召唤&#xff1a;战区2.0》正式上线了。有Steam周榜三连冠的《使命召唤19》在前&#xff0c;《战区2.0》可以说是备受瞩目&#xff0c;免费大逃杀&#xff0c;谁不期待&#xff1f; &#xff08;图源自steam&…

一句话生成图片,FlagAI使用(附页面操作代码) | 机器学习

目录 前言 项目结构 页面交互调整 总结 前言 最近Text-To-Image是一个很火的话题&#xff0c;甚至更进一步的Text-To-Video话题度也在不断上升。最近看到一个开源项目FlagAI&#xff0c;是目前我觉着效果比较好的项目之一。安装操作简单&#xff0c;支持中英文&#xff0c;…

疫情防控管理系统

1、项目介绍 疫情防控管理系统拥有两种角色&#xff1a;管理员和用户 管理员&#xff1a;医护信息管理、物资管理、疫苗管理、疫站管理等 用户&#xff1a;登录注册、物资、疫苗、疫站查看 2、项目技术 后端框架&#xff1a; Servlet、mvc模式 前端技术&#xff1a;Bootst…

yolov5剪枝实战1: 论文及yolov5剪枝实战项目介绍

本系列博客介绍yolov5剪枝方法 1. 介绍 神经网络一般都存在过参数化(over-parameterized)的问题,存在冗余的神经元或权重,所以可以进行剪枝。 其实对网络可以针对不同的颗粒度进行剪枝,可以进行权重、神经元级别的剪枝,也可以基于channel, shape,filter以及layer级别的剪枝…

卷积神经网络基础

由于篇幅所限&#xff0c;本章将重点介绍计算机视觉的经典模型&#xff08;卷积神经网络&#xff09;和两个典型任务&#xff08;图像分类和目标检测&#xff09;。主要涵盖如下内容&#xff1a; 卷积神经网络&#xff1a;卷积神经网络&#xff08;Convolutional Neural Netwo…

Nginx知识汇总

一、Nginx的简介 nginx是一款自由的、开源的、高性能的HTTP服务器和反向代理服务器&#xff1b;同时也是一个IMAP、POP3、SMTP代理服务器&#xff1b;nginx可以作为一个HTTP服务器进行网站的发布处理&#xff0c;另外nginx可以作为反向代理进行负载均衡的实现。 二、Nginx的优…