How do you add a score that works on a timer

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

I’ve been developing a game that needs a score that updates every second. I fairly new and have seen the timer node which counts down.

:bust_in_silhouette: Reply From: jgodfrey

There are a number of ways to do this. Here’s the basics with a Timer.

  • Add a Timer to a sample scene
  • In the inspector, set the timer’s wait_time = 1, autostart = true, and one_shot = false
  • Connect its timeout signal to a script containing something like:

.

var score = 0
func _on_Timer_timeout() -> void:
	score += 1
	print(score)

So, each time the timer “times out” (after 1 second here), it’ll call the _on_Timer_timeout() function, which will increment the score. Once that happens, the Timer will automaticlally reset to 1 second and being counting down again, repeating the process.

As another option (not using a Timer) you could just manage this by hand, via the _process() function. Something like:

var time_out = 1
var time = time_out
func _process(delta: float) -> void:
	if time > 0:
		time -= delta
	else:
		time = time_out
		score += 1
		print(score)

jgodfrey | 2023-01-27 03:50

I was having trouble with making it print the new score, but all you need todo is change print(score) to text = str(score)

SloshySpace | 2023-01-29 03:59