Let’s say we had an object
PupperManager with the goal of managing all of our game’s
Puppers. Since it doesn’t make much sense for more than one thing to manage a group of objects like this,
PupperManager is a great candidate for the singleton pattern. You can look up a really computer-sciencey explanation for Singletons, but primarily our goal here is to create an object that fits the following requirements:
- There’s only one single instance
- It is globally accessible
So let’s start with writing
Create event, this will implement a basic singleton pattern.
Pretty simple, right? I’ll explain some key take aways here:
By using the
global.PupperManagerto noone before anything could possible use it. If I hadn’t done this, then the
instance_existcheck would look more like :1if (variable_global_exists("PupperMananger") && instance_exists(global.PupperManager))
Not the worst, but still, an unnecessary look-up, and this also makes it slightly more dangerous to use our helper macro.
- We destroy any instances created if an instance already exists. It’s important to understand this behavior, if you rely on any helper create functions, they may need to ensure they’re returning valid instances… though, if you always access via the macro you’ll be safe.
- We made a macro - by making the
gPupperManagermacro, anywhere we want to use the PuppyManager we can use
with(gPuppyManager)and have no risk of error - however, if the singleton hasn’t been created, this is the same as
Before we go - it’s important to also Clean Up after yourself:
Reset the global handle, that way if your singleton isn’t persistant, it won’t block new ones from being generated.
And that’s all there is to it! Let me know in the comments if you’ve got any questions, or if anything can be explained more clearly. Stay tuned for more GMS 2 Tips!