UE中委托的使用很广泛,许多Event的触发都有对应的虚函数和委托,虚函数不用讲,只能在派生类中使用,而委托可以在别的类或者蓝图中使用,就应用范围而言,委托的使用更灵活。以AActor的

	/**
* Event when this actor overlaps another actor, for example a player walking into a trigger.
* For events when objects have a blocking collision, for example a player hitting a wall, see 'Hit' events.
* @note Components on both this and the other Actor must have bGenerateOverlapEvents set to true to generate overlap events.
*/
virtual void NotifyActorBeginOverlap(AActor* OtherActor);
/**
* Event when this actor overlaps another actor, for example a player walking into a trigger.
* For events when objects have a blocking collision, for example a player hitting a wall, see 'Hit' events.
* @note Components on both this and the other Actor must have bGenerateOverlapEvents set to true to generate overlap events.
*/
UFUNCTION(BlueprintImplementableEvent, meta=(DisplayName = "ActorBeginOverlap"), Category="Collision")
void ReceiveActorBeginOverlap(AActor* OtherActor); /**
* Event when an actor no longer overlaps another actor, and they have separated.
* @note Components on both this and the other Actor must have bGenerateOverlapEvents set to true to generate overlap events.
*/
virtual void NotifyActorEndOverlap(AActor* OtherActor);

为例,重叠(overlapped)事件发生时,就会触发这组函数(Begin/EndOverlap),但仅限于在派生类中进行重载。其实,AActor提供了另外一种更加灵活的方式,那就是:委托(Delegate)

	/**
* Called when another actor begins to overlap this actor, for example a player walking into a trigger.
* For events when objects have a blocking collision, for example a player hitting a wall, see 'Hit' events.
* @note Components on both this and the other Actor must have bGenerateOverlapEvents set to true to generate overlap events.
*/
UPROPERTY(BlueprintAssignable, Category="Collision")
FActorBeginOverlapSignature OnActorBeginOverlap;

当然也有对应的End形式。查看FActorBeginOverlapSignature的定义:

// Delegate signatures
DECLARE_DYNAMIC_MULTICAST_DELEGATE_FiveParams( FTakeAnyDamageSignature, AActor*, DamagedActor, float, Damage, const class UDamageType*, DamageType, class AController*, InstigatedBy, AActor*, DamageCauser );
DECLARE_DYNAMIC_MULTICAST_DELEGATE_NineParams( FTakePointDamageSignature, AActor*, DamagedActor, float, Damage, class AController*, InstigatedBy, FVector, HitLocation, class UPrimitiveComponent*, FHitComponent, FName, BoneName, FVector, ShotFromDirection, const class UDamageType*, DamageType, AActor*, DamageCauser );
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams( FActorBeginOverlapSignature, AActor*, OverlappedActor, AActor*, OtherActor );
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams( FActorEndOverlapSignature, AActor*, OverlappedActor, AActor*, OtherActor );
DECLARE_DYNAMIC_MULTICAST_DELEGATE_FourParams( FActorHitSignature, AActor*, SelfActor, AActor*, OtherActor, FVector, NormalImpulse, const FHitResult&, Hit ); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam( FActorBeginCursorOverSignature, AActor*, TouchedActor );
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam( FActorEndCursorOverSignature, AActor*, TouchedActor );
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams( FActorOnClickedSignature, AActor*, TouchedActor , FKey, ButtonPressed );
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams( FActorOnReleasedSignature, AActor*, TouchedActor , FKey, ButtonReleased );
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams( FActorOnInputTouchBeginSignature, ETouchIndex::Type, FingerIndex, AActor*, TouchedActor );
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams( FActorOnInputTouchEndSignature, ETouchIndex::Type, FingerIndex, AActor*, TouchedActor );
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams( FActorBeginTouchOverSignature, ETouchIndex::Type, FingerIndex, AActor*, TouchedActor );
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams( FActorEndTouchOverSignature, ETouchIndex::Type, FingerIndex, AActor*, TouchedActor ); DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FActorDestroyedSignature, AActor*, DestroyedActor );
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FActorEndPlaySignature, AActor*, Actor , EEndPlayReason::Type, EndPlayReason); DECLARE_DELEGATE_SixParams(FMakeNoiseDelegate, AActor*, float /*Loudness*/, class APawn*, const FVector&, float /*MaxRange*/, FName /*Tag*/);

其中DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams,表明这是一个多播。在UE中,委托有单播和多播两种方式,另外还有委托的函数带不带返回值(_RetVal后缀),单播和多播均为无返回值(函数返回类型为void)。

一、单播:

使用如下定义(这里只使用无参和一个参数两种类型,别的类似):

//声明委托(单播),无参数
DECLARE_DELEGATE(MyDelegate)
DECLARE_DELEGATE_OneParam(MyIntDelegate, int32)

这样就声明了两种委托类型,MyDelegate(无参数),MyIntDelegate(带有一个int32参数类型),用法如下:

1)声明和实现回调函数,注意:一定要加UFUNCTION修饰函数声明,因为委托是参与UE的反射系统中的,即UE引擎要知道有这个函数,这在下面的例子中会看到,声明和实现如下:

	UFUNCTION()
void IntFunction(int32 Value)
{
GLog->Log(ELogVerbosity::Warning, "IntFunction: " + FString::FromInt(Value));
} UFUNCTION()
void SecondIntFunction(int32 Value)
{
GLog->Log(ELogVerbosity::Warning, "SecondIntFunction: " + FString::FromInt(Value));
}
UFUNCTION()
void ThirdIntFunction(int32 Value)
{
GLog->Log(ELogVerbosity::Warning, "ThirdIntFunction: " + FString::FromInt(Value));
} UFUNCTION()
void SomeFunction()
{
GLog->Log(ELogVerbosity::Warning, "SomeFunction: ");
}

使用方法:

随便找个地方测试,这里在BeginPlay()中测试:

	MyIntDelegate IntDelegate;
IntDelegate.BindUFunction(this, FName("IntFunction"));
IntDelegate.BindUFunction(this, FName("SecondIntFunction"));

单播,只能触发一个委托,因此上面虽然绑定了两个函数IntFunction和SecondIntFuction,但是只有第2个函数(SecondIntFunction)得到调用

	IntDelegate.Execute(999);

在引擎的日志中显示如下:

注意到一个有趣的地方,我们绑定函数时,不是通过函数的名字来绑定的,而是通过函数的名字对应的FName来绑定的,这是说明(或许称推测更准确),这些函数在UE引擎中通过某种方式(VM更有可能)有函数和其对应名称字符串的一个映射,这也是前面在函数声明处添加UFUNCTION修饰的原因(告诉UHT要把这个函数添加进反射系统)。另外一个有趣的地方是调用委托(IntDelegate.Execute(999))时,VC会自动推导出参数类型(VS2017测试)为int32。

二、多播:多播时可以触发多个委托的函数,多播带有Multicast这样的字符串,这很容易和单播区分出来。多播就意味着可以触发多个事件,如下:

	GLog->Log(ELogVerbosity::Warning, TEXT("将要进行多播……。"));
MyIntMulticastDelegate IntMulticastDelegate;
IntMulticastDelegate.AddUFunction(this, FName("IntFunction"));
IntMulticastDelegate.AddUFunction(this, FName("SecondIntFunction"));
IntMulticastDelegate.AddUFunction(this, FName("ThirdIntFunction"));
IntMulticastDelegate.Broadcast(123);

注意,多播要使用Broadcast触发,Broadcast就意味着广播,广播嘛,大家只要愿意,都能收到,这也正是观察者(Observer)模式。

运行结果:

三、扩展:

既然引入时使用了重叠的例子,这里把这个事件再注解下,老了,记性不好:(

void AActor::NotifyActorBeginOverlap(AActor* OtherActor)
{
// call BP handler
ReceiveActorBeginOverlap(OtherActor);
}

该函数是虚函数,调用一个ReceiveActorBeginOverlap函数,ReceiveActorBeginOverlap原型在引入中有原型,看下说明知道,这里其实就是调用蓝图中对应的OnActorBeginOverlap结点(NODE)。所以重载时要注意,如果需要调用蓝图中的事件(通常如此)要显式调用Super::NotifyActorBeginOverlap(OtherActor)。调用的位置,即先于自定义代码,或者在自定义代码后调用,要根据自己的情况,例如,如果在派生类中做些初始化,以供蓝图使用,则应先调用自己的代码,后调用父类,否则,无所谓。

四,进一步说明重叠事件:

在前方中提及,AActor是不参与物体的collide的,这里又有重载,又有多播,这不是打自己的脸?其实不是。AActor是容器!AActor是容器!AActor是容器!所以功能的实现都是通过组件实现的。在前方中说明碰撞(collide,当然包括重叠和Hit,唔,,Hit叫撞击听着不顺就Hit好了)是在UPrimitiveComponent中实现,这里把它扒出来,晾晾。

// @todo, don't need to pass in Other actor?
void UPrimitiveComponent::BeginComponentOverlap(const FOverlapInfo& OtherOverlap, bool bDoNotifies)
{
// If pending kill, we should not generate any new overlaps
if (IsPendingKill())
{
return;
} // UE_LOG(LogActor, Log, TEXT("BEGIN OVERLAP! Self=%s SelfComp=%s, Other=%s, OtherComp=%s"), *GetNameSafe(this), *GetNameSafe(MyComp), *GetNameSafe(OtherComp->GetOwner()), *GetNameSafe(OtherComp)); UPrimitiveComponent* OtherComp = OtherOverlap.OverlapInfo.Component.Get(); if (OtherComp)
{
bool const bComponentsAlreadyTouching = IsOverlappingComponent(OtherOverlap);
if (!bComponentsAlreadyTouching && CanComponentsGenerateOverlap(this, OtherComp))
{
AActor* const OtherActor = OtherComp->GetOwner();
AActor* const MyActor = GetOwner(); const bool bNotifyActorTouch = bDoNotifies && !MyActor->IsOverlappingActor(OtherActor); // Perform reflexive touch.
OverlappingComponents.Add(OtherOverlap); // already verified uniqueness above
OtherComp->OverlappingComponents.AddUnique(FOverlapInfo(this, INDEX_NONE)); // uniqueness unverified, so addunique if (bDoNotifies)
{
// first execute component delegates
if (!IsPendingKill())
{
OnComponentBeginOverlap.Broadcast(this, OtherActor, OtherComp, OtherOverlap.GetBodyIndex(), OtherOverlap.bFromSweep, OtherOverlap.OverlapInfo);
} if (!OtherComp->IsPendingKill())
{
// Reverse normals for other component. When it's a sweep, we are the one that moved.
OtherComp->OnComponentBeginOverlap.Broadcast(OtherComp, MyActor, this, INDEX_NONE, OtherOverlap.bFromSweep, OtherOverlap.bFromSweep ? FHitResult::GetReversedHit(OtherOverlap.OverlapInfo) : OtherOverlap.OverlapInfo);
} // then execute actor notification if this is a new actor touch
if (bNotifyActorTouch)
{
// First actor virtuals
if (IsActorValidToNotify(MyActor))
{
MyActor->NotifyActorBeginOverlap(OtherActor);
} if (IsActorValidToNotify(OtherActor))
{
OtherActor->NotifyActorBeginOverlap(MyActor);
} // Then level-script delegates
if (IsActorValidToNotify(MyActor))
{
MyActor->OnActorBeginOverlap.Broadcast(MyActor, OtherActor);
} if (IsActorValidToNotify(OtherActor))
{
OtherActor->OnActorBeginOverlap.Broadcast(OtherActor, MyActor);
}
}
}
}
}
}

代码有点长,似乎超过50,想必Epic不会找我麻烦,只看后一段好了。

简单来讲就是,如果Actor有效(Valid)则调用自己的Notify对应的事件,然后再广播!顺便做个好人,也帮对方触发一下事件,也帮对方发个小广告,为什么这么做?唔,,,思考一下,很快就明白,在Sweep方式下,被重叠的物体是通常都是被动的,如果它再主动检查,很耗资源,所以,它就在那里吃瓜好了,等我碰到你再帮你触发事件,再帮你发你要发的消息(小广告),反正如果大伙都不动,肯定不会发生碰撞(没有进行物理模拟情况下,有物理模拟,唔,同样的结论似乎也成立)。

五、进进进进一步说明重叠事件:刚才突发奇想,打开物理模拟后又如何?简单测试,随便弄个球好了,球打开物理模拟,设置对pawn对象overlap,勾选generated overlap event,然后在level blueprint中随便打印个字符串,我选择的是输出:overlap字符串。pawn一碰到球,产生了同样的事件,结论成立。

六、感谢Epic提供的源代码,引用我最喜欢的侯捷老师一句话:源码面前,了无秘密。话说侯老师,你不准备写一本《UE4引擎深入浅出》吗?多少money我都支持,擦,,,,MFC深入浅出我买了二本,第一本大多数人都没见过,浅蓝色封面的,可惜送人了。嘻嘻。

UE4的委托的更多相关文章

  1. UE4 中Struct Emum 类型的定义方式 笔记

    UE4 基础,但是不经常用总是忘记,做个笔记加深记忆: 图方便就随便贴一个项目中的STRUCT和 Enum 的.h 文件 Note:虽然USTRUCT可以定义函数,但是不能加UFUNCTION 标签喔 ...

  2. UE4技术总结——委托

    UE4技术总结--委托 目录 UE4技术总结--委托 一.定义 二.用法 2.1 声明与调用委托 2.1.1 单播委托 2.1.1.a 声明 2.1.1.b 绑定 2.1.1.c 执行委托 2.1.1 ...

  3. 【UE4 C++ 基础知识】<8> Delegate 委托

    概念 定义 UE4中的delegate(委托)常用于解耦不同对象之间的关联:委托的触发者不与监听者有直接关联,两者通过委托对象间接地建立联系. 监听者通过将响应函数绑定到委托上,使得委托触发时立即收到 ...

  4. 【UE4 设计模式】观察者模式 Observer Pattern

    概述 描述 定义对象间的一种一对多依赖关系,使得每当一个对象状态发生改变时,其相关依赖对象皆得到通知并被自动更新.观察者模式又叫做 发布-订阅(Publish/Subscribe)模式 模型-视图(M ...

  5. UE4新手引导之下载和安装虚幻4游戏引擎

    1) 进入虚幻4的官方主页(https://www.unrealengine.com/) 这里你可以获得关于虚幻4的最新资讯,包括版本更新.博客更新.新闻和商城等.自2015年起,该引擎已经提供免费下 ...

  6. UE4新手引导入门教程

    请大家去这个地址下载:file:///D:/UE4%20Doc/虚幻4新手引导入门教程.pdf

  7. C#中的委托解析

    谈及到C#的基本特性,“委托”是不得不去了解和深入分析的一个特性.对于大多数刚入门的程序员谈到“委托”时,都会想到“将方法作为方法的参数进行传递”,很多时候都只是知道简单的定义,主要是因为“委托”在理 ...

  8. C#的委托

    之前本人一直在写一些相对比较基础的C#代码,现在做了一段时间项目了,遇到更麻烦的问题,比如今天要讨论的委托和事件,这个算是C#进阶篇的内容吧.现在自己就把这些天所学习的和自己所理解的和大家分享.有错请 ...

  9. 深刻理解:C#中的委托、事件

    C#中的事件还真是有点绕啊,以前用JavaScript的我,理解起来还真是废了好大劲!刚开始还真有点想不明白为什么这么绕,想想和JS的区别,最后终于恍然大悟! C#中事件绕的根本原因: C#的方法,它 ...

随机推荐

  1. java面试题复习(五)

    41.阐述final.finally.finalize的区别 final:修饰符(关键字)有三种用法:如果一个类被声明为final,意味着它不能再派生出新的子类,即不能被继承,因此它和abstract ...

  2. 如何才能成为一个合格的web前端工程师

    转载原文地址:https://juejin.im/post/5cc1da82f265da036023b628 开篇前端开发是一个非常特殊的行业,它的历史实际上不是很长,但是知识之繁杂,技术迭代速度之快 ...

  3. 如何系统的学习Java

    初学者记住一点,学习Java一定是连续性的且循序渐进的“系统化”学习,首先我给你提供一个优秀Java工程师的学习路线. web前端方面:html.css,Java.jQuery.xml解析.Boots ...

  4. STS 安装SVN插件

    1:STS中 Help->Eclipse MarketPlace 搜索svn点击go安装svn插件,然后重启STS. 2:如果Team中出现SVN说明安装好了一半. 3: 手动安装SVN Con ...

  5. SpringMVC Controller接收参数总结

    本文中Controller的开发环境如下表格所示,Maven对应POM配置如下代码段所示: 系统/工具 版本号OS Windows 7 Home BasicJava 1.7.0_79Eclipse M ...

  6. Packet Tracer路由器简单配置

    路由器Router2:Router>enable 进入特权模式Router#configure terminal 进入全局配置模式Router(config)#hostname RA 将路由器名 ...

  7. 微信小程序 project.config.json 配置

    可以在项目根目录使用 project.config.json 文件对项目进行配置. miniprogramRoot Path String 指定小程序源码的目录(需为相对路径) qcloudRoot ...

  8. C语言典型编程2

    关于C的一些小而精的编程,适合希望提升编程能力的初学者学习:关键编程也就几句,但思维可以迁移到其他编程语言.同一问题,算法多种. //任意整数的任意次方取后3位(算数取位)#include<st ...

  9. Hbase--知识点总结3

    Hbase知识点总结:  hbase表中为什么列族的数量不能太多? 因为当一个列族数据溢写的时候,其他列族也会发生数据溢写,但是其他列族中数据的数量还没有达到溢写的阈值,就会导致产生的小文件数量增多. ...

  10. python数据类型及其详解

    一.数据类型介绍 1.字符串(string) 三种类型:单引号,双引号,三引号 a = 'jam' b = "JamHsiao" c = '''JAMHSIAO''' print( ...