Inject on a class/monobehaviour with ITickable interface doesnt call its ticks
Hi there,
I noticed that if i have a class which implements ITickable interface, then its tick method doesnt get called when i objectResolver.inject(myClassImplementingITackable)
How do i solve this case? Thanks
Please refer to the detailed information here.
@stenyin thanks for your reply, i actually did read it but couldnt understand due to being noob in it,
all the injections and everything works on my myClassImplementingITickable after i call
objectResolver.inject(myClassImplementingITickable)
but not Tick
so what i'm missing here? And i cant declare it as entrypoint because these classes i create in IAsyncStartable.StartAsync()
Thanks for being patient with me
unless you are saying i can call builder.RegisterEntryPoint<FooController>(); anytime in future for my classes other than "real entry point"
Hello @kenofori, The objectResolver.Inject(myClassImplementingITackable) call only handles dependency injection into your instance - it won't automatically register the object as an ITickable. The Inject() method's sole responsibility is injecting dependencies, not registering objects with VContainer's systems.
Use EntryPointDispatcher.
public class MyGameLifetimeScope : LifetimeScope
{
protected override void Configure(IContainerBuilder builder)
{
builder.RegisterEntryPoint<MyTickable>(); // Will be automatically registered as ITickable
}
}
While not the good practice, here's one possible approach you can reference:
public class MyTickable: ITickable
{
private readonly List<ITickable> _tickables = new();
public void AddTickable(ITickable tickable)
{
_tickables.Add(tickable);
}
public void Tick()
{
foreach (var tickable in _tickables)
{
tickable.Tick();
}
}
}
After injecting your dependencies, you can manually add your tickable instance to the list:
Container.Resolve<MyTickable>().AddTickable(myClassImplementingITickable);