Hello and welcome to another episode in the Godot basics tutorial series. In
In this episode we will be taking a look at timer's. In Godot, We have two ways of creating a timer. The first way is through our own custom code. The second way is using those built in timer node. Let's go ahead and look at custom code first. So in this example we have a naive implementation of a countdown timer. I have our code inside the physics process virtual method because every 60 physics frames are delta value is able to compound to one.
And so we take advantage of that in this case counter timer. Plus equals our delta value. We
We know at some point counter timer will be equal to one. So we go ahead and create an if statement for that.
And when this becomes true we go ahead and we reduce our remaining time by 1. We set our counter timer to zero.
And our thing just keeps looping through and eventually we will reach zero. And so again this is a naive implementation of a countdown timer. And so in G.D. script it would look like this.
So first we have to declare a time counter and a starting value for our time because we're counting down in this case it's 10 and our time counter starts at zero.
We go ahead and we compound the delta value onto our time counter or delta value again as a constant value because we're inside the physics process virtual method.
In this case I'm casting our float time counter value into an integer and comparing that to one. And when it is in fact true we go ahead and run the code inside our if statement chain.
And so time left is reduced by one every second.
And of course we go ahead and we reset the time counter to zero so we can enter the if statement again and we go ahead and print up to the console so you can see that the naive timer custom code is working now just by looking at this you can see a few things One is that this is in fact a naive implementation because we don't do anything as a matter of fact our timer is going to continue counting down after zero. And so we're going to reach zero eventually we're going to hit negative 1 negative 2 and it's just going to keep reducing itself by 1 for every second in our game.
And so how do we do something about this. How do we actually make this timer custom code useful for us.
Well it's quite simple.
All we need to do is add more functionality for example perhaps you may want the ability to set a countdown timer starting value. It could be a method or done through the constructor. You may also want certain states for your custom timer code or rather class. For example you may want the ability to repeat the timer. You may want the ability to pause your timer and so forth. The most important thing is you would like to do something when your timer reaches zero.
However instead of making it more complicated for us we have the second option which is to use the timer node that Godot provides us as a matter of fact if we continue down the path of adding more functionality we're going to end up creating the timer node class. Let's go ahead and take a look at the timer No the timer node inherits from the node class which means that we have all the basic life cycles on top of that for the timer node to properly work our timer node needs to be added or already inside the same tree. Let's
Let's go ahead and look at some of the timer node properties you may find useful.
Those properties would be auto start paused one shut and wait time auto start one set to true just basically means that our time or node will start its countdown without us having to call the start method when our timer notice added onto the scene. True but by default this is set to false and if we keep it at false when added to the same tree we must call the start method that the timer node provides us if we would like to pause a timer node for any reason we can set the paused property to True the next property and the one I consider the most important property is the one shut property by default one shut property is set to false.
And what that basically means is that we're going to continue to loop our timer. And so if you've connected to the timeout signal you're going to continue calling it every time your counter timer reaches zero.
The last property is the wait time property by default it's set to 1.
However we can change this as well. And basically this is where our countdown timer starts. There are two methods that the timer node class has and that is these start and stop method.
The start method can take in a float value and that basically lets the timer know how many seconds you would like.
The countdown timer to have and of course if you would like to stop your countdown timer you have these stop method. What I think is the most important aspect of the timer node is the fact that we do have a signal called timeout in order to connect to this signal in code we need to use the Connect method followed by the string name of the signal in this case timeout the class we would like to use it in which is our cells so the self keyword. And then lastly the string value that represents the name of your method the timer node is actually quite simple to use.
First added to the same tree whether manually where you drag and drop your timer node on to the Godot application you why seeing tree or you can manually code that by using the ADD child within instantiated timer node object.
The next thing you want to do is connect your function call to the timeout signal method. And then lastly you want to call the timer node start method somewhere and just keep in mind by default when the timer reaches zero it will restart.
Back to the value you passed in inside the start method or the wait time value.
And so this is how you would use the timer node.
In this example we have a timer node attached to the same tree already with a script attached to it. That script is the following. First you want to make sure that your script extends from the Timer class. And unlike our previous example with the naive custom implementation which was inside the physics process virtual method.
Keep in mind that in this example we're inside the ready virtual method which will be called only once when added to the sanctuary.
In this case we set the wait time property of the Timer class to 3 and then we make sure we connect our signaling which is the timeout signal to the print Hello method.
And basically what we're going to do is when our timer reaches zero we're going to print to the console hello world and in order for our timer node to start we need to call these start method.
As you can see here the timer node abstracts the complexity of writing your own custom countdown timer class. Now when exactly do you want to create your own custom Timer class or script. Well when you want to do something other than counting down because the timer node can only count down it cannot count up the second time you want to use custom code is when you need functionality or something uniquely different that the timer node class cannot provide you.
When exactly do you use the timer node.
Well when you need the ability to count down the timer node makes this very simple for us because it abstracts a lot of the things we would need to implement ourselves if we were to do it through raw code. And when it comes to a countdown timer that may not be something to look forward to. I went ahead and added a get hub link to the sample timer project in the description down below so please feel free to download that and play around with both the raw code and the timer node class.
Well that's all I have for you in this episode. Thank you so much for joining me. Thank you for clicking the Like button. And thank you for clicking the Subscribe button. If you have any questions or comments please feel free to leave them in the comments section down below.
I look forward to seeing you and the next episode.
Have an amazing day.