HOW CAN I MAKE A STOPWATCH WITH A LABEL?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ManiCus

HOW CAN I MAKE A STOPWATCH WITH A LABEL?

Please don’t write in all-uppercase; it’s irritating to read.

Calinou | 2020-04-22 14:58

:bust_in_silhouette: Reply From: jgodfrey

I guess it depends on the functionality you want in your stopwatch, but here’s a very simple example that simply accumulates the delta value that’s passed into the _process function when the “stopwatch” is running…

To run this, you’ll need a new scene with the following content:

Control
    ButtonStart
    ButtonStop
    Label

Now, add this script to the root (Control) node:

extends Control

var running = false;
var elapsed = 0;

func _process(delta):
	if (running):
		elapsed += delta;
	$Label.text = "%0.3f" % elapsed

func _on_ButtonStart_pressed():
	running = true;

func _on_ButtonStop_pressed():
	running = false;

Finally, connect both of the button’s pressed signals to the two signal handlers above (_on_ButonStop_pressed() and _on_ButtonStart_pressed()).

Additionally, you could add one more button to Reset the timer. Just add another button and wire its pressed event to this script addition (added to the same script above).

func _on_ButtonReset_pressed():
	elapsed = 0;

jgodfrey | 2020-04-19 15:09