Install Asset
Install via Godot
To maintain one source of truth, Godot Asset Library is just a mirror of the old asset library so you can download directly on Godot via the integrated asset library browser
Quick Information
A utility node for handling Dependency Injection the Godot way ✨
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
Quick Information
A utility node for handling Dependency Injection the Godot way ✨