Loading...
Searching...
No Matches
ml::SceneLifecycle Class Reference

Trait that adds scene transition lifecycle hooks to any class. More...

#include <Malena/Engine/Scenes/SceneLifecycle.h>

Public Member Functions

virtual ~SceneLifecycle ()=default
virtual void onSceneEnter ()
 Called every time this scene becomes the active scene.
virtual void onSceneExit ()
 Called every time this scene is deactivated.
virtual void onScenePause ()
 Called when this scene is pushed to the background by another scene.
virtual void onSceneResume ()
 Called when this scene is returned to the foreground.

Detailed Description

Trait that adds scene transition lifecycle hooks to any class.

SceneLifecycle provides four virtual hooks that fire on scene transitions managed by SceneManager. All have empty default implementations — override only what you need.

Hook When it fires
onSceneEnter() Every time this scene becomes the active scene
onSceneExit() Every time this scene is deactivated
onScenePause() This scene is pushed to the background by another
onSceneResume() This scene is returned to the foreground

The key distinction from Lifecycle is that these hooks fire on every transition, not just once at startup. A cached scene that is entered, exited, and re-entered will have onSceneEnter() called twice but Lifecycle::onInit() called only once.

Hook firing order

// First activation
// Another scene is pushed on top
// Returned to foreground
// Scene is deactivated and removed
virtual void onSceneExit()
Called every time this scene is deactivated.
virtual void onSceneResume()
Called when this scene is returned to the foreground.
virtual void onSceneEnter()
Called every time this scene becomes the active scene.
virtual void onScenePause()
Called when this scene is pushed to the background by another scene.

Usage

class GameScene : public ml::Scene, public ml::SceneLifecycle
{
protected:
void onSceneEnter() override
{
// Resume music, reset timers, show entry animation
}
void onSceneExit() override
{
// Pause music, persist state
}
void onScenePause() override
{
// Freeze physics, dim the scene
}
void onSceneResume() override
{
// Unfreeze physics, restore brightness
}
};
Trait that adds scene transition lifecycle hooks to any class.

Combine with Lifecycle when the scene also needs one-time initialization hooks:

class GameScene : public ml::Scene,
public ml::Lifecycle,
{ ... };
Trait that adds one-time initialization lifecycle hooks to any class.
Definition Lifecycle.h:73
See also
Lifecycle, Updatable, SceneManager

Definition at line 87 of file SceneLifecycle.h.

Constructor & Destructor Documentation

◆ ~SceneLifecycle()

virtual ml::SceneLifecycle::~SceneLifecycle ( )
virtualdefault

Member Function Documentation

◆ onSceneEnter()

virtual void ml::SceneLifecycle::onSceneEnter ( )
inlinevirtual

Called every time this scene becomes the active scene.

Override to resume audio, reset timers, trigger entry animations, or perform any setup that should repeat on each activation.

Definition at line 96 of file SceneLifecycle.h.

◆ onSceneExit()

virtual void ml::SceneLifecycle::onSceneExit ( )
inlinevirtual

Called every time this scene is deactivated.

Override to pause audio, persist state, or clean up resources that should not run while the scene is inactive.

Definition at line 104 of file SceneLifecycle.h.

◆ onScenePause()

virtual void ml::SceneLifecycle::onScenePause ( )
inlinevirtual

Called when this scene is pushed to the background by another scene.

The scene remains alive and cached but is no longer the active scene. Override to freeze simulation, suppress input handling, or dim visuals.

Definition at line 112 of file SceneLifecycle.h.

◆ onSceneResume()

virtual void ml::SceneLifecycle::onSceneResume ( )
inlinevirtual

Called when this scene is returned to the foreground.

Paired with onScenePause(). Override to restore state that was suppressed when the scene was backgrounded.

Definition at line 120 of file SceneLifecycle.h.


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