1.如何在命令行中强制开始游戏
UFUNCTION(exec)
void ForceMatchStart();
2.如何用命令结束游戏
UFUNCTION(exec)
void FinishMatch();
3.在游戏中加了一个大机器人,遇到了机器人不渲染的问题,后来排查是因为下面截图选项问题,如果选 High 就会不渲染,不知道具体原因,先记录下来
4.ShooterGame中,在BotPawn派生类的蓝图中,收到Event Begin Play事件后,GetController获取到的PlayerState是None,后来发现是因为 游戏开始后才创建的 Pawn(即在HandleMatchHasStarted中调用的StartBot),而把Pawn的调用时机提前(即改到HandleMatchIsWaitingToStart时调用StartBot就可以了)
5.ShooterGame中Player的复活机制是怎样的
在ShooterGame中,FreeForAll和TeamDeathMatch两种模式都是死了以后立即复活,具体实现是在APlayerController中的BeginInactiveState函数,起了一个计时器,GameMode中MinRespawnDelay参数可以控制复活延迟,最低是1秒,代码如下
float const MinRespawnDelay = ((GameState != NULL) && (GameState->GameModeClass != NULL)) ? GetDefault<AGameMode>(GameState->GameModeClass)->MinRespawnDelay : 1.0f;
GetWorldTimerManager().SetTimer(TimerHandle_UnFreeze, this, &APlayerController::UnFreeze, MinRespawnDelay);
这里,APlayerController的派生类中可以重写UnFreeze方法,AShooterPlayerController中该方法定义如下
void AShooterPlayerController::UnFreeze()
{
ServerRestartPlayer();
}
即服务器立刻复活该玩家,我们可以重写这个方法,来定义自己的复活机制
6.SpawnActor有时创建不出actor怎么办
有可能目标点有物体,创建的时候设置一下 AlwaysSpawn就可以了,代码如下
SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
6.如何判断Pawn是不是自己客户端控制的
用下面接口即可
UFUNCTION(BlueprintCallable, Category="Pawn")
virtual bool IsLocallyControlled() const;
再看看这个接口的实现
bool APawn::IsLocallyControlled() const
{
return ( Controller && Controller->IsLocalController() );
}
其实是通过Controller来判断的,在看看Controller的实现
bool AController::IsLocalController() const
{
const ENetMode NetMode = GetNetMode();
if (NetMode == NM_Standalone)
{
// Not networked.
return true;
}
if (NetMode == NM_Client && Role == ROLE_AutonomousProxy)
{
// Networked client in control.
return true;
}
if (GetRemoteRole() != ROLE_AutonomousProxy && Role == ROLE_Authority)
{
// Local authority in control.
return true;
}
return false;
}
我们发现,最终是根据NetMode和Role一起判断的,有三种情况
1. Standalone模式返回true
2.NetMode是NM_Client,Role为ROLE_AutonomousProxy时返回true
3.第三种是在程序既是服务器也是客户端的情况下,这种情况下,也相当于是客户端控制
PS:PlayerController一般存在于服务器和玩家所在的客户端,AIController一般只存在于服务器,PlayerState存在于服务器和所有客户端,GameMode存在于服务器,GameState存在于服务器和所有客户端。
7.ShooterGame怎么控制第一人称枪的朝向的
应该是在下面这个函数里
void AShooterCharacter::OnCameraUpdate(const FVector& CameraLocation, const FRotator& CameraRotation)
{
USkeletalMeshComponent* DefMesh1P = Cast<USkeletalMeshComponent>(GetClass()->GetDefaultSubobjectByName(TEXT("PawnMesh1P")));
const FMatrix DefMeshLS = FRotationTranslationMatrix(DefMesh1P->RelativeRotation, DefMesh1P->RelativeLocation);
const FMatrix LocalToWorld = ActorToWorld().ToMatrixWithScale();
// Mesh rotating code expect uniform scale in LocalToWorld matrix
const FRotator RotCameraPitch(CameraRotation.Pitch, 0.0f, 0.0f);
const FRotator RotCameraYaw(0.0f, CameraRotation.Yaw, 0.0f);
const FMatrix LeveledCameraLS = FRotationTranslationMatrix(RotCameraYaw, CameraLocation) * LocalToWorld.Inverse();
const FMatrix PitchedCameraLS = FRotationMatrix(RotCameraPitch) * LeveledCameraLS;
const FMatrix MeshRelativeToCamera = DefMeshLS * LeveledCameraLS.Inverse();
const FMatrix PitchedMesh = MeshRelativeToCamera * PitchedCameraLS;
Mesh1P->SetRelativeLocationAndRotation(PitchedMesh.GetOrigin(), PitchedMesh.Rotator());
}