UE4_20 Scripting with C++ 04

感觉国漫的打斗、美术其实都是挺棒的。元素:搞笑、打斗/动作用于吸引观众。 另外很重要的就是配音的质量,结合音乐、美术、背景故事提供代入感。用心做动漫,国漫很强的其实。另外在人设上不能太蠢。

单词本:
manner:方式,方法
Unreal uses events to notify objects about things that happen in the game world in an efficient manner.
coupling:耦合 
arbitrary:任意的
Events and delegates are useful to ensure that these notifications can be issued in a way that minimizes class coupling, and allows arbitrary classes to subscribe to be notified.
signature:签名
This recipe shows you how to change the signature of the delegate so that it accepts some input.
potentially:潜在的
Multicast delegates are a collection of function pointers, each potentially on different objects, all of which will be invoked when the delegate is broadcast.

Chapter05:Handling Events and Delegates,本章节和第一版内容大致相同

VS的番茄插件,有钱还是要支持下正版的。从官网上先下载个体验版。VS更新到了2019版本,但2019版本目前并不支持UE4_20的编译。

1、简单的通过虚函数实现事件处理
代码很简单:创建一个Actor并且为它添加一个UBoxComponent,并且重写NotifyActorBeginOverlap和NotifyActorEndOverlap,
关键在于分析:实际上是UBoxComponent组件与OtherAtor发生碰撞<这也是一种设计模式,关于设计模式,要走的路还好长>,
会通知Actor自己产生了碰撞,即 NotifyActorBeginOverlap,碰撞结束后会通知NotifyActorEndOverlap

2、把委托绑定到一个UFUNCTION上

委托是一种函数指针,但相对来说要安全一些,它提供了运行时绑定的机制。(看到运行时绑定,就要想到面向对象思想),

这一节通过委托函数调用,结合触发器和PointLight来实现开关灯的效果,来实现怎么做?步骤:
GameMode中:
声明委托:DECLARE_DELEGATE(FStandardDelegateSignature) 
定义委托变量:FStandardDelegateSignature MyStandardDelegate
创建一个Actor,名为DelegateListener其中:加入一个UPointLightComponent
实现委托的函数:EnableLight
绑定委托:MyStandardDelegate.BindUObject(this,&ADelegateListener::EnableLight);
MyTriggerVolume中:利用Overlap事件调用委托函数
委托的调用:MyStandardDelegate.ExecuteIfBound();

由于MyStandardDelegate是声明在GameMode中的委托变量,需要在Actor中先获取到GameMode

    //绑定实现   
 UWorld* TheWorld = GetWorld();
    if (TheWorld != nullptr)
    {
        AGameModeBase* GameMode = 
                               UGameplayStatics::GetGameMode(TheWorld); 
        AChapter_05GameModeBase * MyGameMode = 
                               Cast<AChapter_05GameModeBase>(GameMode);
        if (MyGameMode != nullptr) {
	MyGameMode->MyStandardDelegate.BindUObject(this,
				&ADelegateListener::EnableLight);
		}

    }

3、委托解绑定

对于绑定,尽量的使用BindUObject方法,当物体销毁时,必要的调用解绑定Unbind()
具体方法是重写物体的EndPlay方法,在里面调用解绑定。

4、委托绑定传参

与流程2的普通委托的构建大致相同,唯一不同的是绑定的函数上需要传入适当的参数。
仍然是在GameMode中
声明:DECLARE_DELEGATE_OneParam(FParamDelegateSignature, FLinearColor)
定义:FParamDelegateSignature MyParameterDelegate;
定义一个新的Actor,起名ParamDelegateListener
绑定:MyGameMode->MyParameterDelegate.BindUObject(this,
                &AParamDelegateListener::SetLightColor);
带参函数:void SetLightColor(FLinearColor LightColor);
在MyTriggerVolume中的Overlap事件中
执行带参委托: MyGameMode->MyParameterDelegate.ExecuteIfBound(FLinearColor(1, 0, 0, 1));
注意在EndPlay中解绑定       

书中还提到一种在绑定的时候传入载荷的办法:相当于用DECLARE_DELEGATE_OneParam实现两个参数的传入
修改所绑定的函数:void SetLightColor(FLinearColor LightColor,bool Visiblity)
并且UBindObject的参数中传入新添加的变量
MyGameMode->MyParameterDelegate.BindUObject(this, &AParamDelegateListener::SetLightColor,true);

5、多播委托,作用是在委托调用时,所有绑定的委托函数都会执行

GameMode中
声明:DECLARE_MULTICAST_DELEGATE(FMulticastDelegateSignature)
定义:FMulticastDelegateSignature MyMulticastDelegate;
创建一个MulticastDelegateListener
需要定义一个FDelegateHandle MyDelegateHandle用来存储多播委托
绑定:MyDelegateHandle = MyGameMode -> MyMulticastDelegate.AddUObject(this,
                &AMulticastDelegateListener::ToggleLight);
与普通委托不同的是,这里调用AddUObject方法而不是BindUObject方法,原因是多播委托是函数指针的集合
解绑定: MyGameMode->MyMulticastDelegate.Remove(MyDelegateHandle);
在Mytrigger的Overlap事件中:
把委托调用广播出去 MyGameMode->MyMulticastDelegate.Broadcast(); 
6、自定义事件

A中包含B,B为事件源,A来监听它。

使用MyTriggerVolume作为事件源,
在事件源中声明事件:DECLARE_EVENT(AMyTriggerVolume, FPlayerEntered)
定义事件:FPlayerEntered OnPlayerEntered;
在Overlap事件中广播出去:OnPlayerEntered.Broadcast();

定义一个TriggerVolumeListener
包含事件源:AMyTriggerVolume* TriggerEventSource;//使用EditAnywhere在场景中手动指定
定义接收事件时的调用函数:  void OnTriggerEvent();
绑定自定义事件(与多播相同):TriggerEventSource->OnPlayerEntered.AddUObject(this,            
        &ATriggerVolEventListener::OnTriggerEvent);

本章运行效果如图:

下面的两个实例和上一版书中一样,不赘述

先考虑想实现的功能,然后回馈一下UE4所提供的功能,他们有什么作用呢?能否实现我想要的功能呢?

这样子一一对相应起来再回过头来看看UE4的这些功能用到了什么?怎么用?这样子一步一步来。

猜你喜欢

转载自blog.csdn.net/weixin_33232568/article/details/89229473