Skip to content

Latest commit

 

History

History
60 lines (45 loc) · 1.57 KB

File metadata and controls

60 lines (45 loc) · 1.57 KB

Resource management

Resources (unlike assets) are the global variables of your app. They are designed to be initialized before startup and easily accessible by systems.

auto app =
    ddge::app::create()
        .plug_in(ddge::plugins::Resources{})
        .insert_resource(MyResource{})
        .build();

MyResource& my_resource = app.resource_manager.get<MyResource>();

Dependency injection

Resources can be created from other resources. In order to access another previously initialized resource, you should inject your resource using a function.

ddge::app::create()
   .plug_in(ddge::plugins::Resources{})
   .insert_resource(Dependee{})
   .inject_resource([](Dependee& dependee) { return Dependant{ dependee }; })
   .build();

Injection functions are called from the build method.

Note

The injection function should only take resources as its parameters.

Reference invalidation

References to resources are valid until the app is alive. (Which is a very long time under normal circumstances.) Feel free to keep references to resources around.

Injected resources can also keep using references acquired as parameters. Their lifecycle will always end before the resources they depend on.

Modify previous injections

Sometimes it is necessary to modify previously registered injections. For these cases, your injection is allowed to have a setup() function that takes any number of injections as arguments.

struct Injection {
    auto setup(
        AnotherInjection& another_injection
    ) -> void;

    // ...
};