【发布时间】:2021-11-09 15:56:41
【问题描述】:
这是我的代码。
void AFpsCharacter::ClientRPCInitializeHud_Implementation()
{
AFpsPlayerController* PlayerController = (AFpsPlayerController*)GetOwner();
if (IsValid(PlayerController))
{
AFpsPlayerState* State = PlayerController->GetPlayerState<AFpsPlayerState>();
UE_LOG(LogTemp, Log, TEXT("PlayerController now on %s"), *State->GetPlayerName());
HUD = (AFpsHud*)PlayerController->GetHUD(); //AHUD* HUD in AFpsCharacter.
//HUD = Cast<AFpsHud>(PlayerController->GetHUD());
if (!IsValid(HUD))
{
UE_LOG(LogTemp, Log, TEXT("AFpsCharacter::InitializeHud() : HUD is not valid"));
}
}
else
{
UE_LOG(LogTemp, Log, TEXT("PlayerController is not exist"));
}
}
函数ClientRPCInitializeHud() 在角色生成并且PlayerController 拥有它时运行。我的问题是指针HUD 的值无效。当然,我检查了GameMode 的默认HUD。我该怎么做才能获得APlayerController::GetHUD 的有效值?
我只是按照下面的步骤运行ClientRPCInitializeHud。
- 在控件蓝图中的按钮事件上运行
PlayerController的函数。 - 运行
ServerRPCSpawnAsPlayableCharacter函数 - 调用
APlayerController::Possess(SpawnedCharacter)后运行ClientRPCInitializeHud函数。
这里是代码。
void AFpsPlayerController::OnSelectedTeam(EPlayerTeam team, TSubclassOf<class AFpsCharacter> CharacterClass, FTransform SpawnTransform)
{
ServerRPCSetTeam(team);
ServerRPCSpawnAsPlayableCharacter(CharacterClass, SpawnTransform);
}
void AFpsPlayerController::ServerRPCSpawnAsPlayableCharacter_Implementation(TSubclassOf<AFpsCharacter> CharacterClass, FTransform SpawnTransform)
{
FActorSpawnParameters SpawnParameters;
AFpsCharacter *SpawnedCharacter = GetWorld()->SpawnActor<AFpsCharacter>(CharacterClass, SpawnTransform.GetLocation(), SpawnTransform.GetRotation().Rotator(), SpawnParameters);
SpawnedCharacter->SetSpawnTransform(SpawnTransform);
Possess(SpawnedCharacter);
SpawnedCharacter->OnPossess();
}
void AFpsCharacter::OnPossess()
{
ClientRPCInitializeHud();
}
我尝试检查 GetPlayerController(0)->GetHUD 是否有效,如果在 Level Blueprint 或 Widget Blueprint 中调用,但它也是无效的。
【问题讨论】:
标签: c++ unreal-engine4