ezEngine  Milestone 9
ezGameState Class Reference

ezGameState is the base class to build custom game logic upon. It works closely together with ezGameApplication. More...

#include <GameState.h>

Inheritance diagram for ezGameState:

Public Member Functions

virtual void OnActivation (ezWorld *pWorld, const ezTransform *pStartPosition) override
 When a game state was chosen, it gets activated through this function. More...
virtual void OnDeactivation () override
 Called when the game state is being shut down.
virtual void ScheduleRendering () override
 Has to call ezRenderLoop::AddMainView for all views that need to be rendered.
ezCameraGetMainCamera ()
 Gives access to the game state's main camera object.
bool IsMixedRealityMode () const
 Returns whether the application is running in full mixed reality mode. This is evaluated in OnActivation(), will always return false before that call.
- Public Member Functions inherited from ezGameStateBase
virtual void ProcessInput ()
 Called once per game update. Should handle input updates here.
virtual void BeforeWorldUpdate ()
 Called once each frame before the worlds are updated.
virtual void AfterWorldUpdate ()
 Called once each frame after the worlds have been updated.
virtual ezGameStatePriority DeterminePriority (ezWorld *pWorld) const =0
 Called by ezGameApplication to determine which game state is best suited to handle a situation. More...
virtual void RequestQuit ()
 Call this to signal that a game state requested the application to quit. More...
bool WasQuitRequested () const
 Returns whether the game state wants to quit the application.
- Public Member Functions inherited from ezReflectedClass
virtual const ezRTTIGetDynamicRTTI () const
EZ_ALWAYS_INLINE bool IsInstanceOf (const ezRTTI *pType) const
 Returns whether the type of this instance is of the given type or derived from it.
template<typename T >
EZ_ALWAYS_INLINE bool IsInstanceOf () const
 Returns whether the type of this instance is of the given type or derived from it.

Protected Member Functions

 ezGameState ()
 This class cannot be instantiated directly.
virtual void CreateMainWindow ()
 Creates a default window (ezGameStateWindow) adds it to the application and fills out m_pMainWindow and m_hMainSwapChain.
virtual void DestroyMainWindow ()
 Destroys the m_pMainWindow. Unless overridden Deactivate() will call this.
virtual void ConfigureInputDevices ()
 Configures available input devices, e.g. sets mouse speed, cursor clipping, etc. Unless overridden Activate() will call this.
virtual void ConfigureInputActions ()
 Adds custom input actions, if necessary. Unless overridden Activate() will call this.
virtual void SetupMainView (ezWindowOutputTargetBase *pOutputTarget)
 Creates a default render view. Unless overridden, Activate() will do this for the main window.
virtual ezResult SpawnPlayer (const ezTransform *pStartPosition)
 Overrideable function that may create a player object. More...
void SetupMainView (ezWindowOutputTargetBase *pOutputTarget, ezTypedResourceHandle< ezRenderPipelineResource > hRenderPipeline)
 Creates a default main view with the given render pipeline.
void ChangeMainWorld (ezWorld *pNewMainWorld)
 Sets m_pMainWorld and updates m_pMainView to use that new world for rendering.
virtual void ConfigureMainCamera ()
 Sets up m_MainCamera for first use.

Protected Attributes

ezWindowm_pMainWindow = nullptr
ezWindowOutputTargetBasem_pMainOutputTarget = nullptr
ezViewHandle m_hMainView
ezWorldm_pMainWorld = nullptr
ezCamera m_MainCamera
bool m_bStateWantsToQuit = false
bool m_bMixedRealityMode = false
bool m_bVirtualRealityMode = false
- Protected Attributes inherited from ezGameStateBase
bool m_bStateWantsToQuit = false

Additional Inherited Members

- Static Public Member Functions inherited from ezNoBase
static const ezRTTIGetStaticRTTI ()

Detailed Description

ezGameState is the base class to build custom game logic upon. It works closely together with ezGameApplication.

In a typical game there is always exactly one instance of an ezGameState derived class active. The game state handles custom game logic, which must be handled outside ezWorld, custom components and scripts.

For example a custom implementation of ezGameState may handle how to show a menu, when to switch to another level, how multi-player works, or which player information is transitioned from one level to the next. It's main purpose is to implement high-level game logic.

ezGameApplication will loop through all available ezGameState implementations and ask each available one whether it can handle a certain level. Each game state returns a 'score' how well it can handle the game.

In a typical game you only have one game state linked into the binary, so in that case there is no reason for such a system. However, in an editor you might have multiple game states available through plugins, but only one can take control. In such a case, each game state may inspect the given world and check whether it is e.g. a single-player or multi-player level, or whether it uses it's own game specific components, and then decide whether it is the best fit for that level.

Do not forget to reflect your derived class, otherwise ezGameApplication may not find it.

Member Function Documentation

◆ OnActivation()

void ezGameState::OnActivation ( ezWorld pWorld,
const ezTransform pStartPosition 

When a game state was chosen, it gets activated through this function.

pWorldThe game state is supposed to operate on the given world. In a stand-alone application pWorld will always be nullptr and the game state is expected to create worlds itself. When run inside the editor, pWorld will already exist and the game state is expected to work on it.
pStartPositionAn optional transform for the 'player object' to start at. Usually nullptr, but may be set by the editor to relocate or create the player object at the given destination.

Implements ezGameStateBase.

◆ SpawnPlayer()

ezResult ezGameState::SpawnPlayer ( const ezTransform pStartPosition)

Overrideable function that may create a player object.

By default called by OnActivation(). The default implementation will search the world for ezPlayerStartComponent's and instantiate the given player prefab at one of those locations. If pStartPosition is not nullptr, it will be used as the spawn position for the player prefab, otherwise the location of the ezPlayerStartComponent will be used.

Returns EZ_SUCCESS if a prefab was spawned, EZ_FAILURE if nothing was done.

Reimplemented in ezFallbackGameState.

The documentation for this class was generated from the following files: