How would you add a countdown counter to gui?

#1

What ideas do you have to add a countdown timer to the tutorial? Main.gui?

0 Likes

Text nodes: only displays 1 character, twitches (SOLVED)
#2

Could you explain more of what you want to do? Have you already read the manuals?

0 Likes

#3

So you want some kind of countdown timer ticking down once per second or something? One option would be to use the update(self, dt) function the gui script attached to your gui and simply decrease a timer variable and update a GUI text node:

function init(self)
	self.timer = 100 -- in seconds
end

function update(self, dt)
	if self.timer > 0 then
		self.timer = math.max(self.timer - dt, 0) -- make sure it doesn't go below 0
		gui.set_text(gui.get_node("my_text_node"), tostring(self.timer))
	end
end

Other options would be:

  • Use gui.animate() to animate “position.z” or some other unlikely value (since z is ignored on guis) every seconds to get callbacks and in each callback decrease the timer and update the node
  • Create a timer game object that you can spawn using a factory (so you can have multiple timers) and let that game object have a script with a go.property("timer", 100) and go.animate() that property from it’s initial value to 0 and read it while it’s updating and post that value to the gui script
  • Have a timer Lua module where you can create timers and have an update() function that you can call on the created timers to decrease their value and read and set the value of the timer in your gui script.

Worth noting is that we have also discussed if we should create some kind of built in timer functionality to the engine where you can delay a function call by a certain time. This kind of functionality could be used to create the kind of timer you’re looking for.

9 Likes

#4

HI Mr.Britzl
What a bout show timer countdown in label.
Or not necessary to add label to show timer.
Thanks

0 Likes

#5

Untested code:

local function countdown(label_url, seconds)
	-- start a repeating timer with a 1 second interval
	timer.delay(1, true, function(self, handle, time_elapsed)
			-- decrease seconds and update label
			seconds = seconds - 1
			label.set_text(label_url, tostring(seconds))
			-- cancel timer when countdown reaches 0
			if seconds == 0 then
				timer.cancel(handle)
			end
	end)
end

function init(self)
	countdown("#mylabel", 10)
end
1 Like

#6

Thanks…

0 Likes