How can I avoid using Singletons?


I would like to know how can I avoid singletons as much as possible. I don’t want my game to get real messy and have spaghetti code all over once the game gets bigger. Is there any better alternatives?

Thank you

can you remind me of what singletons are…

singletons are basically global variables that every script can access it, which can get really messy once your project gets bigger and bigger. It’s a pretty controversial topic.

They’re not really that bad of a practice, especially in a high level language like Lua. I’m not sure how it can cause spaghetti code either. There are plenty of useful scenarios for singletons. All of the services, for example, are singletons. Using them doesn’t make your code messy.

That’s assuming, of course, that all of your code is dependent on it. You should only require the singleton module in scripts that use it.