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

Multiple instances of singleton? #707

Open
Toernblom opened this issue Sep 29, 2024 · 6 comments
Open

Multiple instances of singleton? #707

Toernblom opened this issue Sep 29, 2024 · 6 comments

Comments

@Toernblom
Copy link

I've added a gameObject to the VContainerSettings and attached my rootLifetimeScope
In that rootLifetimeScope:

builder.Register<GameManager>(Lifetime.Singleton);
builder.RegisterEntryPoint<GameManager>();

Then in one of my scenes I've got a "PlayableSceneScope" which I've put on a gameObject, changed parent to the rootLifetimeScope.
In that PlayableSceneScope i do:

builder.Register<ServerConnectionController>(Lifetime.Singleton);
builder.RegisterEntryPoint<ServerConnectionController>();

In the ServerConnectionController

[Inject]
private readonly GameManager _gameManager;

In the GameManager, I've got a start method:

public void Start()
  {
      testInt = 15;
  }

If i print that value from inside GameManager its fine,
if i try to access it from ServerConnectionController which was registered by the child of the rootLifeTimeScope that value is 0, not 15, why?

@Toernblom
Copy link
Author

image

image

The VContainer Diagnostics clearly shows its the same instance right?

@Toernblom
Copy link
Author

image

Are there different instances depending on the contract type?

@Toernblom
Copy link
Author

I fixed it by removing RegisterEntrypoint, adding a monobehaviour called "StartCaller" that calls Start instead. I dont think this is how its supposed to work..?

@RamiAhmed
Copy link

RamiAhmed commented Oct 3, 2024

I don't think you're supposed to use both Register and RegisterEntryPoint - if I understand correctly RegisterEntryPoint is just a convenience wrapper around Register that only handles entry point interface implementations (IStartable, IDisposable, etc.). You could try using builder.Register<T>.AsImplementedInterfaces().AsSelf() as that will register the type, bind it by its own type (T) and any interfaces it implements.

You can use Start inside the LifetimeScope (your root lifetime scope), as it's essentially just a MonoBehaviour underneath

@lucasmontec
Copy link

You asked it to register 2 instances, and you got two. That's how it's supposed to work because VContainer supports collections.
Register entry point just registers with implemented interfaces:

public static RegistrationBuilder RegisterEntryPoint<T>(
            this IContainerBuilder builder,
            Lifetime lifetime = Lifetime.Singleton)
        {
            EntryPointsBuilder.EnsureDispatcherRegistered(builder);
            return builder.Register<T>(lifetime).AsImplementedInterfaces();
        }

@stenyin
Copy link
Contributor

stenyin commented Oct 27, 2024

public class GameEntryPoint : IStartable
{
    private readonly GameManager _gameManager;

    public GameEntryPoint(GameManager gameManager)
    {
        _gameManager = gameManager;
    }

    public void Start()
    {
        _gameManager.Initialize(); // Sets testInt or other initial values here
    }
}

Try using a GameEntryPoint to initialize GameManager and register GameEntryPoint in the RootLifetimeScope.

builder.Register<GameManager>(Lifetime.Singleton);
builder.RegisterEntryPoint<GameEntryPoint>();

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

4 participants