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

Trait that adds one-time initialization lifecycle hooks to any class. More...

#include <Malena/Engine/App/Lifecycle.h>

Inheritance diagram for ml::Lifecycle:
[legend]

Public Member Functions

virtual ~Lifecycle ()=default
virtual void onInit ()
 Called once after construction, before the first frame.
virtual void onReady ()
 Called once after onInit(), when all components are registered.

Detailed Description

Trait that adds one-time initialization lifecycle hooks to any class.

Lifecycle provides two virtual hooks that bracket the startup phase of an object. Both have empty default implementations — override only what you need.

Hook When it fires
onInit() Once, immediately after construction, before the first frame
onReady() Once, after onInit(), once all components are registered

The distinction between the two hooks mirrors the pattern established by frameworks such as Angular and iOS UIKit:

  • onInit() is the place to perform setup that does not depend on other components being fully registered (load resources, configure properties).
  • onReady() is the place to wire logic that requires all sibling objects to already exist (cross-component references, initial state calculations).

Usage

// ml::Application already inherits Lifecycle — just override the hooks
class MyApp : public ml::Application
{
public:
MyApp() : ml::Application(1280, 720, 32, "My App") {}
protected:
void onInit() override
{
_button.setSize({120.f, 40.f});
_button.setPosition({100.f, 100.f});
addComponent(_button);
}
void onReady() override
{
// _button is fully registered — safe to reference it here
_button.onClick([]{ std::cout << "clicked\n"; });
}
private:
ml::Rectangle _button;
};
void addComponent(Core &component)
Register a Core object with the application's component manager.
Primary entry point for Malena applications without a manifest.
virtual void onInit()
Called once after construction, before the first frame.
Definition Lifecycle.h:82
virtual void onReady()
Called once after onInit(), when all components are registered.
Definition Lifecycle.h:90
Definition Component.h:22

Lifecycle is independent of SceneLifecycle. Opt into both if your class is a scene that also needs one-time initialization hooks:

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

Definition at line 72 of file Lifecycle.h.

Constructor & Destructor Documentation

◆ ~Lifecycle()

virtual ml::Lifecycle::~Lifecycle ( )
virtualdefault

Member Function Documentation

◆ onInit()

virtual void ml::Lifecycle::onInit ( )
inlinevirtual

Called once after construction, before the first frame.

Override to perform initial setup — configure component properties, load resources, and register objects with the framework. At this point no other sibling components are guaranteed to be fully registered.

Definition at line 82 of file Lifecycle.h.

◆ onReady()

virtual void ml::Lifecycle::onReady ( )
inlinevirtual

Called once after onInit(), when all components are registered.

Override to wire logic that depends on other components already existing — event callbacks, cross-component bindings, or derived initial state.

Definition at line 90 of file Lifecycle.h.


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