UE4 delegate使用

创建两个类:

ABlueprintToCppCharacter.h/.cpp
DelegateExample.h/.cpp

  1. 在ABlueprintToCppCharacter.h 中声明带一个参数的Delegate,并在类中定义:
    UE4 delegate使用
    Delegate定义及使用规则请参考官方文档:https://docs.unrealengine.com/zh-CN/Programming/UnrealArchitecture/Delegates/Dynamic/index.html

  2. 在ABlueprintToCppCharacter.cpp的某个函数中触发delegate的broadcast函数,并传递参数:
    UE4 delegate使用

  3. 在 DelegateExample.h 中引入 ABlueprintToCppCharacter.h 头文件
    UE4 delegate使用

  4. 在DelegateExample.h中定义一个与delegate同参数表函数:
    UE4 delegate使用
    并实现:
    UE4 delegate使用

  5. 在想要绑定触发delegate的函数中引用:
    UE4 delegate使用

该示例的结果为:当player进入过DelegateExample中定义的trigger区域后,开始监听PlayerJump委托,一旦player跳跃,就会触发。(如还没有进入过trigger则不会开始监听)

参考链接:https://www.youtube.com/watch?v=MwqgmhOL8gs