Skip to content

Make ITickable and IInitializable to be Created from factory or instantiated from Container #294

@Battou2501

Description

@Battou2501

I need to be able to create instances of classes that implement ITickable and so they Tick method will be properly called after that.
Right now any way of creating instances and/or binding them using Container do not trigger execution of their Tick method.

I have tried next classes and methods of creating instances. None of them trigger Tick or Initialize.

public class TestZenTick : MonoBehaviour
{
    private DiContainer _container;
    private TestTickable.Factory _factory;
    
    [Inject]
    private void Construct(DiContainer container, TestTickable.Factory factory)
    {
        _container = container;
        _factory = factory;
    }
    
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        if (_factory == null || _container == null) return;

        var t = new  TestTickable();
        _container.BindInterfacesAndSelfTo<TestTickable>().FromInstance(t).AsSingle().NonLazy();
        _factory.Create();
        _container.Instantiate<TestTickable>();
        _container.BindInterfacesAndSelfTo<TestTickable>().FromFactory<TestTickable,TestTickable.Factory>().NonLazy();
        _container.BindInterfacesAndSelfTo<TestTickable>().FromNew().NonLazy();
    }

    public class TestTickable : ITickable, IInitializable
    {
        public void Tick()
        {
            Debug.Log("Tick");
        }
        
        public class Factory : PlaceholderFactory<TestTickable> { }

        public void Initialize()
        {
            Debug.Log("Initialize");
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions