Hello and welcome to another episode in the Godot Basics tutorial series, Godot Tutorials is not affiliated with or sponsored by Godot Game Engine. In this episode, we'll be taking a look at how to code for the particles to the node. Now, as you can see on screen, I have two note. The one on the right is a particle's tuti node with settings already established, and to the left you can see an empty node. However, it has a script attached to it that when we pressed the play button, we'll create the same exact fire as the one on the right.
So as you can see here, if we pressed the play button, you can see that there are two fires.
Now, keep in mind that even though there's a soft error next to the node with the script attached to it, we shouldn't be too concerned because when we run the game, we are going to produce code for the particles to denote to start running the fire. Now, let's go ahead and take a look at the script.
So the script is called Get Texture and it extends from particle's to D. We create a variable called particle material and it is of data type particles material, which is basically a resource class. Now, everything that's running for the particles to denote is all inside the ready virtual method.
We first initialize our setup. Basically, we set the texture and we set the process material, which is a particle material, and then we can go ahead and basically set up our particles to denote however we'd like. I've categorized them into two separate categories, one being very simple and the other being a little more complicated, especially the scale and colorant. Lastly, you'd like to emit turn your particles to the node on.
And lastly, I have something down here for you to see in the console so you can visualize how everything is sort of working underneath. Let's start with the initial setup. So in the initial setup, we set the texture and we have to pass it. Basically an image. In this case, it's going to be the white square image.
After that, we can set the process material and we're going to set it to the particle material variable, which is just a class instantiation of the particles material resource class. And lastly, we set the amount of particles we'd like in one second, and in this case I set it to one hundred.
Now, down here you can see the function in mint and all we do is we call the emitting property and we set that value to true and we do this last all the other functions.
Is everything happening between the initial setup method call and the Imit method call. So let's go ahead and take a look at that. So I have a function called Set Direction, and in this function, all we do is set the direction property to a vector three value. Notice the negative one point zero in the second argument. That means we would like our materials to go up and we set the spread property value to seven point seventy six. Now in these set gravity method, we set the gravity property to zero.
So in this case, our particles to the node has no gravity affecting the particles.
We set the initial velocity by calling the initial velocity property in the particle material variable and in this case we set it to four hundred and twenty seven. Next, we set the angular velocity and we call the angular velocity property and the particle material variable and we set that value to seven hundred and twenty. And on top of that, we do in fact add some randomness by calling the angular velocity random property and setting its value to want.
What this does is values between zero and 720 will be randomly applied to each individual particle. And lastly, we set the angle. Now in this case, we're talking about the starting angle. So in this case, we set the angle property in the particle material variable to three hundred and sixty.
Now, the difference between zero and three hundred sixty is nothing. It's basically in the same position.
However, we do in fact edit the angle, random property and the particle material variable, and we set that value to one. What that does is any angle between zero degrees and three hundred sixty degrees will be applied to each individual particle. And now we move on to something a little more complicated when we want to set our scale or colour ramp or basically anything that has to do with a curve texture or gradient texture. There are multiple steps we need to take in order to achieve the results we want when dealing with code in the particles to denote.
So first we call the set scale method and the order is important here. So first we set this scale for our particle material variable. In this case, we set the scale to zero point three and then next we set the scale curve. A curve texture data type. Now, in this case, we call this method called get curve texture, which will provide a certain curve texture data type. And so if we take a look here at the get curve texture method, we do, in fact return a curved texture.
And to create a curved texture or we have to do is called the curve texture class, followed by the new method. And we pass this value into a variable now or curve texture variable is of data type curve texture and the curve texture provides us a property called Curve the curve property takes any curve data type and so we get the curve data type through the get curve method.
And so if we take a look at the curve method, we do in fact return back a curve data type. And to create a curve data type, we just call the curve class, followed by the new method from this curve variable. We are able to add points through the add point method and it takes any vector to data type. So in this case, we start our curve starting at the position zero zero. We basically want to go up really quick.
So in this case, on the x axis, we're zero point one and on the Y axis we're basically zero point nine if we round it. And then lastly, we'd like to finish off basically a downward slope.
And so our last point is one on the x axis and zero on the Y axis. And then once we're done adding or points, we go ahead and return back the curve variable.
And so in our get curve method, we do in fact, return that curve variable, pass it along to the curve texture variables, curve property, and then we return back the curve texture over here. And so now our curve textured data type gets passed along and is set to the skill curve property of the particle material variable, which is in fact a vertical material resource class. And so I hope the explanation made it simple, especially if you're beginning.
But basically we do have to go and do quite a lot in order to set our scale with the appropriate curve for our fire node or in this case, our particle tuti system that's emitting fire.
And basically, if you got this down, then you'll get everything else because it's basically the same process. So let's take a look at the colorant.
So over here in the color room, we go through the same process. So basically you'll notice here we follow the hierarchy, we start at the top and we work our way down.
And so you'll notice the same pattern for setting the color. So first we call the set color ramp method. And in this method, we call our variable called particle material.
And we want to set its color ramp property. The color property on the particle material resource class does, in fact hold the variations of our color. In this case, we want to set different colors at different points in the particles life cycle in order to create that binary effect. And so what we do is we call the get gradient texture method, the get gradient texture method returns back a gradient texture data type. And to create a gradient textured data type we have to do is call the gradient texture class followed by the new method, and we pass that along to a variable called gradient texture.
Now the gradient texture variable class does in fact have the gradient property and this gradient property needs a gradient data type. And so we use the gradient method in order to get the gradient data type. And so if you look here at the get gradient method in order to create a gradient data table, you have to do is call the gradient class, followed by the new method, and we pass that along to the gradient variable or variable called gradient. Now it's from the gradient class that we are able to create our points along the x axis of the colour ramp.
And so notice here we use the add point method. The first argument takes in a float value between zero and one, and that represents where exactly on the gradient scale of our colour graph do we want to add a point to? And so it's always recommended that you start at zero. If you choose a number beyond zero, the default colour set to zero is black. And so if you don't overwrite zero, black will be applied. Let me show you. So I'm going to change this to to save press the button.
And you can notice here that we do have, in fact, a little black.
And so that's just something you have to keep in mind. So in this case, I start at zero for the first argument in the at point method and the second argument takes any colour data type.
So as you can see here are coloured data type takes in for arguments.
Each individual argument is a representation of the colour scale or Giba. Each argument can only take in a value between zero and one, and so I'm going to show you how to convert a value you can find online into a value that will be acceptable to the Godot game engine. So let's go ahead and do that. So over here, I'm on the website called Adobe Color. And so, as you can see here, we have the color white. And if we look at each individual value, the RGB a scale, you can see that the first three values are 255, the last value is one hundred.
And so in order to convert these into a decimal value between zero and one, all you have to do is divide the first three arguments by 255.
And the last argument by one hundred. And so, for example, if we wanted to get this red color here, as you can see here in the middle, or we have to do is divide 255 by 255, divide 24 by 255, divide zero by 255, and divide one hundred by one hundred in order to get our values in this case would be one about zero point one zero and one. Now, let's go ahead and look at the code we placed for our colorant. Now, the first three arguments are basically colors close to orange.
However, the second to less color is red. And so if you look here, the first argument is, in fact, one. The second argument is, in fact, close to zero. Point one, the third argument is zero and the last argument is one. And so that's how you can convert something online into something you can use in the color method. And of course, if you look here at the less color we have, white everything is 255, 255, 255 and one hundred.
And if you divide by 255, you get one one one one of the last argument being divided by one hundred, of course. Now, once you've added your points, you're going to want to return back the gradient variable, which is your gradient data type. And so we go through this chain again. So once you return gradient in the get gradient method, we assign the gradient variable to the gradient property method of the gradient texture class and we return back the gradient texture class and so the gradient texture gets assigned to the color realm of the particle material.
Lastly, we do call the Emit method, which again all we do is set omitting to true. Keep in mind that omitting is not part of the particle material node or class resource class, but rather it is part of the particles to denote. And so once we press play, you can see here in all its glory, we start from orangery, rich, red, and we end up with a white that's basically for coding. It's a little tedious. I do not recommend making particles to these systems through code.
However, we can use code to manipulate already established particle tuti nodes that are already on the scene trying. Well, that's all I have for you in this episode. I'm going to go ahead and upload everything to GitHub, so please feel free to play around with the particles to the node through code and of course through the Inspector Teb, because I have two particle nodes attached to the same tree.
Thank you so much for joining me. Thank you for clicking the subscribe button and thank you for clicking the like button.
If you have any questions or comments, please feel free to leave them in the comments section down below and they look forward to seeing you in the next episode. Have an amazing day.