UE4 VR局域网(二)基础知识

复制

1.引用类型可以复制。(实际传递的是引用对象的ID(FNetworkGUID)

2.大部分组件不支持复制。

 

 

1.使用组件的set is replicated时,可以用RepNotify服务器和客户端执行相同的同步后的事件):

I managed to resolve the issue by getting rid of the Set Is Replicated nodes and making the variable of DeltaRELCombine (the one which is set after calculations and determines where the new StaticMesh should be placed) RepNotify. In the On Rep function I implemented the StaticMeshComponent addition so that it executes both on the client and the server in the same way. I also had to play with the dynamic material instances a bit as they where replicated only upon some significant events.

 

2.同步static mesh

 https://answers.unrealengine.com/questions/21524/replicate-static-mesh.html  //利用创建static meshactor 解决。

 

3.变量同步,事件通知

 例:

//声明示例

UPROPERTY(Transient, Replicated)

TArray<class AShooterWeapon*> Inventory;

 

//声明示例

UPROPERTY(Transient, ReplicatedUsing = OnRep_CurrentWeapon)

class AShooterWeapon* CurrentWeapon;

 

//此函数不需要声明,用来设置不同条件的定义同步变量

cpp加上#include "UnrealNetwork.h"

 

  void AShooterCharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const

{

Super::GetLifetimeReplicatedProps(OutLifetimeProps);

// only to local owner: weapon change requests are locally instigated, other clients don't need it

DOREPLIFETIME_CONDITION(AShooterCharacter, Inventory, COND_OwnerOnly);

// everyone except local owner: flag change is locally instigated

DOREPLIFETIME_CONDITION(AShooterCharacter, bIsTargeting, COND_SkipOwner);

DOREPLIFETIME_CONDITION(AShooterCharacter, LastTakeHitInfo, COND_Custom);

// everyone

DOREPLIFETIME(AShooterCharacter, CurrentWeapon);

DOREPLIFETIME(AShooterCharacter, Health);

}

 

上面的事件通知可以传递此变量同步前的值作为参数,像这样:

UFUNCTION()

void OnRep_CurrentWeapon(class AShooterWeapon* LastWeapon);

void AShooterCharacter::OnRep_CurrentWeapon(AShooterWeapon* LastWeapon)

{

SetCurrentWeapon(CurrentWeapon, LastWeapon);

}

 

注意:

1.结构体不可以同步。

2.服务器上OnRep_CurrentWeapon这个函数不会触发,只会在客户端上触发!如下


4.replicate属性下的replicate movement这个同步功能:1.位置同步。2.旋转同步。actor不能同步缩放.

 

5.C++中同步函数使用注意:

   如:

void AMyActor::DoFun()

{

if (Role<ROLE_Authority)

{

ServerRotateFunitureEnter(_begin,_end);

return;    //存在和不存在的区别!!!

}

 

//逻辑部分

}

//省略Validate函数

Void AMyActor::ServerDoFun_Implementation()

{

DoFun();

}

 

基本上RPC函数都是这样的模型,当客户端调用DoFun()时,没有return时,客户端调用后转到服务器上执行逻辑部分后,客户端自己又会执行一次逻辑。所以,如果逻辑部分只在服务器上执行逻辑部分时需要加上return,这样客户端只需要等待服务器执行完逻辑部分后的同步效果。


相关性

 

   

 

//关联性先用看到来理解。

Only relevant to onwer :不关联给别人,只有自己能看的。

Always relevant :  始终关联,大家都能看到的。

Net user owner relevancy: 根据自己的所有者来决定关联性。

Net cull distance squared: 在一定距离内能被看到。

 

//注意!

Only relevant to owner 使用时,从服务器上生成的话不会同步到客户端即使勾上了replicates

owner默认为谁生成这个actor,这个actor的owner就是谁。可以AActor::setowner()来设置owner。

 

 

Priority(优先级),更新频率

 

 

//一般比较重要的如主角色优先级是3,普通actor是1.

//2通常是1更新速度两倍。

 

Net update frequency :更新频率

Min net update frequency: 最小更新频率



猜你喜欢

转载自blog.csdn.net/qq_35760525/article/details/78581764