MaxSpectators

MaxSpectators

#Overview

name: MaxSpectators

The value of this variable can be defined or overridden in .ini config files. 1 .ini config file referencing this setting variable.

It is referenced in 7 C++ source files.

#Summary

#Usage in the C++ source code

The purpose of MaxSpectators is to define the maximum number of spectators allowed by the game server. This setting variable is primarily used in the game session management system of Unreal Engine 5.

The MaxSpectators variable is primarily relied upon by the Engine module, specifically within the GameFramework subsystem. It is used in the AGameSession class, which is responsible for managing game sessions, including player and spectator limits.

The value of this variable is typically set in the game configuration files or through code. It can be initialized in the following ways:

  1. Through global configuration (UPROPERTY(globalconfig))
  2. By passing options when initializing the game session (InitOptions function)
  3. By overriding the value using MaxSpectatorsOptionOverride

MaxSpectators interacts with other variables such as:

  1. MaxPlayers: Another limit defining the maximum number of active players
  2. MaxSpectatorsOptionOverride: An optional override for the MaxSpectators value

Developers must be aware of the following when using this variable:

  1. It affects the server’s capacity for spectators, which can impact performance and networking requirements
  2. The actual enforcement of this limit occurs in the AtCapacity function
  3. It can be dynamically changed during runtime, which may affect ongoing game sessions

Best practices when using this variable include:

  1. Set an appropriate value based on your game’s requirements and server capabilities
  2. Consider the relationship between MaxSpectators and MaxPlayers to balance active players and spectators
  3. Use the MaxSpectatorsOptionOverride when you need to temporarily change the limit without altering the global configuration
  4. Regularly check and log the MaxSpectators value (as done in the DumpSessionState function) to ensure it’s set correctly
  5. When implementing custom game modes or session handlers, respect this limit to maintain consistent behavior across the engine

#Setting Variables

#References In INI files

Location: <Workspace>/Engine/Config/BaseGame.ini:68, section: [/Script/Engine.GameSession]

#References in C++ code

#Callsites

This variable is referenced in the following C++ source code:

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Classes/GameFramework/GameSession.h:30

Scope (from outer to inner):

file
class        class AGameSession : public AInfo

Source code excerpt:

	/** Maximum number of spectators allowed by this server. */
	UPROPERTY(globalconfig)
	int32 MaxSpectators;

	/** Maximum number of players allowed by this server. */
	UPROPERTY(globalconfig)
	int32 MaxPlayers;

	/** Restrictions on the largest party that can join together */

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Classes/GameFramework/GameSession.h:281

Scope (from outer to inner):

file
class        class AGameSession : public AInfo

Source code excerpt:

	TOptional<int32> MaxPlayersOptionOverride;

	/** Override for the default value of MaxSpectators passed to InitOptions. */
	TOptional<int32> MaxSpectatorsOptionOverride;
};

/** 
 * Returns the player controller associated with this net id
 * @param PlayerNetId the id to search for

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/GameSession.cpp:137

Scope (from outer to inner):

file
function     void AGameSession::PostReloadConfig

Source code excerpt:

		if (MaxSpectatorsOptionOverride.IsSet())
		{
			MaxSpectators = *MaxSpectatorsOptionOverride;
		}
	}
}

bool AGameSession::HandleStartMatchRequest()
{

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/GameSession.cpp:161

Scope (from outer to inner):

file
function     void AGameSession::InitOptions

Source code excerpt:

	if (UGameplayStatics::HasOption(Options, TEXT("MaxSpectators")))
	{
		MaxSpectators = UGameplayStatics::GetIntOption(Options, TEXT("MaxSpectators"), MaxSpectators);
		MaxSpectatorsOptionOverride = MaxSpectators;
	}
	
	if (GameMode)
	{
		UClass* PlayerStateClass = GameMode->PlayerStateClass;
		APlayerState const* const DefaultPlayerState = (PlayerStateClass ? GetDefault<APlayerState>(PlayerStateClass) : nullptr);

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/GameSession.cpp:352

Scope (from outer to inner):

file
function     bool AGameSession::AtCapacity

Source code excerpt:

	if ( bSpectator )
	{
		return ( (GameMode->GetNumSpectators() >= MaxSpectators)
		&& ((GetNetMode() != NM_ListenServer) || (GameMode->GetNumPlayers() > 0)) );
	}
	else
	{
		const int32 MaxPlayersToUse = CVarMaxPlayersOverride.GetValueOnGameThread() > 0 ? CVarMaxPlayersOverride.GetValueOnGameThread() : MaxPlayers;

#Loc: <Workspace>/Engine/Source/Runtime/Engine/Private/GameSession.cpp:446

Scope (from outer to inner):

file
function     void AGameSession::DumpSessionState

Source code excerpt:

{
	UE_LOG(LogGameSession, Log, TEXT("  MaxPlayers: %i"), MaxPlayers);
	UE_LOG(LogGameSession, Log, TEXT("  MaxSpectators: %i"), MaxSpectators);

	UOnlineEngineInterface::Get()->DumpSessionState(GetWorld());
}

bool AGameSession::CanRestartGame()
{

#Loc: <Workspace>/Engine/Source/Runtime/EngineSettings/Classes/GameSessionSettings.h:16

Scope (from outer to inner):

file
class        class UGameSessionSettings : public UObject

Source code excerpt:

	/** Maximum number of spectators allowed by this server. */
	UPROPERTY(globalconfig, EditAnywhere, Category=GameSessionSettings)
	int32 MaxSpectators;

	/** Maximum number of players allowed by this server. */
	UPROPERTY(globalconfig, EditAnywhere, Category=GameSessionSettings)
	int32 MaxPlayers;

    /** Is voice enabled always or via a push to talk key binding. */