 This video was brought to you by my patrons, thank you so much for your support! Hello there, following the series where I'm studying object-oriented design, so I'd like to remember that this is not a tutorial, now we'll start to talk about something that I like a lot, there is design patterns, but design patterns are not specific algorithms, you not just copy-paste a design pattern into your software and into your game. The concept behind a design pattern is that they propose a solution and you is the one that should apply it, should implement it. So in this series we'll see how these patterns can be or are already implemented in Godot Engine, and in this video we'll talk about the observer pattern. So let's get started! A brief definition of the observer pattern is that it is a behavioral design pattern that lets you define a subscription mechanism to not find multiple objects about any events that happen to the object they are observing, and it intends to define a one-to-many dependency between objects so that when an object stage changes, all its dependents are notified and updated automatically. So what's the problem that the observer pattern tries to solve? Let's say we have an interface and this interface has a button and a click sound, that is this sound effect here. So we want that whenever the button is pressed we want the click sound to play. One way that you can achieve that is that you can create a script and let's create this, and in the process callback you can check if the button is pressed. And then if it is pressed you can play this sound, right? This will work, if we play this, ok? But note something, we are always checking this in the process callback so every tick in the game, so it can be 60 times per second, you will be checking that. Think about it, if you have like an event of something like a collision, let's say you have an error 2D and you want to communicate about the collisions that happen in this error 2D, you have a lot of objects that want to get notified about this event. So if you try to check on every tick for a change in the state of the error 2D, you will lose a lot of process because a lot of objects will be checking for the condition for the change in the state of a single object. And also note that we are hard-coping these objects and they shouldn't be coped up because they are basically siblings. So a sibling shouldn't know about its siblings, right? This is not a very good practice. So what the observer patterns propose instead? The propose that we instead have a subscription system that whenever an object is interested in the state, in a change in the state of another object, we can use this subscription system to subscribe this observer to a list of observer and update them whenever we change the object state. And thanks to Godot developers, we already have this subscription system implemented in Godot. So if I go here in the help, search help, and if I go here in the object, so every node, every resource and reference, everything that inherits from object will have this implemented already, this subscription system, the observer pattern implemented already. If I go here in the object and go down to here, here we have the connect, the disconnect and the emit signal methods. So the connect method asks for a signal that should be connected to a target at a given method. The target here is the actual observer. So this is the subscription system that the observer pattern proposes. When we connect a signal, the signal will be the notification that will communicate a change in an object state to an observer at a given method. So the subject is aware of the observer because it has to know about a method that has to exist in the observer that is the target here. So this method has to be implemented in the observer. So the subject is coupled in some sense because it has to know about the implementation of the observer. It has to know that the observer has a given method because otherwise it will throw an error. And the disconnect method basically just disconnect is the opposite of the connection method. So we will unsubscribe a signal from a target that will be the observer at a given method. So we can unsubscribe a signal from an observer at a given method and we can even keep the signal at this observer but in a different method. We are just disconnecting it at a very specific method here. And last we have this emit signal method that will just ask for a string that will be the notification that we want to communicate and we can pass some arguments here. But remember that the observer pattern tries to define a one-to-many relationship between a subject and many observers. But here in Godot we have both the subject and the observer merged together in the object class. So an object here in Godot can be both an observer and a subject. So let's see how we can use this observer pattern here in Godot Engine. Well there are two ways to use it. One is using the editor interface and another is using script. So let's see how we can use the editor interface first. So I will go here in the 2D workspace and I will go actually in the script workspace and I will take rid of this so we have a clean script, save it and I will go back to the workspace and if I click here in the button also you'll notice that the interface is kind of different because I'm using the Godot 3.1 Alpha 5 so if you want to follow up but this is the same in every version of Godot Engine okay so you can follow up in every version. So if I click here in the button node here in the specter tab we have both the specter tab and the node tab so I'll click in the node tab and here we have a list of signals that this node can emit so these are the notifications that this node can use and here we have the bottom up or the precede notification but let's try to use the bottom up notification so this is the bottom up signal and here we have at the bottom left the connect button so let's click on it and now we can define who will listen to this notification so this is an interface of the subscription of a observer of a listener to the signal so this is an interface of the observer pattern that we have here in Godot Engine the listener the observer that we want to subscribe to the bottom up notification is the click sound and notice that we have here a make function this will make sure that the method that we are creating here will actually be implemented in the observer that we are subscribing so this will make sure that this will actually work so we won't have an error if we use this make function so make sure to have this turned on and I will go here in the connect method so this method will be created in the click sound but this will only happen if your observer has a script if it doesn't have one you will have to use the default methods such as we can use this play and this won't create another method but let's try to use the on bottom up method here in the click sound and I will go here in the connect and here we have it we already have the observer pattern implemented for us so if I want this click sound to play whenever the bottom is up I can just call the play method here so if we test this it already works so this is using the actor interface is the safer way you can achieve this observer pattern but let's try to see how we can use the scripts to achieve the same behavior to achieve this observer pattern so I will go here in the bottom note and in the note tab in signals I will disconnect this signal from the click sound at this on bottom up method so I'll disconnect this and notice that we are basically using the connect and disconnect methods from the object API so when we click here we are using the disconnect method so disconnect this just make sure that when we play the game it won't be connected due to the editor interface but due to the script okay so I will add a script here to the bottom and yeah button.gd I'll take rid of this and in the ready callback I will connect the bottom up signal to the get node we'll go to the parent and get the click sound and you can see that this is already dangerous because if we change the path so if we change the sin tree this will already throw an error because we won't be able to find this click sound node here so by choosing to use the script you have to be very careful about how you are implementing this observer pattern okay and we will connect this notification the bottom up notification to the on bottom up method of this click sound node okay and here we have it we already have this connected so if we play this and play this it is working so this is how you can use this observer pattern using a script and this is it if you have some doubts or if I say it's something wrong and you want to correct me please leave a comment below I'll be more than happy to help and to get some feedback from you if you like this kind of content don't forget to leave a thumbs up this is one way that you can say to me that I am on the right direction into creating content for you and don't forget that YouTube has an observer pattern implemented so if you want to be notified when I publish content hit the subscribe button below and turn on the notifications by clicking on the bell by the way if you like this kind of content if you like what I'm doing here you can support my work by becoming one of my patrons and this is it I hope you enjoy it thank you so much for watching keep developing and until the next time