I am trying to make a timer that counts how long you hold mouse button 1. I am currently just trying to send that number to console with a message, but my code doesn't seem to be working. I would appreciate
some help, thanks.
Programming Help: Timer
- Edited
It's not clear from the posted code snippet how often timebarcreate() gets called.
If it's called only once then things obviously cannot work as you need to check the button status every frame to catch if it's been pressed or released.
On the other hand if it's called each frame, then things still cannot work because you check if action is released only if action is at the same time pressed. This can never happen.
Although what you want could be achieved by polling the Input singleton, it's much more convenient to use _input() event handler as it will be called only when input events actually happen. Inside the handler you need to determine the type of the event and act accordingly:
extends Popup
var time_start: float
func _input(event):
if event is InputEventAction:
if event.action == "CreateTimerBar":
if event.pressed:
time_start = OS.get_unix_time()
else:
print(OS.get_unix_time() - time_start)
I put what you did, but I can't seem to get it to print and I just can't figure out why
Is the action "CreateTimerBar" created in the project input settings and set to the correct key?
Is your "CreateTimerBar" action properly defined?
Actually, it seems like Popup nodes do not receive input events. I'm looking into it.
I did a bit of researching and I got to this code which IS returning a number to console, but it's only returning 0 right now
That is because the first if is getting called every frame, so it will have the same number (since the last press and release will be almost at the same time). You could use is_action_just_pressed instead and it should work.
- Edited
Seems the problem is with if event is InputEventAction:
Try:
extends Popup
var time_start: float
func _input(event):
if event.is_action("CreateTimerBar"):
if event.pressed:
time_start = OS.get_unix_time()
else:
print(OS.get_unix_time() - time_start)
Your last example with cybereality's correnction should work too but it executes more code than needed. As I said earlier, polling needs to run every frame while _input() handler runs only when input happens. Since you just need to measure the time between two events (as opposed to check button status each frame) it's more appropriate to use the event handler.
Also note that get_unix_time() can only return full seconds. For precise measurement you'd probably want to use get_system_time_msecs().
Working just as intended thank you guys! If at all possible is there a way to make it go to a couple decimal places because currently it's only whole numbers?
- Edited
Here, this will work.
extends Popup
var time_start = 0.0
func _input(event):
if event.is_action_pressed("CreateTimerBar"):
time_start = OS.get_system_time_msecs()
elif event.is_action_released("CreateTimerBar"):
var seconds = (OS.get_system_time_msecs() - time_start) / 1000.0
print("Seconds passed: " + str(seconds))
TYSM you're awesome