Countdown timer

To create and display a simple countdown timer you could adapt this code.

This implementation assumes the object holding the text for the timer is on a Gui in a text node called, 'timer_text'.

function init(self)
	self.timer = 10 -- seconds
end

function update(self, dt)
	if self.timer > 0 then
		self.timer = math.max(self.timer - dt, 0)
		gui.set_text(gui.get_node("timer_text"), string.format("%.1f",tostring(self.timer)))
	end
end
What does this code do?

timer is an attribute that holds the current time on the timer in seconds.

If the timer is greater than zero it retuns the greater value between the timer and the number zero. This ensures that -0 is not shown when the timer reaches zero.

The timer is cast to a string. The Gui text node is set to a formatted string of the timer to one decimal place.

% is the integer part of the timer. 1f means one decimal place. You can change this to 2f etc.