0

I have a Character class. Its mono and have CharacterInstaller. And I'm using MonoPoolableMemoryPool for Character pool. But I have to bind derived class by character runtime parameters. Is there any way to do that?


    public enum Type
    {
        MELEE,
        RANGED,
    }
    
    public class CharacterCreator
    {
        Character.Factory _factory;

        private void CreateMelee()
        {
            _factory.Create(Type.Melee);
        }
    }

 public class CharacterInstaller : MonoInstaller
    {
      
        public override void InstallBindings()
        {
            if (type == Type.Melee)
            {
                
                Container.Bind<IAttackSystem>.To<MeleeAttackSystem>();
            }
            else
            {
                Container.Bind<IAttackSystem>.To<RangedAttackSystem>();
            }
        }
        
}
 
     

I didnt find any solution.

1 Answers1

0

Pools in Zenject require specific prefabs per pool. Depending on how you're binding your pool you may need a different pool per character type.

It's hard to say without seeing how you're binding the pool. Can you show that, too?

Also, when using pools I've found that I need to do less injecting inside constructors and instead pass params into the factory/pool creation method and then cache the dynamic params with whatever pooled object is implementing Zenject.IPoolable.

Here's how I'm binding my pool for projectiles:

Container.BindFactory<Projectile.Settings, Projectile, Projectile.Factory>()
    .FromPoolableMemoryPool( pool => pool
        .FromSubContainerResolve()
        .ByNewContextPrefab( _gun.ProjectilePrefab )
        .WithGameObjectName( _gun.ProjectilePrefab.name )
        .UnderTransform( context => context.Container.ResolveId<Transform>( _gun.ProjectilePoolId ) )
    );

Here's how I'm grabbing an available projectile:

private readonly Projectile.Factory _factory;

// ...

Projectile newProjectile = _factory.Create( _projectileSettings );

And, finally, here's the definiton for the Projectile:

public class Projectile : 
    IPoolable<Projectile.Settings, IMemoryPool>, 
    IDisposable
{
    public void OnSpawned( Settings settings, IMemoryPool pool )
    {
        _settings = settings;
        _pool = pool;
    }

    public void Dispose()
    {
        _pool.Despawn( this );
    }

    public void OnDespawned()
    {
        _pool = null;

        // Handle despawn logic here.
        // Fire events, dispose other references, reset data, etc.
    }

    [System.Serializable]
    public class Settings
    {
        // ...
    }
}

The important thing to note is that before I implemented pooled projectiles the Settings class was being directly injected into the Projectile and was readonly. But, now that it's pooled and these settings can change dynamically depending on who's firing the projectile I do not inject them and instead pass them in as creation params.

Finally, I think that if you can provide more info I can get a better understanding of what exactly the problem is. Hope you found this helpful!

Here are two EXTREMELY helpful resources I've found when working with factories and pools:

Memory Pools: https://github.com/Mathijs-Bakker/Extenject/blob/master/Documentation/MemoryPools.md

Sub Containers: https://github.com/Mathijs-Bakker/Extenject/blob/master/Documentation/SubContainers.md

MaxHimms
  • 1
  • 1
  • Thank you. I use it the way you use it. I know pool parameters but my problem slightly different. I want to change object bindings depending the arguments. It might be impossible after that object created. But i want to know if it. – Enescan BEKTAŞ Jul 19 '23 at 18:17