Random beginner-question #10: Is my timer really stopping?

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

Hi everyone,

I built one of those buttons that trigger action#1 at a quick tap/click and trigger action#2 after holding down for some time. To achieve this, I set up two timers that start when holding down, a short one and a long one.

func _ready():
	timer_long = Timer.new()
	timer_long.connect("timeout", self, "_on_timer_timeout_long")
	timer_short = Timer.new()
	timer_short.connect("timeout", self, "_on_timer_timeout_short")

func _on_HoldButtonArea_input_event(_viewport, _event, _shape_idx):
	if Input.is_action_just_pressed("mousebuttonclick"):

So releasing before the short timer runs out triggers action#1, holding down until the long timer runs out triggers action#2.

Releasing in between the end of the short timer and the end of the long timer cancels everything. (Mouse-exiting cancels everything at any point, by the way.)

Among some variables there are:

var short_timer_out = false
var long_timer_out = false

func _on_timer_timeout_short():
	short_timer_out = true
	print ("Short-timer out!")

func _on_timer_timeout_long():
	long_timer_out = true
	print ("Long-timer out!")

The signals are being used for some separate animations to run above the button: A bar filling up from the moment when the short timer finished until the long timer finishes…
I use the variables for dealing with canceling things.

This all works wonderfully, but: only once! As soon as the short timer runs out once, the button does not work properly anymore (quick tap/click won’t register anymore etc.).
What I can observe is the cancel-animation that should play after the short timer runs out then also plays when canceling during short-timimg, and this makes me wonder if “timer_short.stop” actually really cancels the timer, so/and the timer_short.start() really starts at a reset 0 time. I have the impression that the counters might somehow be paused and then resumed instead, messing everything up… could that be the case? Is “timer_short.stop” not “definite” enough?

Any suggestions?

:bust_in_silhouette: Reply From: gioele

I believe you don’t need to stop timers in timeout callbacks.
If timeout signal is emitted they are already stopped. But I am not sure it will completely solve your problem, maybe try commenting those lines.

By the way, why all by code? Is it to display on this site? Because you can easily make those timer settings in GUI…

Thanks for your thoughts, gloele.

I startet these button-features by code with that simple _on_timer_timeout function and had no idea how complex things would grow afterwards… how would you suggest doing the settings in GUI? (I’ve been looking into the timer nodes but am not convinced yet.)

What I try to achieve (as a first step) is to “interrupt” a timer and reset it, so it will start at 0 again next time. And I wonder if timer.stop() actually does this properly.
(Not stopping the short timer in the _on_timer_timeout function makes it repeat over and over again while holding down the button.)

One weird thing is that finishing the long timer always triggers the action like it should. It’s only the short tapping that doesn’t work anymore after the short timer runs out once…

pferft | 2020-08-29 16:44