Singletons? Why? Why not?


Singletons, is a programming design pattern used to make sure there is only one instance of something at a time. The single existence of something in code, allows everything else to hook into it and use it to perform operations. Notably they're used on the most significant points where there is a lot of back and forth operations.  

As an example, we can take the Player. The Player can have a movement script, cameras, abilities attached to it. Now the neat part is that because these scripts are attached to the Player and if there is a Singleton Player, an external script access data inside of all different scripts using the Player as a Data Member. You dont have to find or search everywhere to find it, you can directly find and access it if it exists in the scene. 

Notable places where Singletons are most efficient are:

  • SceneController
  • AudioController
  • GameManager

On contrary to the above given example, you would not necessarily use a Player as a Singleton Class, its always a good idea to make a Singleton out of something that we must always need ONE of. The more efficient way to structure it would be to add the Player as a data member inside GameManager which is a Singleton. 

They sound so great, don't they?

Well it can get complicated soon, if you decide to cache everything inside one Singleton, you will run into issues when you want to isolate parts of the system to solve a bug, then realizing everything is inside the Singleton and you cant run without it. Its important to make sure Singletons exist for systems which are important but can also exist so they can be reused level to level. 

Singletons are poggers, until you don't make a mess of them.

Technical Gibberish brought to you by - Divesh Jagger

Get Heist against Heretics

Leave a comment

Log in with itch.io to leave a comment.