Making a countdown time readout in Godot 4.1
Timers are a common sight in games nowadays and an important part of many games. In Godot, we use nodes to represent features, and in order to create a timer, we use the timer nodes. So, let us look at the timer nodes in a bit of detail.
Timer node
The timer node is a built-in feature in Godot that counts down a specified interval and emits a signal on reaching 0. This node has certain properties and methods that make it a flexible feature and effective in game development.
Properties and methods
The following image shows the properties and methods that are in the timer node:
Let us look at the following properties and methods in a bit more detail:
autostart: This is a boolean property, which, iftrue, automatically starts the timer as the scene starts.one_shot: This is a boolean property, which, iftrue, will stop the timer after playing it once. If it isfalse, then it will restart.paused: This is a boolean property, which, iftrue, will pause the timer. The timer will not process until it is unpaused again.time_left: This is a float property, which stores the remaining time in seconds. It will return0if the timer is inactive.wait_time: This is a float property, which sets the wait time in seconds. By default, the value is1.0.start: This is a method which has a floattime_secas a parameter. This parameter starts the timer from the time provided.
Note: The
startmethod will not resume a paused timer.
stop: This is a method that stops the timer.timeout: This is a signal which is used to trigger theon_timeoutfunction. This signal is sent when the timer reaches0.
Implementation
To make a resettable timeout reader in Godot, we used the following nodes:
Timer: This node is used to implement the countdown timer. In the Godot inspector, we check the
autostartandone_shotproperty.TextureButton: This node is used to implement the resettable button, which resets the timer.
Labels: They are used to show textual information.
Following is the script that we attach to our main object:
#default parent class for objects in Godotextends Node2D#This function is called every frame of the game, when it is runningfunc _process(delta):#This sets the text of the timer countdown label$TimerCountdown.text = "%s" % roundf($Timer.time_left)#signal function which is called when timer timeoutsfunc _on_timer_timeout():#Changes the text of message underneath the button$Message.text = "Timer ran out"#This is a signal function, which is triggered when the button is clickedfunc _on_texture_button_pressed():#Restarts timer and updates the message label$Timer.start(10)$Message.text = "Timer resetted"
Code explanation
Let us look at the explanation for the code snippet above:
Line 2: The
Node2Dis the default parent class in Godot for every 2D node.Lines 4–6: In
_process(delta)function, we are using the Timer node’s text property, which we have namedTimerCountdown, and we will update it accordingly with each frame.Lines 8–10: The
_on_timer_timeout()is a signal function in Godot that is triggered when the timer node timeouts. After timing out, it will change the initial message from “Timer started” to “Timer ran out.”Lines 12–15: The
_on_texture_button_pressed()is also a signal function in Godot, which is triggered when we click the Reset button in the game. This resets the timer node and gives an updated message.
Note: By clicking the “Run” button the game will be executed. To view the game in the browser, click the application link. This will open the application in a new window.
import React from 'react';
require('./style.css');
import ReactDOM from 'react-dom';
import App from './app.js';
ReactDOM.render(
<App />,
document.getElementById('root')
);
Free Resources