 This video was brought to you by my patrons. Thank you so much for your support. Hello there! In this video we will talk about the factory method, a design pattern that tries to facilitate instantiation of new objects. But before we actually dive into this pattern, I want to talk about something with you. So let me switch here. So this is my analytics for the last 90 days. What I want to talk about here is that I had 6000 unique viewers that on average watched 2.2 videos. But only 459 subscribers in the last 90 days. What I want to talk here is that I want to make a deal with you. There is a new viewer and it's not subscribed yet. Subscribe now to the channel so that I know that I reached someone that is actually interested in the content that I am making. And at any point of the video you can unsubscribe so that I know that the way that I presented the content is not valuable to you. But if you do like the content, so if you watch it, if you are interested in this kind of content and you watch the whole video, I ask you to turn on the notifications so you can receive more of this content so I can provide more value to you. This is the deal that I want to make with you. So let's go back to the design pattern. So I have here the 2 books that I am using for reference in this series. And the factory method is described as following. It is a creation of a design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of the objects that will be created. And the intent of this design pattern is to define a virtual constructor or to define an interface for creating an object. Also, the new keyword can be harmful. So the factory method tries to work around this new keyword necessity. So let's go to Godot Engine and I have prepared here something that I... Let's try to remember what I am trying to do here. This is not a tutorial. I am sharing my studies with you. So the way that I understood this design pattern and you can for sure correct me in the comments below if you find out that maybe I understood this the wrong way. But the way that I understood it is that we will try to provide an interface for instantiation of some classes. And then we can inherit this interface because we don't have actual interface in Godot Engine. We will be working with concrete classes directly. But then we can override, we can use polymorphism to work with this method in different ways depending on what we are instantiating. So here I created a basic spawner because I think that this is a good use case for this pattern. This spawner has a spawn method. It can spawn objects. And in this method it creates an instance of a spawner. A spawner is a packet scene. So in Godot Engine we have a packet scene which are basically saved scene trees. We can save the structure of a scene tree here in Godot Engine. And when we save it to the disk it becomes a packet scene. And then when we instantiate this packet scene, Godot will parse all this information on the packet scene and create the structure that we need. So when we spawn here we will create an instance of this spawner and set its global position to be the global position of this spawner class. Because it is a spawner2d class it inherits from node2d. So it has a position, it has a rotation in space. And we will use this position to set the position of the new instance. And when we instantiate this we emit a signal telling that this was instantiated and we also returned this instance from this method. So why this? Any class can then connect this signal or get the return of this function and work on this instance from the other class. So let's say we have a structure where we connect this spawned signal which passes a spawned instance. We connect this signal to the add child method. So instantly when this spawn is created it is added to the node that is connected to the signal. So now we can extend the functionalities of this class. So I will create a new inherited scene here which will inherit from spawner. And this inherited scene will be a timet spawner. So it will have a timer node. And what I will do here since the spawner class, this timet spawner inherits from the spawner I know that I only have to call the spawn method. So I will connect the timeout method of this timer here to the spawn method of the timet spawner. So it is connected already. And from time to time I can set this here out of start for instance. And I can say that from 3 to 3 seconds it will create a new instance. So it's a timet spawner. So let me save this. And we can specialize this class to create a specific type of object. So I have here a flying body. So it's a comet which I use in Mochises. So basically what this comet does is that we can drag it and it will fly towards the direction that we drag. But as you can see it doesn't fly automatically. Because what we have to do to make it fly is to have something that we call a fly method inside of it. Passing a vector 2 that will be the direction of the fly. So if we use this vector 2 that is the direction so it's a normalized vector. And it will multiply by the velocity of this object. So if I connect it here you can see that we have a flying speed and have the fly method with a direction. So when we fly we will set the velocity to be the flying speed multiplied by the direction. And then since it inherits from this it will move at this velocity per second. So if we start it here it will fly because I connected the ready method to this fly method here. Passing a vector pointing to the right direction. So that's what I made here as you can see. But I don't want this to be made inside of this comet. As you can see I used the ready method because when it's ready it will fly towards a direction. But I don't want this to be made inside of it. I want that an outside class will tell which direction it should fly towards. So I want this to be made on its spawner because the ready method is when the class is ready. So it's kind of like after the construction of this class, of this object, of this node. So I will try to do the same but using a treatment using a spawner. So I will create here a new inherited scene that will inherit this timet spawner. And it will be a flying body timet spawner. Save. I will extend this script here. Create. And everything that I will do here is that I will export a vector to variable. That will be the fly direction. And by default it will point to the right. And then what I will make is that I will use polymorphism to overwrite the parent class method. So that when we instantiate it, it will point towards this direction that we have here. So we will call the fly method passing this direction. So function spawn. Oops, I'm using Portuguese keyboard here. So what we will do is that we will have a flying body variable. That will be the return of the upper class method. So we will tell it to spawn. And since we return this instance, we can work on it here. This will be the instance that we just created. Now we can say that the flying body will fly towards this fly direction. And then we return this flying body. So now from every class outside of this hierarchy, both this class will work in the same ways. Because they have the same method for instantiating. So if I go here and I create some kind of playground level or something. Let me save this here. I will have here two. Actually I will implement the three spawners here. So I will have a simple spawner. I will have a timetable spawner and I will have a flying spawner. Let me change this. So the timetable spawner will be here. The flying body spawner will be here. And the spawner will be here. So what I will do here is that I will pass a scene to this. And I will call these spawn methods on each one of these. So I will create a simple scene here. Just so we can have something to work with. So let me change this to a sprite. So sprite. I will save this here. And I will use the Godot texture side of this. Save, okay. These classes here, the spawner and the timetable spawner, will instantiate this sprite here. Remember that since this sprite is on the 00 position, when we instantiate it, it should be here. So we won't see it. But since we are using a method to spawn them, that we are setting the spawning, we are encapsulating the instantiation of these classes inside of this spawn method. When we instantiate it, we will set the global position to be the global position of the spawn class. So I will connect the spawned signal to this node at child method. And the same goes for this at child. And the same goes for this as well at child. Save. Well, we need to make this automatic. So, oh, it's auto-start already. So we will start to see that this won't instantiate. So I will actually connect the ready signal here to this spawner spawn method. So we can have at least one instance of this sprite that we made here. So if we try this. Okay, so the flying body spawner, timetable spawner doesn't have something to work with, which I will set it now. And what you see is that this will fly towards this direction. This will keep here, but we will have some instance of a sprite here and we'll have only one instance here. So let me save this. Actually, I will use the commit sprite here so that we have some uniform sprites there. So I will actually leave it on the horizontal direction. So let's try this out. We have one. After three seconds, we will have this and then this. And after three seconds, we will have another one of these and it will keep spawning this. We also have some of these here, but we can see because it's just a sprite overlapping. But yeah, these spawners are working as we designed them to work. This is how I understood the factory method pattern. It's a way to make some treatments or to work on an instantiation of an object and to create an interface for other objects to instantiate these objects that we want, but without having to do these procedures. So we encapsulate these procedures inside of a single instantiation method. And this is it. Let me remember you about her deal. If you watch it and if you are interested in this content and you like it the way that I presented it to you, turn on the notifications so you can get more of this content. So I can provide more value to you. Also, don't forget to leave a thumbs up if you actually enjoyed the content or actually a thumbs down if you don't like it. So I know that you don't like the way that I presented it to you. And don't forget to leave comments on the way that I presented, the way that you understood. If I say something wrong as well, please let me know. I'm really confused about this design pattern. I didn't get it well, to be honest. So this is it. Thank you so much for watching. Keep developing and until the next time.