• UE4 C++:UFUNCTION宏、函数说明符、元数据说明符


    目录

    UFUNCTION声明的作用

    函数说明符(常见):

    BlueprintCallable 蓝图可调用

    BlueprintPure 蓝图纯函数(无执行线,必须要有返回值)

    BlueprintImplementableEvent 蓝图可实现事件(覆盖C++)

    BlueprintNativeEvent 蓝图原生事件(C++必须实现,蓝图也可以实现)

    Exec 控制台可调用函数

    meta说明符

    ExpandEnumAsExecs:多执行流(多返回值)


    UFUNCTION声明的作用

    • UFunction时UE反射系统可识别的C++函数。UObject或蓝图函数库可将成员函数声明为UFunction,方法是将 UFUNCTION 宏放在头文件中函数声明上方的行中。宏将支持 函数说明符 更改UE4解译和使用函数的方式。
    • 可利用函数说明符将UFunction对蓝图可视化脚本图表公开,以便开发者从蓝图资源调用或扩展UFunction,而无需更改C++代码。
    • 在类的默认属性中,UFunction可绑定到委托,从而能够执行一些操作(例如将操作与用户输入相关联)
    • 它们还可以充当网络回调,这意味着当某个变量受网络更新影响时,用户可以将其用于接收通知并运行自定义代码。用户甚至可创建自己的控制台命令(通常也称 debug、configuration 或 cheat code 命令),并能在开发版本中从游戏控制台调用这些命令,或将拥有自定义功能的按钮添加到关卡编辑器中的游戏对象。
    1. UFUNCTION([specifier1=setting1, specifier2, ...], [meta(key1="value1", key2, ...)])
    2. ReturnType FunctionName([Parameter1, Parameter2, ..., ParameterN1=DefaultValueN1, ParameterN2=DefaultValueN2]) [const];

    函数说明符(常见):

    BlueprintCallable 蓝图可调用

    • 可设置返回值
    • 可通过参数形式,返回多个参数
    1. UFUNCTION(BlueprintCallable, Category="methods")
    2. void FunBlueprintCallable1();
    3. UFUNCTION(BlueprintCallable, Category = "methods")
    4. bool FunBlueprintCallable2(FString Path,float input1, const float& input2, float& output, TArray Points1, const TArray& Points2, TArray& Position );

    BlueprintPure 蓝图纯函数(无执行线,必须要有返回值)

    • 没有执行引脚
    • 必须要有返回值
    1. UFUNCTION(BlueprintPure, Category = "methods")
    2. float FunBlueprintPure1();
    3. UFUNCTION(BlueprintPure, Category = "methods")
    4. void FunBlueprintPure2(float& Value);

    BlueprintImplementableEvent 蓝图可实现事件(覆盖C++)

    • 无需再C++写实现函数,需要在蓝图override
    • 有返回值和无返回值 有所区别
    1. UFUNCTION(BlueprintImplementableEvent, Category = "methods")
    2. void FunBlueprintImplementableEvent1();
    3. UFUNCTION(BlueprintImplementableEvent, Category = "methods")
    4. void FunBlueprintImplementableEvent2(float& Value);

    BlueprintNativeEvent 蓝图原生事件(C++必须实现,蓝图也可以实现)

    • C++中定义事件,C++和蓝图中都可以实现(C++必须实现)。
    • 如果蓝图不实现,会执行C++的函数实现
    • 如果蓝图和C++都实现,蓝图则会覆盖C++实现,只执行蓝图实现.
    • C++函数实现,需要额外定义一个名为:函数名+_Implementation的返回值和参数列表都一致的函数.
    • BlueprintNativeEvent需要配合BlueprintCallable一起使用,否则蓝图中不可调用
    • 有返回值和无返回值 表现形式有所区别
    1. //DisplayName 蓝图中实际调用的函数名
    2. //DeprecationMessage显示警告信息
    3. //此处参数用Fsting str 编译不通过
    4. UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "methods", meta=(DisplayName="FunBlueprintNativeEvent测试",DeprecatedFunction, DeprecationMessage = "This FunBlueprintNativeEvent 的测试."))
    5. void FunBlueprintNativeEvent(const FString& str="From C++");
    1. void AMyActor::FunBlueprintNativeEvent_Implementation(const FString& str)
    2. {
    3. GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Blue, str);
    4. }

    Exec 控制台可调用函数

    此函数可从游戏中的控制台中执行。Exec命令仅在特定类中声明时才产生作用。包括:

    • Pawns,
    • Player Controllers,
    • Player Input,
    • Cheat Managers,
    • Game Modes,
    • Game Instances,
    • overriden Game Engine classes,
    • Huds
    1. UFUNCTION(Exec, Category = "methods")
    2. void FunExec(float Value);
    1. void AMyPawn::FunExec(float Value)
    2. {
    3. GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Blue, FString::Printf(TEXT("BPNative C++ Call Value:%f"), Value));
    4. }

    meta说明符

    ExpandEnumAsExecs:多执行流(多返回值)

    1. UENUM(BlueprintType)
    2. enum class BranchOutput : uint8
    3. {
    4. Branch0,
    5. Branch1,
    6. Branch2,
    7. };
    8. UFUNCTION(BlueprintCallable, Category = "methods", Meta = (ExpandEnumAsExecs = "Branches"))
    9. void FunExpandEnumAsExecs(int32 Input, BranchOutput& Branches);
    1. void AMyActor::FunExpandEnumAsExecs(int32 Input, BranchOutput& Branches)
    2. {
    3. if (Input == 0){
    4. Branches = BranchOutput::Branch0;
    5. }
    6. else if(Input == 1){
    7. Branches = BranchOutput::Branch1;
    8. }
    9. else{
    10. Branches = BranchOutput::Branch2;
    11. }
    12. }

     

    参考链接:

    虚幻引擎UFunction | 虚幻引擎5.0文档 (unrealengine.com)

    【UE4 C++ 基础知识】<2> UFUNCTION宏、函数说明符、元数据说明符 - 砥才人 - 博客园 (cnblogs.com)

  • 相关阅读:
    RabbitMQ:路由模式
    select/poll/epoll 学习
    Python NumPy的基本使用
    代理IP与Socks5代理的技术奇妙之旅
    Linux学习记录——삼십일 socket编程---TCP套接字
    2022杭电多校第四场题解
    Feign(黑马程序员)
    基金的估值,费用,会计核算
    k8s环境生成自签名证书配置secret tls并配置到浏览器
    小米在B站,跟1500万年轻人聊了啥?
  • 原文地址:https://blog.csdn.net/Jason6620/article/details/126503337