How to get the time between a button appearing and the button being pressed?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Cakee

I currently have a button that appears and I would like to get the time between when it appears and when the user presses it. I would then like this time to be displayed through any type of text box if that is possible.

:bust_in_silhouette: Reply From: jgodfrey

So, it sounds like you already have the button appearing. In that case…

Define a global variable to store the time the button appears:

var appear_time

At the point where the button appears, record the current time the variable. There are lots ways to do that, but here’s one…

appear_time = OS.get_ticks_msec()

Then, wire the button’s pressed() event to a handler in your script. In that handler, just calculate a delta time between the time you recorded earlier and now. Then, put that value in your some label ($Label here)…

var elapsed_time = OS.get_ticks_msec() - appear_time
$Label.text = String(elapsed_time / 1000.0)

I ended up getting an error with the $Lable.textsaying that $ should not be used. Is there another way to edit the text?

Cakee | 2020-09-07 23:25

In the example, I had a label node named Label as a direct child of my scene’s root node.

That $Label references that node. If your label is named something else or is at a different location in the scene tree, you’ll have to change the reference to match your scene.

Otherwise, the code works.

jgodfrey | 2020-09-08 00:01