Unreal Engine 网络系统(五):带宽管理(相关性及优先级)

目录

相关性

优先级

创建、查找、加入房间(Session)


网络游戏是通过计算机硬件通信方案将多台终端连接,组建的玩家沟通环境,从而使得玩家连接到一起游戏。

受限于网络传输环境的影响,软件设计本身必须要考虑网络资源的应用合理性。不能无限制使用网络带宽。软件设计的本质是优化资源分配,合理使用资源。

从引擎使用层面上,Unreal设计了很多策略来解决网络带宽的节约。

相关性

  • 用于决定是否需要在多人游戏期间复制Actor
  • 复制期间将剔除不相关的Actor,可解决带宽,以便Actor高效复制。
  • 默认的情况是根据玩家与Actor的距离来判断相关性。
  • Server上不会剔除Actor,因为需要保存所有对象信息。当玩家靠近一定距离,在向Client发送信息。
  • 覆盖IsNetRelevantFor函数,可以手动控制相关性,可使用NetCullDistanceSquared属性决定相关距离。

虚幻引擎(依次)参照以下规则确定玩家的相关Actor类组,在AActor::IsNetRelevantFor 中进行:

  1. 如果Actor是bAlwaysRelevant,归属于Pawn或PlayerController、或本身为Pawn或Pawn是某些行为的发起者,则具有相关性
  2. 如果Actor是bNetUseOwnerRelevancy 且拥有一个所有者,则使用所有者的相关性
  3. 如果Actor是bOnlyRelevantToOwner且没有通过第一轮检查,则不具有相关性
  4. 如果Actor被附加到另一个Actor骨架模型,它的相关性将取决于其所在基础的相关性
  5. 如果Actor是不可见的(bHidden==true)。并且它的RootComponent并没有碰撞,则不具有相关性
  6. 如果没有Root Component,则会记录警告,提示是否将它设置为bAlwaysRelevant=true
  7. 如果AGameNetworkManager被设置为基于距离的相关性,则只要Actor低于净剔除距离,即被视为具有相关性

代码如下:

// Engine\Souce\Runtime\Engine\Private\ActotReplication.cpp
bool AActor::IsNetRelevantFor(const AActor* RealViewer, const AActor* ViewTarget, const FVector& SrcLocation) const
{
	if (bAlwaysRelevant || IsOwnedBy(ViewTarget) || IsOwnedBy(RealViewer) || this == ViewTarget || ViewTarget == GetInstigator())
	{
		return true;
	}
	else if (bNetUseOwnerRelevancy && Owner)
	{
		return Owner->IsNetRelevantFor(RealViewer, ViewTarget, SrcLocation);
	}
	else if (bOnlyRelevantToOwner)
	{
		return false;
	}
	else if (RootComponent && RootComponent->GetAttachParent() && RootComponent->GetAttachParent()->GetOwner() && (Cast<USkeletalMeshComponent>(RootComponent->GetAttachParent()) || (RootComponent->GetAttachParent()->GetOwner() == Owner)))
	{
		return RootComponent->GetAttachParent()->GetOwner()->IsNetRelevantFor(RealViewer, ViewTarget, SrcLocation);
	}
	else if(IsHidden() && (!RootComponent || !RootComponent->IsCollisionEnabled()))
	{
		return false;
	}

	if (!RootComponent)
	{
		UE_LOG(LogNet, Warning, TEXT("Actor %s / %s has no root component in AActor::IsNetRelevantFor. (Make bAlwaysRelevant=true?)"), *GetClass()->GetName(), *GetName() );
		return false;
	}

	return !GetDefault<AGameNetworkManager>()->bUseDistanceBasedRelevancy ||
			IsWithinNetRelevancyDistance(SrcLocation);
}

bool AActor::IsReplayRelevantFor(const AActor* RealViewer, const AActor* ViewTarget, const FVector& SrcLocation, const float CullDistanceOverrideSq) const
{
	return IsNetRelevantFor(RealViewer, ViewTarget, SrcLocation);
}

优先级

有时没有足够的带宽复制所有相关的Actor。因此Actor拥有优先级,决定优先复制的Actor

  • PawnPlayerControllerNetPriority默认为3.0;
  • 基础Actor的NetPriority默认为1.0

创建、查找、加入房间(Session)

PS:Unreal一般不建议使用OpenLevel来加载地图和关卡,推荐使用StreamLevel。

下图是手动方式,也可以通过Level Streaming Volume来自动生成

猜你喜欢

转载自blog.csdn.net/Jason6620/article/details/129691223