GithubHelp home page GithubHelp logo

filip-drabinski / godot.dependencyinjection Goto Github PK

View Code? Open in Web Editor NEW
50.0 4.0 6.0 87 KB

Godot.DependencyInjection

License: MIT License

C# 100.00%
dependency-injection godot godot-engine godot-game-engine godotengine

godot.dependencyinjection's Introduction

Godot.DependencyInjection

Godot.DependencyInjection is a lightweight and easy-to-use dependency injection framework for the Godot game engine, specifically tailored for C#. It aims to help developers create more modular, testable, and maintainable game projects using the Godot engine.

Features

  • Easy to set up and use
  • Supports property, field, and method injection
  • Provides member dependency injection for usage with resources
  • Utilizes standard C# dependency injection abstractions
  • Supports transient, scoped, and singleton lifetimes

Installation

You can install the package via NuGet or clone the repository and add a project reference to the solution created by Godot.

Usage

using Godot.DependencyInjection.Services.Input;

public partial class RegularNode : Node2D
{
    [Inject]
    public IInputService inputService;

    [Inject]
    public IService Service { get; set; }

    [Inject]
    public IService[] Services1 { get; set; }

    [Inject]
    public IEnumerable<IService> Services2 { get; set; }

    [Export]
    [InjectMembers]
    public CustomResource Resource { get; set; }

    [Inject]
    public void Inject(IService service, IService[] services1, IEnumerable<IService> services2)
    {
        // ***
    }
}

Configuration

  1. Create a script for dependency manager:
    public partial class DependencyInjectionNode : DependencyInjectionManagerNode
    {
    }
  2. Add the script to Project -> Project Settings -> Autoload.
  3. Create a Node and script for dependency registration:
    public partial class DependencyRegistrationNode : Node, IServicesConfigurator
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddGodotServices();
            services.AddTransient<IService, Service>();
        }
    }
  4. Add dependency registration node to scene

Remarks

Please note that this project is in its early stages of development and may require significant improvements. The framework is functional but may not be suitable for production use in its current state. Any feedback, suggestions, or contributions to improve the framework are highly appreciated.

Contributing

I'm currently in the process of reevaluating the architecture and scope of the project, so only pull requests containing changes to documentation might be merged. If you have any ideas for code improvements, please create a new issue instead.

Support

If you have any questions, issues, or suggestions, please create a new issue on the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for more information.

godot.dependencyinjection's People

Contributors

filip-drabinski avatar hawkerm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

godot.dependencyinjection's Issues

Upgrade to 0.3.0 from 0.2.3 fails with get_RawNode error

Describe the bug
Tried updating to latest 0.3.0 and saw an issue from the Godot Debug window, if I downgrade the same code to 0.2.3 (and is the same code I had with 0.2.2) - it works fine.

To Reproduce
Steps to reproduce the behavior:

  1. Follow instructions in Readme to add package to project
  2. Take the project with <PackageReference Include="Godot.DependencyInjection" Version="0.2.3"/> dependency
  3. Update to 0.3.0
  4. See failure

E 0:00:01:0026 :0 @ void Godot.DependencyInjection.DependencyInjectionManagerNode._EnterTree(): System.TypeLoadException: Method 'get_RawNode' in type 'Godot.DependencyInjection.NodeWrapper' from assembly 'Godot.DependencyInjection, Version=0.3.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
<C++ Error> System.TypeLoadException
<C++ Source> :0 @ void Godot.DependencyInjection.DependencyInjectionManagerNode._EnterTree()
:0 @ void Godot.DependencyInjection.DependencyInjectionManagerNode._EnterTree()
Node.cs:2057 @ Boolean Godot.Node.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
:0 @ Boolean Godot.DependencyInjection.DependencyInjectionManagerNode.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name& , Godot.NativeInterop.NativeVariantPtrArgs , Godot.NativeInterop.godot_variant& )
CSharpInstanceBridge.cs:24 @ Godot.NativeInterop.godot_bool Godot.Bridge.CSharpInstanceBridge.Call(IntPtr , Godot.NativeInterop.godot_string_name* , Godot.NativeInterop.godot_variant** , Int32 , Godot.NativeInterop.godot_variant_call_error* , Godot.NativeInterop.godot_variant* )

Expected behavior
Dependency Injection system should load.

Screenshots
If applicable, add screenshots to help explain your problem.

image

Desktop (please complete the following information):

  • OS: Windows 10, Godot 4.1.1

Additional context
Add any other context about the problem here.

Feedback

I'm currently in the process of reevaluating the architecture and scope of the project. Please feel free to provide feedback and share your ideas for new features here.

Inject attribute unusable private method

I'm sorry I'm not good at English.

I want to hide the inject method from the outside. Can you support inject for private methods?
The Inject attribute does not work in code like the following:

public partial class Dependent : Node
{
    [Inject]
    private void Constructor(ISomeService service)
    {
        service.Foo();
    }
}

[Docs/Feedback] Injected members are unusable at `_on_ready`

DependencyInjectionManagerNode injects all nodes at the Ready stage.

This is called first to children, then to their parents. Because of this, all nodes have their fields set to null even if the services are defined properly.

Not entirely sure what's the intended way of doing this, maybe you thought we should use this instead. Regardless, seems pretty easy to solve.

[Inject]
public void _ReadyInject() {}
Unrelated but would be nice to have more Node dependency support, similar to how Zenject for Unity works. E.g adding nodes existing in the scene tree to the ServiceCollection automatically, their interfaces etc.

I made my own solution which adds nodes in the tree implementing IServiceNode to the service collection.

Edit:
The workaround shown above won't work for injected fields or properties. Fields and properties are not injected at the time the [Inject] method is called, but parameters are.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.