Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declaring Factory for Object which doesn't have all its dependencies yet #668

Open
EmmetOT opened this issue Jan 8, 2020 · 0 comments
Open

Comments

@EmmetOT
Copy link

EmmetOT commented Jan 8, 2020

Hey, I have a specific situation where I have a state machine which I want to be global and scene independent. I want to be able to create new instances of states, states can be scene-dependent (such as a 'combat encounter state') and these scene-dependent states will have dependencies which exist only in that scene (such as 'enemies in the combat encounter'.)

Because I want to create new state objects, I have factories set up for each state class, and contained in a global state factory class which the state machine object has a dependency on. However because this factory class is bound in the start scene, and therefore in a different context than the combat encounter scene, none of the scene-specific bindings are bound correctly when I try to create an instance of the combat encounter state.

Here's a simple example with all the state machine complexity stripped out:

I have an empty MonoBehaviour, ObjectInGameScene, and a class ObjectWithDependency:

public class ObjectWithDependency
{
    private readonly ObjectInGameScene m_object;

    public ObjectWithDependency(ObjectInGameScene objectInGameScene)
    {
        m_object = objectInGameScene;

        Debug.Log("Created a dependency object. Object is null: " + (m_object == null), m_object);
    }

    public class Factory : PlaceholderFactory<ObjectWithDependency>
    {

    }
}

I have a TestFactoryContainer class which just has a dependency on this object's factory:

public class TestFactoryContainer
{
    private readonly ObjectWithDependency.Factory m_factory;

    public TestFactoryContainer(ObjectWithDependency.Factory factory)
    {
        m_factory = factory;
    }

    public void Spawn()
    {
        m_factory.Create();
    }
}

My scenes are set up so that one scene, Start, loads the other Game. Start contains the TestFactoryContainer class whereas the actual ObjectInGameScene object exists in Game. Usually I try binding the factory and factory container in an installer in the start scene, and binding the actual object in the game scene, but unsurprisingly this results in the following error:

ZenjectException: Unable to resolve 'ObjectInGameScene' while building object with type 'ObjectWithDependency'. Object graph:
ObjectWithDependency

because the ObjectWithDependency and the factory classes don't share a Container. I understand that the solution might involve using scene parenting or scene decorator contexts but I've been unable to get either to work. Or maybe this entire approach to using Zenject is flawed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant