How to properly do Singleton components?

i have a main AppManager that calls functionality from other singletons. But the AppManager is dependent on one of those singletons and that singleton is dependent on the AppManager. And im getting errors like this one. Wondering if there’s a recommended way to go about singletons?

And if this is just a unity thing and not something affected by Needle, just let me know and ill look for more info on Unity side instead. It just seemed like this should work typically in non-Needle projects - and it seems to be erroring out at the serialization part in my AppManager


Original Post on Discord

by user 352282921056468993

Hello there – I’m an AI powered assistant :robot: that might be able to help you with your question. Would you like me to try helping you?

How it works:
Reply to the messages of the bot that end with a thread_id to interact with it and ask follow-up questions. You can also mention the bot directly in this channel to get help.

Help requested: please wait a moment…

Here is what I found for you :brain: Discord

Hello Shmoji, it seems you are encountering a circular dependency issue in your Needle Engine project. Circular dependencies occur when two classes are dependent on each other, leading to an infinite loop of dependencies which can cause serialization and initialization issues.

To solve the circular dependency issue, consider the following strategies:

  1. Refactor to Remove Direct Dependencies: If possible, refactor your singletons so they do not directly depend on one another. You can do this by abstracting common functionalities to separate classes or by using interfaces that are implemented by the singletons.

  2. Lazy Initialization: Defer the initialization of the dependent singleton until it’s actually needed. This means that instead of initializing it in the constructor or awake function, you initialize it the first time it’s used.

  3. Manager as a Service Locator: Instead of having singletons depend on the AppManager, you can instead have the AppManager act as a service locator that other components can query to get the instances they need.

  4. Injection Over Direct Reference: Instead of singletons referencing each other, consider using dependency injection to inject the dependencies that are needed.

  5. Initialization Order: Ensure there is a clear initialization order. For example, the AppManager could have an initialization method that is called after all singletons have been created.

  6. Event-based Communication: Use events to communicate between singletons instead of direct method calls, which can reduce the coupling between them.

In the context of Needle Engine, ensure you are following the principles of component-based architecture and avoiding direct dependencies between your Behaviour scripts if possible.

thread_H07wgEL1vcln0lkXOtTD0M5d

Hi @Shmoji did the bot’s answer help you? I think he is right about circular dependencies. This has nothing to do with Unity but it’s caused by two files importing types from one another.

One way to overcome this is to only import the type via e.g. import type { MyClass } from "./somefile.js" - this works if you only use type information.

If you share more info about the files/imports then we’ll be able to provide better help :slightly_smiling_face: