NT-2.2.2|控制权限
大约 3 分钟
前言
经过前面几章的讨论我们对UE的网络复制用法有了些许了解,本章将对网络控制权限方面进行进一步深入。
HasAuthority
bool AActor::HasAuthority() const
{
return (Role == ROLE_Authority);
}

ENetRole
bool AActor::HasAuthority() const
{
return (Role == ROLE_Authority);
}
//其中的Role 就是这个枚举
/** 在本地/远程网络环境中,演员的网络角色 */
UENUM()
enum ENetRole : int
{
/** 没有角色。 */
ROLE_None,
/** 该演员的本地模拟代理。 */
ROLE_SimulatedProxy,
/** 该演员的本地自治代理。 */
ROLE_AutonomousProxy,
/** 对该演员的权威控制。 */
ROLE_Authority,
ROLE_MAX,
};
IsLocallyControlled
bool APawn::IsLocallyControlled() const
{
return ( Controller && Controller->IsLocalController() );
}



服务端在Pawn中判断是不是本地
HasAuthority() && IsLocallyControlled()

客户端在Pawn中判断是不是本地
!HasAuthority() && IsLocallyControlled()
环境 | HasAuthority() | IsLocallyControlled() | 意义说明 |
---|---|---|---|
✅ 服务端控制的 Pawn | true | true | 服务器上本地控制的 Pawn(主控) |
🟡 服务端复制的 Pawn | true | false | 服务器上远程控制的 Pawn(非本地) |
✅ 客户端本地 Pawn | false | true | 客户端上本地控制的 Pawn(主控) |
❌ 客户端远程 Pawn | false | false | 客户端上同步来的其他玩家 Pawn |
bool AController::IsLocalController() const
{
const ENetMode NetMode = GetNetMode();
if (NetMode == NM_Standalone)
{
// Not networked.
return true;
}
if (NetMode == NM_Client && GetLocalRole() == ROLE_AutonomousProxy)
{
// Networked client in control.
return true;
}
if (GetRemoteRole() != ROLE_AutonomousProxy && GetLocalRole() == ROLE_Authority)
{
// Local authority in control.
return true;
}
return false;
}