How to measure reaction time between circle appearing and mouse click in Godot 4?

Godot Version

Godot 4

Question

Hi everyone,

I’m a beginner making a simple clicking game in Godot 4. A circle appears at a random position on screen, and when the player clicks it, it jumps to a new random position. The game has a 30-second countdown and counts how many circles the player clicks.

I would like to also measure the (reaction time) – meaning the time between the circle appearing at its new position and the moment the player clicks it – and display it on screen.

Here is my current circle script (attached to an Area2D):

extends Area2D

func _ready():
    random_position()

func random_position():
    var screen_size = get_viewport_rect().size
    var x = randf_range(0, screen_size.x)
    var y = randf_range(0, screen_size.y)
    position = Vector2(x, y)

func _input_event(_viewport, event, _shape_idx):
    if event is InputEventMouseButton and event.pressed:
        if get_parent().time_left > 0 and not get_parent().game_over:
            get_parent().add_point()
            random_position()

I think I need to save a start_time using Time.get_ticks_msec() when the circle appears, and then calculate the difference when the player clicks. But I’m not sure where exactly to store the start time and how to display it correctly.

Any help is appreciated, thank you!

You can simply create a variable in your Area2D (or wherever you’d like to keep track of these times) that you update first.
As soon as your circle appears, update that variable and set it equal to the result of Time.get_ticks_msec, then later, when the player clicks on the circle, you can store the result in, say, an array of numbers.
The result, by the way, can be calculated by subtracting the time you get back when the click happens from the time you recorded when the circle appeared.
Storing the values in an array means you’ll easily be able to look all the reaction times back and even be able to calculate an average if you want to.