1

I think it's better to formulate the problem via code. I have a BaseClass.

public abstract class BaseUnit {

    [System.Serializable]
    public class Settings
    {
    }
}

And some derived classes, for example.

public class Archer : BaseUnit {

    public ArcherSettings Settings;

    [System.Serializable]
    public class ArcherSettings : Settings
    {
           //CanWalk is a MonoBehaviour and WalkSettings is a Serrializable class
            public CanWalk.WalkSettings WalkSettings;
    }
}

So as you can see I want to have several unit types with appropriate WalkSettings which will be set from ScriptableObject.

public class ScriptableLevelInstaller : ScriptableObjectInstaller<ScriptableLevelInstaller>
{


    public Archer.AracherSettings Aracher;
    public Knight.KnightSettings Knight;
    //Some more...
}

So the question is how to Inject appropriate settings into appropriate classes with Zenject any help or clarification would be very helpful.

---UPD---

I express myself poorly the first time. What I want is bind CanWalk.WalkSetting to approprirate settings. So I can do

Container.Bind<CanWalk.WalkSettings>().FromInstance(Archer.WalkSettings);

But this is wrong because the last binding will just override walk settings for every class. So What I need is something like

Container.Bind<CanWalk.WalkSettings>().FromInstance(Archer.WalkSettings).WhenInjectInto("CanWalk which is attached to an Archer")

For now I'm just doing this inside Aracher.

GetComponent<CanWalk>().Settings = _settings.WalkSettings;

But maybe there is something in Zenject to solve this.

Argus Kos
  • 175
  • 1
  • 12

1 Answers1

0

Just use Container.BindInstance like this:

public class ScriptableLevelInstaller : ScriptableObjectInstaller<ScriptableLevelInstaller>
{
    public Archer.AracherSettings Aracher;
    public Knight.KnightSettings Knight;

    public override void InstallBindings()
    {
        Container.BindInstance(Aracher);
        Container.BindInstance(Knight);
    }
}

If you want you can also specify the class that should get access to it like this:

public class ScriptableLevelInstaller : ScriptableObjectInstaller<ScriptableLevelInstaller>
{
    public Archer.AracherSettings Aracher;
    public Knight.KnightSettings Knight;

    public override void InstallBindings()
    {
        Container.BindInstance(Aracher).WhenInjectedInto<Archer>();
        Container.BindInstance(Knight).WhenInjectedInto<Knight>();
    }
}

But this is not necessary, which is why I tend to use the first approach

Steve Vermeulen
  • 1,406
  • 1
  • 19
  • 25
  • Thanks for the answering but I'm probable expressed myself poorly I updated the question. Would be great if you can find time to answer this one. – Argus Kos Apr 16 '18 at 19:18