main() blog

プログラムやゲーム、旅、愛する家族について綴っていきます。

【UE4】GameMode、GameState、PlayerState、PlayerControllerの関連を確認してみる

GameModeとGameStateとPlayerStateの関連について再度確認してみる。

毎回、
「GameModeはサーバーだけにしかなくて、GameStateは各端末毎に生成されるんだっけ?」
「PlayerStateは?PlayerControllerは?」
とその都度確認しているので再度確認してみる。

各クラスの概要

UE4のオフィシャルドキュメント。

簡単にまとめると以下の通り。

GameMode

  • サーバーにしか存在しない

GameState

  • 各マシン毎に存在
  • PlayerArrayという変数でプレイヤ分のPlayerStateを保持
  • Ownerは持たない(GetOwner()はnullptr)

PlayerState

  • 全てのマシン上で、全てのプレイヤーに対してPlayerStateが存在
  • OwnerはPlayerController(GetOwner()でPlayerControllerを返す)

PlayerController

  • 操作しているPawnを所持(GetControlledPawn()で操作しているPawnが取得できる)
  • Ownerは持たない(GetOwner()はnullptr)

Playerが操作しているPawn

  • SpawnされてPossessされるとPlayerControllerに所有される

シングルプレイ、マルチプレイでの構成について確認してみる。

シングルプレイの場合

GameMode GameStateの関連図.png

マルチプレイの場合

Server

GameMode GameStateの関連図(Server).png

Client1

GameMode GameStateの関連図(Client1).png

Client2

GameMode GameStateの関連図(Client2).png

確認

パーシスタントレベルにBPを組んでログで確認。
※BPのスクリーンショットは割愛

シングルプレイ

LogBlueprintUserMessages: [Test_Map_C_1] found GameMode  : MyGameMode
LogBlueprintUserMessages: [Test_Map_C_1]   found GameState : MyGameState
LogBlueprintUserMessages: [Test_Map_C_1]     GameState : not found Owner
LogBlueprintUserMessages: [Test_Map_C_1]     found PlayerState : MyPlayerState
LogBlueprintUserMessages: [Test_Map_C_1]       found Owner : MyPlayerController
LogBlueprintUserMessages: [Test_Map_C_1]         PlayerController : not found Owner
LogBlueprintUserMessages: [Test_Map_C_1]           found GetControlledPawn : MyPlayerPawn
LogBlueprintUserMessages: [Test_Map_C_1]           locally controlled
LogBlueprintUserMessages: [Test_Map_C_1] all actors > PlayerPawn : 1
LogBlueprintUserMessages: [Test_Map_C_1]   Player : MyPlayer
LogBlueprintUserMessages: [Test_Map_C_1] all actors > PlayerController : 1
LogBlueprintUserMessages: [Test_Map_C_1]   player controller : MyPlayerController

マルチプレイ

Server

GameModeもGameStateもClinet含めたPlayerStateもPlayerController、PlayerPawnも全て生成されていることが確認できる。
PlayerPawnは必ずしも順番通りにはならいことに注意。

LogBlueprintUserMessages: [Test_Map_C_2] Server: found GameMode  : MyGameMode
LogBlueprintUserMessages: [Test_Map_C_2] Server:   found GameState : MyGameState
LogBlueprintUserMessages: [Test_Map_C_2] Server:     GameState : not found Owner
LogBlueprintUserMessages: [Test_Map_C_2] Server:     found PlayerState  MyPlayerState
LogBlueprintUserMessages: [Test_Map_C_2] Server:       found Owner : MyPlayerController
LogBlueprintUserMessages: [Test_Map_C_2] Server:         found GetControlledPawn : MyPlayerPawn
LogBlueprintUserMessages: [Test_Map_C_2] Server:         locally controlled
LogBlueprintUserMessages: [Test_Map_C_2] Server:     found PlayerState : MyPlayerState1
LogBlueprintUserMessages: [Test_Map_C_2] Server:       found Owner : MyPlayerController1
LogBlueprintUserMessages: [Test_Map_C_2] Server:         found GetControlledPawn : MyPlayrPawn1
LogBlueprintUserMessages: [Test_Map_C_2] Server:         not locally controlled
LogBlueprintUserMessages: [Test_Map_C_2] Server:     found PlayerState : MyPlayerState2
LogBlueprintUserMessages: [Test_Map_C_2] Server:       found Owner : MyPlayerController2
LogBlueprintUserMessages: [Test_Map_C_2] Server:         found GetControlledPawn : MyPlayerPawn2
LogBlueprintUserMessages: [Test_Map_C_2] Server:         not locally controlled
LogBlueprintUserMessages: [Test_Map_C_2] Server: all actors > PlayerPawn : 3
LogBlueprintUserMessages: [Test_Map_C_2] Server:   Player : MyPlayerPawn2
LogBlueprintUserMessages: [Test_Map_C_2] Server:   Player : MyPlayerPawn
LogBlueprintUserMessages: [Test_Map_C_2] Server:   Player : MyPlayerPawn1
LogBlueprintUserMessages: [Test_Map_C_2] Server: all actors > PlayerController : 3
LogBlueprintUserMessages: [Test_Map_C_2] Server:   player controller : MyPlayerController
LogBlueprintUserMessages: [Test_Map_C_2] Server:   player controller : MyPlayerController1
LogBlueprintUserMessages: [Test_Map_C_2] Server:   player controller : MyPlayerController2
Client1

クライアントの場合はGameModeは生成されていない。
GameStateは生成されてPlayerArrayを回すとPlayerState自体はプレイヤー人数分取得できる。
PlayerControllerは自分のみ生成されている。
PlayerPawnはプレイヤー分生成されない。
実際に2人分のみが生成されていることが分かる。
他の端末との距離や表示範囲などで同期が必要ない場合はSpawnされない。
一定範囲や描画されるなどで同期が必要になったタイミングで初めてSpawnされる。
必ず同期取りたい場合はPawnのプロパティにAlwaysRelevantを有効にすることで必ず同期を取るようになるので最初にSpawnされるようになる。

LogBlueprintUserMessages: [Test_Map_C_2] Client 1: not found GameMode
LogBlueprintUserMessages: [Test_Map_C_2] Client 1:   found GameState : MyGameState
LogBlueprintUserMessages: [Test_Map_C_2] Client 1:     GameState : not found Owner
LogBlueprintUserMessages: [Test_Map_C_2] Client 1:     found PlayerState : MyPlayerState
LogBlueprintUserMessages: [Test_Map_C_2] Client 1:       not found Owner
LogBlueprintUserMessages: [Test_Map_C_2] Client 1:     found PlayerState : MyPlayerState1
LogBlueprintUserMessages: [Test_Map_C_2] Client 1:       found Owner : MyPlayerController
LogBlueprintUserMessages: [Test_Map_C_2] Client 1:         found GetControlledPawn : PlayerPawn
LogBlueprintUserMessages: [Test_Map_C_2] Client 1:         locally controlled
LogBlueprintUserMessages: [Test_Map_C_2] Client 1:     found MyPlayerState : MyPlayerState2
LogBlueprintUserMessages: [Test_Map_C_2] Client 1:       not found Owner
LogBlueprintUserMessages: [Test_Map_C_2] Client 1: all actors > Player : 2
LogBlueprintUserMessages: [Test_Map_C_2] Client 1:   Player : PlayerPawn
LogBlueprintUserMessages: [Test_Map_C_2] Client 1:   Player : PlayerPawn2
LogBlueprintUserMessages: [Test_Map_C_2] Client 1: all actors > PlayerController : 1
LogBlueprintUserMessages: [Test_Map_C_2] Client 1:   player controller : MyPlayerController

AlwaysRelevantを有効にした場合は全員Spawnされている。

    :
    :
LogBlueprintUserMessages: [Test_Map_C_2] Client 2: all actors > PlayerPawn : 3
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:   Player : MyPlayerPawn
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:   Player : MyPlayerPawn1
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:   Player : MyPlayerPawn2
Client2

Clinet2も同様。

LogBlueprintUserMessages: [Test_Map_C_2] Client 2: not found GameMode
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:   found GameState : MyGameState
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:     GameState : not found Owner
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:     found PlayerState : MyPlayerState
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:       not found Owner
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:     found PlayerState : MyPlayerState1
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:       not found Owner
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:     found PlayerState : MyPlayerState2
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:       found Owner : MyPlayerController
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:         found GetControlledPawn : MyPlayerPawn
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:         locally controlled
LogBlueprintUserMessages: [Test_Map_C_2] Client 2: all actors > PlayerPawn : 2
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:   Player : MyPlayerPawn
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:   Player : MyPlayerPawn1
LogBlueprintUserMessages: [Test_Map_C_2] Client 2: all actors > PlayerController : 1
LogBlueprintUserMessages: [Test_Map_C_2] Client 2:   player controller : MyPlayerController

参考

imoue.hatenablog.com