Since Godot’s Timer node only counts down, you may want to have some sort of stopwatch to help record your timing in a racing game or speedrun.
This Project’s Difficulty level is: 1
Nodes You’ll want:
- Label
So far as I’m aware, the Timer node uses the delta (amount of time between each frame) variable to calculate time even through lag, that’s what we’ll want to use.
To be able to change the value from the editor, add export before the var to be able to set that. You can set a hint by prefacing var with (type). Where type can be a datatype, resource type, or enumeration.
I’ve set an exported variable to allow you to set the ‘step’ of the estimation. Otherwise it defaults to 0.01. We want to control the length of our stopwatch.
To see the code execute in the editor, just add tool to before the rest of the script. In this case, we can see the counter go up endlessly
Steps
TBA
Script
tool
extends Label
export (String) var title
export (float) var round_to
var counting = true
var count = 0
func _ready():
if title == ”:
title = ‘time’
if round_to == 0.0:
round_to = 0.01
func _process(delta):
if counting:
count += delta
set_text(‘%s: %%s’ % title % str(stepify(count,round_to)))
func _unhandled_input(event):
if event.is_action_released(“ui_accept”):
counting = !counting
Something like that.
Optional:
You may notice that as of right now the watch only counts seconds. I may want to help you with that but this is something to start with.