A utility stopwatch node for Godot 4.x
stopwatch-test.mp4
This plugin encapsulates the functionality of a stopwatch, offering advanced features beyond basic time measurement. It accurately calculates the duration between activation and deactivation, providing the ability to pause the stopwatch and reset the timer as needed. Additionally, the plugin allows users to retrieve the current time in a formatted string, facilitating seamless integration with text nodes for easy display.
The video above is one of the demonstration scenes, showing what can be done with this plugin, and also a demonstration on how you can use this plugin effectively. You can find this demonstration in the "examples" folder of the project.
Go to the download section of this github repository and download the lastest version of the plugin.
Once downloaded, extract the contents of the zip into the addons folder. Your project directory should look something like this:
Your Project
addons
stopwatch
...plugin files
...other plugins
...other files
In the future, I will make a video demonstration on how to use this plugin. for now, here is the documentation that already exist in the code.
Obs.: You can access this documentation, by clicking the stopwatch logo with right-click on mouse button.
- float elapsed_time [padrão: 0.0] [propriedade: setter, getter]
- bool paused [padrão: true] [propriedade: setter, getter]
- Array[Array] checkpoints [propriedade: setter, getter]
- String process_callback [padrão: "Idle"]
- bool autostart [padrão: false]
- bool pause_on_reset [padrão: false]
- Array add_checkpoint()
- Dictionary get_elapsed_time_as_dictionary()
- String get_elapsed_time_as_formatted_string(format: String)
- static String get_time_as_formatted_string(time_in_seconds: float, format: String)
- static Dictionary get_time_dictionary_from_seconds(total_time_in_seconds: float)
- void reset()
- void toggle_pause()
Emitted when a new checkpoint is added, returns the elapsed time when the checkpoint is requested. and the difference between this checkpoint and the previous one
Emitted when the pause status is changed, returns if the stopwatch is currently paused or unpaused.
Emitted when the stopwatch is requested to reset, returns the elapsed time before it is resetted.
Determines weather the processing of elapsed time is calculated during process(_process) or during physics frames(_physics_process) Obs.: It only takes effect when the node first enters the SceneTree.
Determines if the Stopwatch start after the node is ready or not.
Determines if the Stopwatch pauses when is resetted.
amount of seconds elapsed since start.
Determines if the execution of the stopwatch is paused or not.
An array containing all the times a checkpoint was requested. The time is kept within an Array with two positions, the first one is the elapsed time when the checkpoint was requested, and the second position contains the difference between the current checkpoint and the previous one.
Add a checkpoint to the stopwatch based on its elapsed time, returns an array with the current elapsed time, and the difference between the current elapsed time and the last checkpoint.
Note : this function does not take into account if the stopwatch is paused or not, so keep this in mind when invoking this function.
Get the elapsed time since the stopwatch's start time and return a dictionary representing the time in hours, minutes, seconds, and milliseconds.
Formats the current elapsed time value into a string based on the specified format. the "format" is a string containing placeholders for day, hour, minute, second, and millisecond.
Placeholders: "{dd}" for day, "{hh}" for hour, "{MM}" for minutes, "{ss}" for seconds, "{mmm}" for milliseconds.
Returns: String: The formatted time string based on the provided format.
@export var stopwatch : Stopwatch
...
var time: float = 1234.567 [br] [br]
var formatted_time_str: String = stopwatch.format_time("{dd}:{hh}:{MM}:{ss}:{mmm}")[br]
print("Formatted Time:", formatted_time_str)[br]This example will output a string representing the time as "00:00:20:34:567", where:
- 00 days,
- 00 hours,
- 20 minutes,
- 34 seconds,
- 567 milliseconds.
Note: There is a static version of this function called Stopwatch.get_time_as_formatted_string()
This function is a static version of the function Stopwatch.get_elapsed_time_as_formatted_string(). It works in the same way, with the only differece is that here you have to provide the parameter time_in_seconds
Convert a time in seconds as a dictionary representing the time in hours, minutes, seconds, and milliseconds. "hours" : stores the hours "minutes" : stores the minutes "seconds" : stores the seconds "milliseconds" : stores the milliseconds
Reset the current elapsed time.
Toggle the stopwatch to be paused or not, depending on previous state.
This project is licensed under the MIT license.
