How do I add a stopwatch that will count up when the player is alive and reset when he died?

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

So, I have this game where you dodge droids and i need a timer to make something a person would want to play and improve on. I decided to add a stopwatch using a script inside a timer inside a player (so the timer will follow the player when you move) currently my script is:

extends Label
    var time_elapsed := 0.0
    var counter = 1
    func _process(delta: float) -> void:
    	time_elapsed += delta
    	$Label.text = str(counter)

I am very new to Godot so i kind a have a vague idea on what to do.
I would love it if you could leave a script down too (along with the explanation) because I’m a very visual person and explain can only go so far.

Godot Tutorial - Simple Health Timer +

Venex2004 | 2023-05-23 09:59

:bust_in_silhouette: Reply From: Tom Mertz

That is a great start, then you’d just tell the label to show the player the amount of time that has elapsed and have a method to reset it:

extends Label

var time_elapsed := 0.0
# You don't really need this
var counter = 1
var is_stopped := false

func _process(delta: float) -> void:
	if !is_stopped:
		time_elapsed += delta
		$Label.text = str(time_elapsed).pad_decimals(2)

func reset() -> void:
	# possibly save time_elapsed somewhere else before overriding it
	time_elapsed = 0.0
	is_stopped = false

func stop() -> void:
	is_stopped = true

You might also need to stop the timer from animating once your player does what you want them to. For docs on the pad_decimal check out this link. But basically, it just trims everything after two decimals, so 0.212341151 turns into 0.21 to keep the UI nice.

Hi again, so I was running your script, and everything seems good, but I get an error in the code of line $Label.text = str(time_elapsed).pad_decimals(2) the error says

Invalid set index ‘text’ (on: base 'null instance) with value type of ‘String’

I currently don’t know how it’s happening other than somethings wrong with the String statement maybe it’s from the str() or the $Label.text

I’m sorry for wasting your time.

TheBadDev | 2023-05-23 21:38

Oh, yeah, is your script on your label? The $ is a shortcut for the get_node method

So, it will search for a node named “Label” as a child of the node that the script is added to. If your script is added onto your Label (as it looks like by your extends Label at the top) you can access any of the Label’s properties or methods directly from the script.

So, in that case you’d just change it to text = str(time_elapsed).pad_decimals(2). In most cases if you see an error Invalid “something” (on: base null) you look for the “something” in your code and look at what’s ahead of the dot before it and that’s usually what’s null.

Tom Mertz | 2023-05-24 00:47

The new line of code worked, I’m so glad and now I can finally relax and not worry about the timer! Thank you soooo much! I wish you the best.

TheBadDev | 2023-05-24 12:43