How do you use the observer pattern?

Godot Version

4.4

Question

Let’s say you have 3 scenes, namely, Main scene, Write Quiz scene, and Answer Quiz scene.

The Main scene is just the “main” scene to handle scenes and codes. The Write Quiz scene lets you write your question data and write your answer data. The Answer Quiz scene lets you answer those questions. There will only be one quiz shown in a screen, by the way.

The question is, how do you make those 3 scenes communicate with each other without using singletons but using the observer pattern?

This sounds like a homework question :rofl:

Anyway, the godot docs say this about signals:

Signals are a Callback mechanism. They also fill the role of Observers, a common programming pattern. For more information, read the Observer tutorial in the Game Programming Patterns ebook.

So you can make your own custom signals for the quiz stuff and connect the signals to other nodes.

1 Like

I’m actually trying to make a quiz game of some sort where the user writes the questions and answers. Then there is a different scene for the quiz screen. It sort of like a challenge to myself of using Control nodes since I’ve always used Node2D.

I’ve been using singletons for a while now and I’ve read in the docs that there are going to be some problems if you use it extensively. Like bugs are harder to find and whatnot.

And I’ve experienced that in a 2D game that I’ve tried to clone. Now everything right there is hard to track. :sweat_smile:

And so I realized, I need to learn more about this Observer Pattern. I watched some YT videos and they are understandable. Using signals, not in a singleton way, is one of the approach given, which is what you said, the Observer Pattern.

I intend to understand it more by asking a question here at the forum to really learn the basics of Godot Engine. Thank you for your help.

Sounds like you need a database shared between the “Writing a Quiz” and “Answering a Quiz” scenes, this isn’t a use case for observers, singletons may be good, or reading/writing files.

Signals are best suited to “fire and forget” situations, but writing a quiz isn’t very “forget”. If you use signals send the written quiz to the answering game then you could never edit the quiz again, not without sending it back, then you have more state and databases to manage and sync up between the two rather than a centralized database through a singleton or file.

1 Like

Wow. I did not realize that. I’m still a beginner with Game Development, by the way.

So, one should really use a singleton for this. Call that singleton “quiz_data” and have the scenes access it all the time. Thank you for your help.

It’s all tools, use the right one for the right situation. Singletons often seem like a holy grail when starting a project, but overuse will quickly make the project hard to work in. That’s why they are avised against so heavily.

1 Like

Yes, I actually thought about that. I said to myself that I do not need anything anymore because of this singleton. But experiencing the problem it gives on the 2D game I made back then gave me a realization that something is wrong. That things should be more organized. But I didn’t know that in the quiz game I’m making that singleton is actually the one I need this time.

Thank you again.