Check out our latest project ✨ OpenChapter.io: free ebooks the way its meant to be 📖

Dependency Injection Node

An asset by truevision
The page banner background of a mountain and forest
Dependency Injection Node hero image

Quick Information

0 ratings
Dependency Injection Node icon image
truevision
Dependency Injection Node

A utility node for handling Dependency Injection the Godot way ✨

Supported Engine Version
4.4
Version String
1.0.0
License Version
GPLv3
Support Level
community
Modified Date
1 day ago
Git URL
Issue URL

Godot Dependency Injection Node

This is a library that offers a node to handle injection during or after instantiation It has been tested on Godot 4.4+

How it works

This is a very basic library that works as a node with utility methods on it.
The injection does not happen in the constructor because classes that extend from Node cannot have parameters
Instead we go with another option which is to tag a method in the class with an attribute and inject on that.
The code uses factory methods to handle the injection of the nodes during instantiation.

Basic usage

public partial class App : DIContainerNode {
    public App() {
        // This sets up the underlying service collection
        CreateServiceCollection();

        // You can register instaces or services like normal
        _serviceCollection.AddSingleton(this);
        _serviceCollection.AddSingleton<DbManager>();
        
        // You can also register classes that extend from a node class but dont need a scene
        // This is useful for something that might just be a manager that handles data or events

        // This will create the node and insert it into the tree as a child of this node
        _serviceCollection.AddSingleton<IGameManager>(InjectNodeClass<GameManager>())
        _serviceCollection.AddSingleton<IAudioManager>(InjectNodeClass<AudioManager>())

        // You can use a scene as well to create something DI managed
        // This is added as a child of the tree root
        _serviceCollection.AddSingleton<IDebugViewManager>(InjectInstantiatedPackedScene<DebugViewManager>("res://components/ui/DebugViewManager.tscn"))

        // Or inject on an already created node if it was created by the scene
        // The injection on these will run during the build service container method
        AddDeferredInjectedInstance(GetNode<Camera3D>("%Camera3D"));

        // Finish up by building the service provider
        BuildServiceContainer();

        // There is another utility that can help you with scoping services per scene
        CreateSceneScope();

        // Close the scene scope in your scene manager and then create a new one
        // App.CloseSceneScope();
        // App.CreateSceneScope();

        // If you have a global game manager then you can get the service right here
        // Since its created an added to the game it can be used as an entrypoint for the rest of your game.
        _serviceProvider.GetRequiredService<IGameManager>();
    }
}

"How do you get a service if a node is created dynamically?"

The factory method of this utility tries to recursively inject anything in an object that it created but that also poses an issue that nodes can be created way after this Container is set up.

The best way that I have had success would be to use your DI container as a singleton pattern. After that you can bind your classes from inside the class that was instantiated.

It might look something like this

public partial class App : DIContainerNode {
    public static App Instance {get; set;}
    public App() {
        if (Instance is not null) {
            QueueFree();
            return;
        }
        Instance = this;

        // then add the rest of the code for setting up your services
    }
}

Then in some other class

public partial class Enemy : Node2D {
    private Player _player;
    public override void _EnterTree(){
        _player = App.Instance.ServiceProvider.GetRequiredService<IPlayerManager>().Player;
    }
    /// the res of your enemy code
}

A utility node for handling Dependency Injection the Godot way ✨

Reviews

0 ratings

Your Rating

Headline must be at least 3 characters but not more than 50
Review must be at least 5 characters but not more than 500
Please sign in to add a review

Quick Information

0 ratings
Dependency Injection Node icon image
truevision
Dependency Injection Node

A utility node for handling Dependency Injection the Godot way ✨

Supported Engine Version
4.4
Version String
1.0.0
License Version
GPLv3
Support Level
community
Modified Date
1 day ago
Git URL
Issue URL

Open Source

Released under the AGPLv3 license

Plug and Play

Browse assets directly from Godot

Community Driven

Created by developers for developers