[UE4]方法标签(总结)

来源:互联网 发布:流量互刷软件 编辑:程序博客网 时间:2024/06/06 09:13

UE4中有两种函数, 一种是传统的C++函数, 一种是UFunction. UFunction通过加入一些描述符来支持一些特殊功能。

UFUNCTION([specifier, specifier, …], [meta(key=value, key=value, …)])
ReturnType FunctionName([Parameter, Parameter, …])

下面来介绍各个标签的作用

  • BlueprintImplementableEvent/蓝图实现
  • BlueprintNativeEvent/蓝图可选择实现
  • Exec/控制台命令
  • BlueprintPure/蓝图无Exec引脚
  • Const关键字/蓝图无Exec引脚
  • BlueprintCosmetic/服务端不执行
  • BlueprintCallable/蓝图可调用
  • Server/在服务端执行
  • Client/在客户端执行
  • NetMulticast/客户端多播
  • Reliable/可靠通道执行
  • Unreliable/不可靠通道执行
  • Category/蓝图中的显示分类
  • WithValidation/在服务端执行时的校验
  • BlueprintAuthorityOnly/网络所有权执行

BlueprintImplementableEvent

源码描述:

This function is designed to be overridden by a blueprint. Do not provide a body for this function;
the autogenerated code will include a thunk that calls ProcessEvent to execute the overridden body.

总结:

此方法被蓝图重写,不要在源码中写方法体内容。自动生成的代码将包含一个形式转换,将调用ProsessEvent去执行重写的方法体。当传递数组时,一定要用,&符号。举例如下:

void Test([const] TArray<FPlayerSelect>& playerSelect){ }

是否使用const在蓝图中的表现也是有区别的,区别如下:
• 有const,蓝图中实现时,是事件。
• 没有const,蓝图中实现时,是方法,引用传递有返回值。

代码示例:

.h文件中

UFUNCTION(BlueprintImplementableEvent)void TestFunc();

.cpp文件中不写


BlueprintNativeEvent

源码描述:

This function is designed to be overridden by a blueprint, but also has a native implementation.
Provide a body named [FunctionName]_Implementation instead of [FunctionName]; the autogenerated
code will include a thunk that calls the implementation method when necessary.

总结:

这个方法可以被蓝图重写,但也可以有其自己的默认实现。需要提供一个方法体名称为,[FunctionName]_Implementation 的替代方法,当需要时,代码将包含一个形式转换,转换成蓝图的实现,或者默认的实现。

代码示例:

.h文件中

UFUNCTION(BlueprintNativeEvent)void TestFunc();

.cpp文件中

Void AMyActor::TestFunc_Implementation() { }

Exec

源码描述:

This function is executable from the command line.

总结:

可以使用命令行来调用此方法,根据网上的资料显示,只有以下类的方法可以支持Exec标签:

  • APawn
  • APlayerController
  • UPlayerInput
  • UCheatManager
  • AGameMode
  • UGameInstance
  • AHUD

如果要带参数,则在控制台之后加空格区分,例如:

UFUNCTION(Exec)FuncA(int32 Param1, FString Param2)

则在调用时,按“~”键,输入“FuncA空格123空格HelloUnreal”
测试过,入参可以使用指针,编译通过,实际情况没人会这么做的。

代码示例:

MyGameMode.h

UFUNCTION(Exec)void TestExecMacro();

MyGameMode.cpp

void AMyGameMode::TestExecMacro(){    GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red, TEXT("Hello Unreal"));}

BlueprintPure

源码描述:

This function fulfills a contract of producing no side effects, and additionally implies BlueprintCallable.

总结:

这个修饰符修饰的方法具有以下特点:

  • 在蓝图中没有白色的Exec引脚
  • 必须带返回值
  • 必须使用Category
代码示例:

.h 文件中

UFUNCTION(BlueprintPure, Category = "Default")int32 TestPureFunc();

.cpp 文件中

int32 AMyActor::TestPureFunc(){    return 0;}

Const(C++关键字)

总结:

当方法的后面跟着const关键字时,此方法在蓝图中的表现,是没有exec引脚的节点。

UFUNCTION(BlueprintCallable, Category = "Character")float GetMaxHealth() const;

BlueprintCosmetic

源码描述:

This function is cosmetic and will not run on dedicated servers

总结:

表现函数, 不会在专用服务器执行.


BlueprintCallable

源码描述:

This function can be called from blueprint code and should be exposed to the user of blueprint editing tools.

总结:

此方法能够从蓝图节点调用,能够被暴露给蓝图编辑器


Server

源码描述:

This function is replicated, and executed on servers. Provide a body named [FunctionName]_Implementation instead of [FunctionName];
the autogenerated code will include a thunk that calls the implementation method when necessary.

总结:

此方法是可复制的,并且只在服务端执行。需要提供[FunctionName]_Implementation名称的方法代替原方法名;
当需要时,代码将包含一个形式转换,转换成蓝图的实现,或者默认的实现。
需要其他的修饰符:

  • Reliable 或 Unreliable
  • WithValidation

此方法如果是非PlayerController,PlayerState,受控制的Pawn,需要使用SetOwner,设置一个连接到服务端的PlayerController对象,才能使用对应客户端PlayerController的连接。

代码示例:

.h文件中写

UFUNCTION(Server, Reliable, WithValidation, Category = "Default")void TestFunc();

.cpp中写

void TestClass::TestFunc_Implementation() { }bool TestClass::TestFunc_Validate(){     // 可以根据某个变量来控制执行服务器逻辑方法的有效性    If(syncBool)    {        return true;    }    Else    {        return false;    } }

Client

源码描述:

This function is replicated, and executed on clients. Provide a body named [FunctionName]_Implementation instead of [FunctionName];
the autogenerated code will include a thunk that calls the implementation method when necessary.

总结:

此方法是可复制的,并且只在客户端执行。需要提供[FunctionName]_Implementation名称的方法代替原方法名;
当需要时,代码将包含一个形式转换,转换成蓝图的实现,或者默认的实现。
需要其他的修饰符:

  • Reliable 或 Unreliable
代码示例:

.h文件中写

UFUNCTION(Client, Reliable, Category = "Default")void TestFunc();

.cpp中写

void TestClass::TestFunc_Implementation() { }

NetMulticast

源码描述:

This function is both executed locally on the server and replicated to all clients, regardless of the Actor’s NetOwner

总结:

不管Actor的NetOwner是什么,多播方法在本地和服务器都执行,并且复制到所有客户端
需要其他的修饰符:

  • Reliable 或 Unreliable
代码示例:

.h文件中写

UFUNCTION(NetMulticast, Reliable, Category = "Default")void TestFunc();

.cpp中写

void TestClass::TestFunc_Implementation() { }

Reliable

源码描述:

Replication of calls to this function should be done on a reliable channel.
Only valid when used in conjunction with Client or Server

总结:

调用此方法的复制会在一个可靠的通道上完成。
仅在结合了Client或Server标签时有效。


Unreliable

源码描述:

Replication of calls to this function can be done on an unreliable channel.
Only valid when used in conjunction with Client or Server

总结:

调用此方法的复制会在一个不可靠的通道上完成。
仅在结合了Client或Server标签时有效。


Category

源码描述:

Specifies the category of the function when displayed in blueprint editing tools.
Usage: Category=CategoryName or Category=”MajorCategory,SubCategory”

总结:

在蓝图编辑显示时指定方法的类型。
用法:Category=CategoryName 或 Category=”MajorCategory,SubCategory”


WithValidation

源码描述:

This function must supply a _Validate implementation

总结:

在Server标签使用时使用,需要实现_Validate

代码示例:

.h文件中写

UFUNCTION(Server, Reliable, WithValidation, Category = "Default")void TestFunc();

.cpp中写

void TestClass::TestFunc_Implementation() { }bool TestClass::TestFunc_Validate(){     // 可以根据某个变量来控制执行服务器逻辑方法的有效性    If(syncBool)    {        return true;    }    Else    {        return false;    } }

BlueprintAuthorityOnly

源码描述:

This function will not execute from blueprint code if running on something without network authority

总结:

如果没有网络权限,将不会从蓝图代码执行此函数。
拥有所有权的那一端,可以执行此方法。

  • 如果Actor在服务端Spawn,则服务端有权限。
  • 如果Actor在客户端Spawn,则客户端有权限。
0 0
原创粉丝点击