UE4 delegate使用
创建两个类:
ABlueprintToCppCharacter.h/.cpp
DelegateExample.h/.cpp
-
在ABlueprintToCppCharacter.h 中声明带一个参数的Delegate,并在类中定义:
Delegate定义及使用规则请参考官方文档:https://docs.unrealengine.com/zh-CN/Programming/UnrealArchitecture/Delegates/Dynamic/index.html -
在ABlueprintToCppCharacter.cpp的某个函数中触发delegate的broadcast函数,并传递参数:
-
在 DelegateExample.h 中引入 ABlueprintToCppCharacter.h 头文件
-
在DelegateExample.h中定义一个与delegate同参数表函数:
并实现: -
在想要绑定触发delegate的函数中引用:
该示例的结果为:当player进入过DelegateExample中定义的trigger区域后,开始监听PlayerJump委托,一旦player跳跃,就会触发。(如还没有进入过trigger则不会开始监听)