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

Singletons: ensure there is no duplicate Singletons of the same type when bind/resolve them in child-scope! #618

Open
Watcher3056 opened this issue Jan 19, 2024 · 0 comments

Comments

@Watcher3056
Copy link

Watcher3056 commented Jan 19, 2024

Hi!
I have a bit specific case(or not).
In my game I have:

  • Root Scope
  • Entry Point Scope(Basically first scene load)
  • Level Scope

What I want is to declare all needed dependencies only for specific scene. For example for specific Level. While so there should be possibility to declare similar dependencies(I mean bind same Singletons) in other scenes.

What I want to achieve:

  • User can launch scene on specific level avoiding entry point and this should work without issues, because all dependencies(Singletons) would be created on launch, because they are declared in Scene Scope
  • User can launch game from Entry Point and then launch Level creating Level Scope. Despite the fact they have similar Singleton binds they should avoid creating local instance of Singleton in case when it's declared in Root Scope.

In my current situation when entering Entry Point Scope(Entry Scene) and it loads Level(Level Scope) 3 instances of the same Singleton creating despite the fact it's already declared(bind) in a Root Scope.

image

RootScope:

public class RootScope : LifetimeScope
{
    [SerializeField]
    private CameraManager cameraManagerPrefab;

    protected override void Configure(IContainerBuilder builder)
    {
        base.Configure(builder);

        builder.RegisterComponentInNewPrefab<CameraManager>(cameraManagerPrefab, Lifetime.Singleton);
    }
    private void Start()
    {
        DontDestroyOnLoad(Container.Resolve<CameraManager>());
    }
}

Entry Point Scope:

public class EntryPointScope : LifetimeScope
{
    [SerializeField]
    private CameraManager cameraManagerPrefab;

    protected override void Configure(IContainerBuilder builder)
    {
        base.Configure(builder);

        builder.RegisterComponentInNewPrefab<CameraManager>(cameraManagerPrefab, Lifetime.Singleton);
    }
    private void Start()
    {
        DontDestroyOnLoad(Container.Resolve<CameraManager>());

        SceneManager.LoadScene(gameObject.scene.buildIndex + 1, LoadSceneMode.Additive);
    }
}

Level Scope:

public class LevelScope : LifetimeScope
{
    [SerializeField]
    private CameraManager cameraManagerPrefab;

    protected override void Configure(IContainerBuilder builder)
    {
        base.Configure(builder);

        builder.RegisterComponentInNewPrefab<CameraManager>(cameraManagerPrefab, Lifetime.Singleton);
    }
    private void Start()
    {
        DontDestroyOnLoad(Container.Resolve<CameraManager>());
    }
}

Note: only two instances of CameraManager was actually created(one from Entry Point Scope and another from Level Scope), because Root Scope Start() was not triggered, so it wasn't resolved and created.
Also seems like there is no possibilities to trigger non-lazy Singleton creation from the Root Scope to ensure CameraManager is instantiated(else your would not be able to see the game, lol)

@Watcher3056 Watcher3056 changed the title Singletons: ensure there is Singletons of the same type when bind/resolve them in child-scope! Singletons: ensure there is no duplicate Singletons of the same type when bind/resolve them in child-scope! Jan 19, 2024
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