+1 vote

Hi,

is it possible to use Dependency Injection in Godot (GDscript)? Maybe its already implemented in some way and im just not seeing it.

Should you even bother using DI or are there better ways to decouple things in Godot (apart from signals) ?

Im not having a specific problem, just curious if DI is good practice in Godot (if you can even use it).

Thanks.

Godot version 3.4.4
in Engine by (78 points)

1 Answer

+2 votes
Best answer

Give your class an init-function with the DI-object as parameter and call it after you create the class instance with .new(). Basically like a constructor in other languages.

by (129 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.