Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Stopwatch node #93

Merged
merged 2 commits into from
Jun 22, 2021
Merged

Add Stopwatch node #93

merged 2 commits into from
Jun 22, 2021

Conversation

Xrayez
Copy link
Contributor

@Xrayez Xrayez commented Jun 20, 2021

Complements Timer node in Godot. Allows to measure time intervals.

image

Resolves godotengine/godot-proposals#2878.

When comparing Timer to Stopwatch:

  1. Does not have wait_time nor time_left properties, but time_elapsed.
  2. Does not have one_shot, does not make sense in Stopwatch.
  3. Does not have paused property. Intervals are measured via Stopwatch.start() and Stopwatch.stop(), yet Stopwatch.stop() does not reset Stopwatch.time_elapsed to zero. You have to use Stopwatch.reset() for this. This is because you can measure multiple intervals in succession. In essence, Stopwatch.stop() freezes the cumulative elapsed time value.
  4. Does not have timeout() signal, but interval_measured() (emitted when stop() is called). Can be useful to insert speedrun intervals to a scoreboard, even without having to reset the stopwatch (you can keep track of start and stop times provided by the signal parameters as well).

Example

extends Node

func _process(_delta):
	$Debug.text = str($Stopwatch.time_elapsed)

func _input(event):
	if event.is_action_pressed("ui_accept"):
		if $Stopwatch.is_stopped():
			$Stopwatch.start()
		else:
			$Stopwatch.stop()
	if event.is_action_pressed("ui_cancel"):
		if $Stopwatch.is_stopped():
			$Stopwatch.reset()

func _on_stopwatch_interval_measured(time_interval, time_start, time_stop):
	print("Interval: %s, start: %s, stop: %s" % [time_interval, time_start, time_stop])

Complements `Timer` node in Godot. Allows to measure time intervals.
Depending on the use case, you may use either `is_running()` or `is_stopped()` to check stopwatch state, this increases readability of the code that is going to use stopwatch functionality.

This also makes the doc generation in Godot properly group related methods.
@Xrayez Xrayez merged commit 156dc3b into gd3 Jun 22, 2021
@Xrayez Xrayez deleted the stopwatch branch June 22, 2021 13:31
@Xrayez Xrayez modified the milestones: gd3, 1.1-gd3 Jul 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 💡 New feature proposal topic:core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add stopwatch functionality to the Timer node
1 participant