1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UE4でSeamlessTravel後のBeginPlayでGetPlayerController(0)が古いPlayerControllerを取得してしまう

Posted at

マルチプレイのSeamlessTravel後のLevelBlueprintやActorのBeginPlay時にGetPlayerControllerが古いPlayerControllerを取得してしまう現象が起きたので内容と対応方法をメモしておきます。
UE4の仕様とかエディタのコードはちゃんと追えてません。
UE4.25.3で確認しました。

発生した現象

OnlineSubsystemを用いたマルチプレイで以下のような関数をサーバーで実行してマップ遷移していた。

bool ATestGameMode::Travel(FString LevelName)
{
	bUseSeamlessTravel = true;
	return GetWorld()->ServerTravel(LevelName, false, false);
}

以下のようにBeginPlay時にPlayerControllerを取得すると古いものを取得していた。
(GameModeのOnSwapPlayerControllersなどで取得できるPlayerControllerのDisplayNameで一致を確認)
capgetplyercontroller.PNG

対応したこと

以下のような関数を使うようにして対応。

UFUNCTION(BlueprintPure, Category = MyUtils, meta = (WorldContext="WorldContextObject", UnsafeDuringActorConstruction="true"))
	static class APlayerController* GetFirstLocalPlayerController(const UObject* WorldContextObject);
class APlayerController* UMyUtils::GetFirstLocalPlayerController(const UObject* WorldContextObject)
{
	if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull))
	{
		if (ULocalPlayer* LocalPLayer = World->GetFirstLocalPlayerFromController())
		{
			return LocalPLayer->GetPlayerController(World);
		}
	}
	return nullptr;
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?