The problem is that I'm not getting proper values in sub-container installer constructor. For instance, if I'm creating my poolable facadeObject with factory like this: QuxFactory.Create(3.1415);
Then in QuxInstaller constructor I'm getting 0 for a float parameter instead of 3.1415.. But I'm getting expected results if I'm not bind my QuxFactory
like FromPoolableMemoryPool
. So I'm confused how could I get my params in sub-container installer for further injection into sub-container's dependencies?
Here is a code from Extenject docs that I'm playing around:
I have my factory injected and I'm instantiating new instances like this
_shipFactory.Create(Random.RandomRange(2, 20));
public class GameInstaller : MonoInstaller
{
[SerializeField] private Object[] shipPrefabs;
public override void InstallBindings()
{
Container.BindInterfacesTo<GameRunner>().AsSingle();
Container.BindFactory<float, ShipFacade, ShipFacade.Factory>().
FromPoolableMemoryPool(x => x.WithInitialSize(2).FromSubContainerResolve().
ByNewPrefabInstaller<ShipInstaller>(GetPrefab));
}
private Object GetPrefab(InjectContext context)
{
return shipPrefabs[Random.Range(0, shipPrefabs.Length)];
}
}
public class ShipFacade : IPoolable<float, IMemoryPool>, IDisposable
{
private IMemoryPool _memoryPool;
private float _speed;
...
blah
...
public void OnSpawned(float speed, IMemoryPool memoryPool)
{
_memoryPool = memoryPool;
_speed = speed; //here I'm getting correct value
}
public void Dispose()
{
_memoryPool.Despawn(this);
}
public class Factory : PlaceholderFactory<float, ShipFacade>
{
}
}
public class ShipInstaller : Installer<ShipInstaller>
{
private readonly float _speed;
public ShipInstaller([InjectOptional] float speed)
{
Debug.Log(speed); // here I'm getting 0 !, instead of Random between 2 : 20
_speed = speed;
}
public override void InstallBindings()
{
Container.Bind<ShipFacade>().AsSingle();
Container.Bind<Transform>().FromComponentOnRoot();
Container.BindInterfacesTo<ShipInputHandler>().AsSingle();
Container.BindInstance(_speed).WhenInjectedInto<ShipInputHandler>();
Container.Bind<ShipHealthHandler>().FromNewComponentOnRoot().AsSingle();
}
}
Further when I'm injecting float to ShipInputHandler it injects as 0;
And I think there is a 'typo' in documentation page in this line:
Container.BindFactory<Vector3, Foo, Foo.Factory>().
FromMonoPoolableMemoryPool<Foo>(x => x.WithInitialSize(2).
FromComponentInNewPrefab(FooPrefab).UnderTransformGroup("FooPool"));
It won't work with FromMonoPoolableMemoryPool<Foo>()
, cuz we have a parameter Vector3. It should be either FromMonoPoolableMemoryPool<Vector3, Foo>()
or FromMonoPoolableMemoryPool()
. If I'm correct here..