Unreal Engine/기타

[UE] AGameMode와 MatchState

KANTAM 2024. 2. 24. 16:42

AGameModeBase는 아래의 설정을 담당한다. 

  • Defautl class들을 설정
  • Player의 Pawn을 spawn
  • restart player
  • restart the game

 

AGameMode는 AGameModeBase의 파생 클래스로 다음의 설정을 담당한다. 

  • Match State
  • Handling Match States
  • Custom Match Sates

 

MatchStates

게임 모드에는 MatchState namespace가 있다. 

/** Possible state of the current match, where a match is all the gameplay that happens on a single map */
namespace MatchState
{
	extern ENGINE_API const FName EnteringMap;			// We are entering this map, actors are not yet ticking
	extern ENGINE_API const FName WaitingToStart;		// Actors are ticking, but the match has not yet started
	extern ENGINE_API const FName InProgress;			// Normal gameplay is occurring. Specific games will have their own state machine inside this state
	extern ENGINE_API const FName WaitingPostMatch;		// Match has ended so we aren't accepting new players, but actors are still ticking
	extern ENGINE_API const FName LeavingMap;			// We are transitioning out of the map to another location
	extern ENGINE_API const FName Aborted;				// Match has failed due to network issues or other problems, cannot continue

	// If a game needs to add additional states, you may need to override HasMatchStarted and HasMatchEnded to deal with the new states
	// Do not add any states before WaitingToStart or after WaitingPostMatch
}

 

게임은 MatchStates의 위의 순서로 진행되는데, 여기서 custom MatchState를 넣으려면 WaitingToStart 후, WatingPostMatch 전에 추가해야 한다. 예시로 게임모드의 StartMatch()를 보면 MatchState를 변경시키면서 게임을 진행한다는 것을 볼 수 있다. 

void AGameMode::StartMatch()
{
	if (HasMatchStarted())
	{
		// Already started
		return;
	}

	//Let the game session override the StartMatch function, in case it wants to wait for arbitration
	if (GameSession->HandleStartMatchRequest())
	{
		return;
	}

	SetMatchState(MatchState::InProgress);
}

 

OnMatchStateSet()은 SetMatchState에서 호출된다(MatchState가 변경되면). 현재 MatchState에 따라서 Delegate와 다른 함수들을 호출한다. 

void AGameMode::OnMatchStateSet()
{
	FGameModeEvents::OnGameModeMatchStateSetEvent().Broadcast(MatchState);
	// Call change callbacks
	if (MatchState == MatchState::WaitingToStart)
	{
		HandleMatchIsWaitingToStart();
	}
	else if (MatchState == MatchState::InProgress)
	{
		HandleMatchHasStarted();
	}
	else if (MatchState == MatchState::WaitingPostMatch)
	{
		HandleMatchHasEnded();
	}
	else if (MatchState == MatchState::LeavingMap)
	{
		HandleLeavingMap();
	}
	else if (MatchState == MatchState::Aborted)
	{
		HandleMatchAborted();
	}
}

OnMatchStateSet은 virtual 함수이므로, override를 통해 MatchState가 변경됨에 따라 우리가 취해야 하는 동작을 넣을 수 있다(예를 들어, 게임 시작 전에는 HUD를 표시하지 않는다).

 

이외에도 MatchState와 관련된 함수들이 있다. 

  • HasMatchStarted()
  • HasMatchEnded()
  • GetMatchState()
  • SetMatchState()
  • OnMatchStateSet()

 

게임모드에는 bDelayedStart 변수가 있다. 값이 true라면 WaitingToStart에서 MatchState가 머무르게 할 수 있다. 여기서 다음 MatchState로 갈려면(게임을 시작하려면) StartMatch()를 호출한다. 

bool AGameMode::ReadyToStartMatch_Implementation()
{
	// If bDelayed Start is set, wait for a manual match start
	if (bDelayedStart)
	{
		return false;
	}

	// By default start when we have > 0 players
	if (GetMatchState() == MatchState::WaitingToStart)
	{
		if (NumPlayers + NumBots > 0)
		{
			return true;
		}
	}
	return false;
}