Game Programming Patterns in Godot: The Singleton Pattern (Godot Autoload)

preview_player
Показать описание


(Disclosure: As an Amazon Associate I earn from qualifying purchases.)

In this Godot tutorial, I will explain to you what is the Singleton Pattern and how can we leverage it via the Autoload feature in the Godot Game Engine. The Singleton Pattern can be used to have a unique instance of a given class, thus enabling you to keep the state of any component between scene changes. This pattern is implemented via Godot Autoload, which is the concrete way for the engine to deal with this game design pattern.

🔔 Subscribe:

Subscribe to my channel to see more videos like this:

📖 Useful resources:

- More information about Godot Autoload in the official Godot Game Engine documentation:
- Assets used in this tutorial:

🔵 Follow me:

Find out about my latest videos and publications:

⏳ Timecodes:

00:00 - Introduction on this Godot tutorial
01:43 - The Singleton Pattern
03:54 - Create a Godot Singleton with Godot Autoload
05:38 - Conclusion

---

Music credits:

Track: Alexi, Action, Infraction- Witch's Whispers
Music provided by Infraction No Copyright Music
Рекомендации по теме
Комментарии
Автор

Thank you for explaining this in a video, I was reading documentation and it just wasn't clicking and as soon as you demonstrated I was able to work it how I needed to. Very helpful!

SquibbyJ
Автор

I rarely comment and most of the time I am just a simple lurker. But your videos are amazing! Keep up the good work!

fudo
Автор

i was confused at what a "Singleton pattern" was, turns out I was using it in godot for my Global variables

ryoonk
Автор

First time ever I've heard someone use Phonk as their background music in a tutorial, and I'm mega vibing with it

myme
Автор

I do this as well on my game. Also using Core as main scene and then instantiate and queue free levels is good too. Its similar as well for performance

samuraikina
Автор

Happy New Year! Thanks for the new tut!

mbg
Автор

Thanks for the video David. I'm new developing games and I have been using the singleton pattern. Essentially for each object I create (player, levers, coins) I create a "controller" (singleton) script with essentially the variables and functions that I will need later for the actual scene script to consume. I'm not sure if what I'm doing is correct. I'm worried about later performance or code structure in general to turn into a mess. Any suggestions?

FernandoPujol
Автор

Technically script classes are resources which are in effect singletons. The Script Resource itself only has 1 version of itself in the project period.

So making a static instance of the scripts instance would store the instance in the resource at runtime.

Now why you would want to do this... to rage against the machine?

dibaterman
Автор

I noticed when using the autoload, if I call the script in a node within the game. Then it doesn't retain the singleton property of single instance.

Sirlegna
Автор

It means "One per program" just like the static keyword means "one per class" and it's strongly recommended that you avoid using this at all costs unless you can absolutely avoid it. It's meant for things like utility classes with functions like add_two_numbers(num1, num2) -> int because this doesn't store anything, it just performs a transformation on the data and returns it and it's deterministic so you get the same output for the same input every single time.

When you start tracking states improperly through singletons, you will have a lot of possible places that will alter the data you're trying to work with, leading to impossible to solve bugs, race conditions, objects getting queue_free()d and other nonsense you really don't want to deal with and don't have to if you just don't use singletons for things they aren't meant for.

henryjubeda
Автор

the singleton pattern sucks. any other design choice is better.

poleve
welcome to shbcf.ru