Reusing Timer node, _timeout() not firing a second time?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CyborgNachte
:warning: Old Version Published before Godot 3 was released.

I’m making a mission system where the player clicks on a mission button that initiates a timer, that timer counts down and completes the mission.

When the mission is completed, the button is repopulated with a new mission that has a new mission time length.

I have a timer node set up that ideally I just reuse by reassigning it a new _set_wait_time() and two if statements in the button to determine what the button does based on if the mission is complete or not.

Heres my code:

extends Control
#Boolean to stop _ready from resetting missions
var first = false
#Boolean for resetting _on_button_press
var complete = false
#randomize ID
var id = -1
#node variables.
onready var button = get_node("Panel/Button")
onready var label = get_node("Panel/Label")
onready var missiontimer = get_node("missiontimer")

## DICTIONARIES START HERE##
var a_msn = {
	title = "Test Mission A",
	msn_length = 3
}

var b_msn = {
	title = "Test Mission B",
	msn_length = 5
}

var c_msn = {
	title = "Test Mission C",
	msn_length = 7
}
## ARRAYS START HERE ##
var test_array_a = [a_msn, b_msn, c_msn]

func _randomize_mission():
	randomize()
	return randi()%3
	
func _pop_msn():
	id = _randomize_mission()
	label.set_text(test_array_a[id].title)
	missiontimer.set_one_shot(true)
	missiontimer.set_wait_time(test_array_a[id].msn_length)
	#missiontimer.set_active(true)

func _ready():
	# populate missions for first time.
	if first == false:
		_pop_msn()
		first = true
	

#on press of button, find mission's length and set it to timer's length -> run timer.
func _on_Button_pressed():
	if complete == true:
		#reward window
		get_node("Panel/Button/WindowDialog").show()
		button.set_disabled(true)
		_pop_msn()
		button.set_text("Mission Start")
		##***note: put this disable flag in rewards popup later***##
		button.set_disabled(false)
		complete = false
		

	elif complete == false:
		missiontimer.start()
		button.set_disabled(true)
		print(complete)
		var time = missiontimer.get_time_left()
		print(time)
		var active = missiontimer.is_active()
		print(active)

func _on_missiontimer_timeout():
	button.set_text("complete")
	button.set_disabled(false)
	complete = true

What happens is that the mission works fine the first time, allows me to hit the the button to complete and repopulate it, but when I click ‘mission start’ for the second time, the timer never complete or possibly never starts and therefore never calls
_on_missiontimer_timeout() again and I can not figure out why.

I reconstructed your scene based on your code and it appears to work.

Control

  • missiontimer (connected _on_missiontimer_timeout)
  • Panel
    – Label
    – Button (connected _on_Button_pressed)

The only change to script, i commented out get_node("Panel/Button/WindowDialog").show()
Because i don’t know what WindowDialog contains or if it has its own script.
It may be the cause of problem.

I don’t know what you have in your scene beyond this, but stripped to the bare minimum it works for me, so the problem probably lies somewhere outside of that.

aozasori | 2017-09-29 20:20

The confusing issue is the is nothing outside of this. I have it stripped to the bare minimum as well.

Here is my scene, just to see that its set up exactly as you have done it:

The window dialog has a empty starter script that has no errors in and isn’t called by anything, its just a place holder. I commented it out just as you did and ran the game to see if it functions and I still run into the same issue.

initial load up:

clicked and waiting for completion:

Completed, clickable to repopulate:

New mission populated, ready to click Start Mission

Mission Start clicked, button deactivated, timer never runs out, button never becomes complete after this:

so just to clarify, you did in fact get the button to reach ‘complete’ a second time, using my code?

CyborgNachte | 2017-09-29 21:10

Hey, so I can confirm. Your code works fine for me too. Maybe post a minimum project to test.

indicainkwell | 2017-09-30 05:09

Yes, got to ‘complete’ a second time. (I tested up to 8 times with no issue.)

aozasori | 2017-09-30 21:46

After some trial and error, I recreated my entire code slowly from scratch in a new project and the problem ceased to exist. My code is identical in each, so it must be some strange or obscure user error of my own fault. (something checked in the gui? Or something leftover from a deleted node??)

Thank you for showing me that it does work, I was sitting there for days trying to figure out what bizarre logic error I was making.

CyborgNachte | 2017-10-04 04:26