 This video was brought to you by my patrons, thank you so much for your support. The bread and butter of digital games is the ability to dynamically place objects into the game screen. For instance, it's different from a tabletop game where you have to stop the match, place the object into the board, and then resume the match again. From the grinding of an RPG to spawning bullets in an FPS, making things appear in the game world is essential to have a living and emergent gameplay. And in this video we are going to see how we can dynamically add these objects into our game world using what is known as a spawning system. One of the most common things we do when we are developing not just games but most type of applications is to create objects in runtime. This process is known as distanciation and is the basics of object oriented programming for instance. An object is an instance of a class. But when we talk about adding objects or entities into the game world itself like into a house, into a dungeon, into a level, we usually use the term spawning, like these types of enemies spawn on this level, on this area, or we should spawn an NPC on this dungeon after the play completes the quest. In that sense a spawner is an object that spawns things in the game world. The spawning process is like the distanciation, but it doesn't end on the creation of the object because after we create the object we have to treat it to fit it into the game world, giving it a position for instance. In this treatment we can do all sorts of stuff to this new created object. But let's understand the very basics of a spawner and then we'll see some different versions and some other features a spawner can have. The implementation of a spawner is quite similar to the abstract factory pattern. It is an object that creates another object and provides this product to other classes. This object should appear somewhere into the game world and needs a reference position. Usually especially with basic spawners we use the very global position of the spawner itself. That's why I use a position 2D as the root node for my spawners. When implementing a spawner you should be aware of its interface because it will be used by other classes. So it should have a reference to the object it will spawn, a method that other classes can use that will return this object after it creates this object and optionally a signal that will not file when this object is created. In GradEngine we can export a packet scene to use as reference of the object we are going to spawn. This will allow us to change this packet scene, this reference, the reference of the spawning through the inspector, and even animate it. Using animations to change this spawning reference is how implemented form collects event system for instance. In the spawning process we can use the packet scene instance method to parse the packet scene as a node. Since this method returns the node we just created we can store this node as a variable and start to treat it to insert it into the game world. I usually add a spawning as a child of the spawner itself so that it appears based on the spawner position, but this leads to a small issue. As sometimes the spawner will move or even rotate, this movement can affect the spawning behavior. For instance, I abstract guns as bullet spawners and these guns are often attached to an object that moves and rotates, such as the player's character. If we add the bullets as children of the gun, as the player moves and rotates and even jumps, the trajectory of the bullets will be messed up. To avoid that we can use the set a stop level method on any canvas art such as node 2Ds or controls. With that our spawning won't be affected by the spawner's transform such as its position or its rotation, but that also means that the spawners won't appear at the spawners position by default, so we have to mainly assign the spawner's global position to the spawning. After that we can notify that a spawning was just created and we can also return it so that client classes can use it as well. And with that we have a basic spawner, but the magic of spawning doesn't come from the basics. Now that we have the ability to dynamically create objects into the game world, we can start to be creative. So for instance, what if we want to create these objects within a given radius around the spawner? So here I have the spawner that we just created, let's create a new inherited scene based on this spawner and let's call it Radial Spawner because we are going to spawn things around the radius, right? And we are also going to extend this script, so the spawner script, let's create it, save this scene as well. And the first thing that we are going to do is to export a variable that's going to be the spawning range or radius, which by default will be 100 pixels. And what we are going to do here is to override the parent class, the spawner, spawn method. With that we can keep the interface so that client classes can consider all spawners as a single spawn, so they will be considered all then as being the same thing. So if someone calls the spawn method on the spawner and the spawn method on the Radial Spawner, they can consider both as being the same thing. The first thing that we are going to do here is to make a super call to call the parent class spawn method. We can do that by pre-painting a dot before we actually call the spawn method. This will make so that we are going to execute this method here all again. And since this returns the spawning that we just created, going back to the Radial Spawner, we can store that on a spawning variable. Now we cap all the procedures that we made in the basic spawner, and now we can start to treat this new spawning to make all the different behavior that we are going to implement. The first thing that we are going to make here, the next thing that we are going to make actually, is to create a offset, a Radial Offset that's going to displace the position of this new spawning around the spawner. So let's create a Radial Offset variable. And it's going to be a vector 2 based on the vector 2 dot right. This is a unit vector that's pointing to the right. We are going to use this one because a vector pointing to the right in got it engine means that it is a vector with a zero rotation. And why that is important is because we are going to rotate it by a random range between zero and tau. Tau is basically a 360 rotation, right? So between zero and tau. So let's make this a range, a random range actually, range, range. And now that we have a direction in which we are going to spawn an object, we now need the actual range, the radius in which we are going to spawn it. So for that, we are going to multiply this Radial Offset, this Radial Offset which by now is just a normalized vector, it is just a direction, we are going to rotate it using the spawning radius. So we are going to use a random radius between zero and the spawning radius. So Radial Offset is going to be multiplied by a random range between zero and the spawning radius. And after that, we can offset the spawning global position by this Radial Offset. So spawning dot global position plus equal to Radial Offset. And we must not forget that this new implementation of the spawn method should also return the spawning so that client classes can have access to it. And if we test this now, let's reset this to the enemy scene. Let's increase this spawning radius to something around 500 and test this scene now. Oh, of course, we didn't call the spawning yet. So in the ready callback, we are going to call spawn just so we can test this. And there we have it. But the magic doesn't stop there, we can go even more creative. For instance, what if we want to spawn objects at a random time basis? We can create a random timer that takes some random time intervals and connect its time out signal to the spawners spawn method. Or we can use animations and using the call method track, we can make some calls to the spawn method and create these objects based on the animation itself. So comment below how we are going to use this new skill in your games. Oh, don't forget that patronage supports all the videos that I do here on YouTube. So become a patron right now to get access to exclusive content, such as the project that I used in this video. That's it, thank you so much for watching, keep developing and until the next time.