How to make a loop which adds 0.5 to a var each time interval while a button is held down?

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

Like, every second it adds 0.5 to var numb, when the button is no longer pressed, loop breaks and var numb becomes 0?

:bust_in_silhouette: Reply From: alexandremassaro

Add a button and a timer to your scene, connect the button_down and button_up signals of the button and the timeout signal of the timer, then use the following code:

var numb = 0.0

onready var timer = get_node("Timer")

func _on_Timer_timeout():
    numb += 0.5

func _on_Button_button_down():
    timer.start()

func _on_Button_button_up():
    numb = 0.0
    timer.stop()