Stopwatch

Inherits: Node < Object

A stopwatch to measure time intervals.

Description

A stopwatch allows to measure time intervals. It can measure elapsed time for one interval, or the total of elapsed time across multiple time intervals. To start measuring a time interval, call start, then eventually call stop, and then check time_elapsed.

extends Node

func _ready():
    var stopwatch = Stopwatch.new()
    add_child(stopwatch)
    stopwatch.start()
    yield(get_tree().create_timer(1))
    stopwatch.stop()
    print(stopwatch.time_elapsed) # Prints 1.0

Measurement can be resumed with successive start calls. Each call to stop ends the current interval measurement and freezes the cumulative elapsed time value, which also emits the interval_measured signal. Use method reset to clear the elapsed time to zero in order to measure new time intervals.

Since the class is used to measure time intervals between existing events of interest, the stopwatch does not have a notion of maximum elapsed time, nor has a timeout signal. Use Timer node for this purpose instead.

Methods

bool is_running ( ) const
bool is_stopped ( ) const
void reset ( )
void start ( )
void stop ( )

Signals

  • interval_measured ( float time_interval, float time_start, float time_stop )

Emitted when the stopwatch stops the measurement of a time interval. The time_interval is the difference between elapsed times between start and stop calls. The signal also provides the start and stop times via time_start and time_stop respectively.

If the stopwatch is started and then immediately stopped, the signal is not emitted.

Enumerations

enum ProcessMode:

  • PROCESS_PHYSICS = 0 — Update the stopwatch during the physics step at each frame (fixed framerate processing).
  • PROCESS_IDLE = 1 — Update the stopwatch during the idle time at each frame.

Property Descriptions

Default false
Setter set_autostart(value)
Getter has_autostart()

If true, the stopwatch will automatically start when entering the SceneTree.

Note: This property is automatically set to false after the stopwatch enters the SceneTree and starts.


Default 1
Setter set_process_mode(value)
Getter get_process_mode()

Processing mode. See ProcessMode.


Getter get_time_elapsed()

The cumulative measured time interval. Returns 0.0 if the stopwatch has not been started yet with start or reset to zero with reset.

Method Descriptions

  • bool is_running ( ) const

Returns true if the stopwatch is in the process of measuring a time interval. The opposite of is_stopped.


  • bool is_stopped ( ) const

Returns true if the stopwatch is inactive (or hasn’t been started yet). The opposite of is_running.


  • void reset ( )

Stops time interval measurement and resets the time_elapsed to zero. The stopwatch cannot be reset if it’s already running. In that case, call stop before resetting the stopwatch.


  • void start ( )

Starts (or resumes) measuring elapsed time for an interval.


  • void stop ( )

Stops measuring elapsed time for an interval. Emits the interval_measured. You can reset the stopwatch to zero by calling reset manually.