![06d64bcdb2a83646f41c94092b31dab3.png](https://img-blog.csdnimg.cn/img_convert/06d64bcdb2a83646f41c94092b31dab3.png)
最近抽空把 Bluelua 计划的最后一部分功能内容做完了,就是在 lua 中重载 UE4 中的网络事件,这样就可以直接在 lua 中重写网络相关的逻辑了。还有就是将之前重载纯蓝图函数和事件几个崩溃修复了。网络相关的重写示例在 BlueluaDemo 的 NetTest 文件夹中
UE4 中网络事件分两种,一种是 C++ 中的网络事件,就是在 UFUNCTION
中带上 Server/NetMulticast/Client
关键字,另一种是在蓝图中,创建一个 Custom Event,然后在这个事件的复制属性中选择 Run On Server/Multicast/Run on owning Client
,如图
![f11d97f65e05e20b9173f3f64802ff95.png](https://img-blog.csdnimg.cn/img_convert/f11d97f65e05e20b9173f3f64802ff95.png)
这两种是互相独立的,也就是 C++ 中的 Server/NetMulticast/Client
函数是无法在蓝图中进行重写,所以如果有这样的需求就需要在 C++ 的 Server/NetMulticast/Client
函数中去调用其它 BlueprintNativeEvent/BlueprintImplementable
函数,将这个事件抛到蓝图中,略显麻烦。Bluelua 中就不用这么麻烦了,现在可以直接在 lua 中分别重写这两类网络事件
重写 C++ 网络事件
首先在 ANetCharacter
的 C++ 类中定义三个函数,一个可复制属性和属性的修改通知函数
UFUNCTION(Unreliable, Server, WithValidation)
void TestNativeServerFunction();
UFUNCTION(Unreliable, NetMulticast)
void TestNativeNetMulticastFunction();
UFUNCTION(Unreliable, Client)
void TestNativeClientFunction();
UPROPERTY(ReplicatedUsing=OnRep_Counter)
int32 Counter;
UFUNCTION(BlueprintNativeEvent)
void OnRep_Counter();
实现这几个函数
void ANetCharacter::TestNativeClientFunction_Implementation()
{
UE_LOG(LogTemp, Display, TEXT("%sTestNativeClientFunction get called"), *GetPrefix(this));
}
void ANetCharacter::TestNativeNetMulticastFunction_Implementation()
{
UE_LOG(LogTemp, Display, TEXT("%sTestNativeNetMulticastFunction get called"), *GetPrefix(this));
}
void ANetCharacter::TestNativeServerFunction_Implementation()
{
UE_LOG(LogTemp, Display, TEXT("%sTestNativeServerFunction get called"), *GetPrefix(this));
TestNativeNetMulticastFunction(); // will run on local and remote
TestNativeClientFunction(); // will run on remote
}
bool ANetCharacter::TestNativeServerFunction_Validate()
{
return true;
}
void ANetCharacter::OnRep_Counter_Implementation()
{
UE_LOG(LogTemp, Displ