Detect Hold Time in Toucscreen

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

Hello,
I have touchscreen for my game so I am using InputEventScreenTouch function in my code.
I have Collisionpolygon2D area in my game and I add the input_event signal which name is
_on_20_Points_input_event for this area. I need to detect this area I want to detect that this area is pressed for 2 seconds. How can I do this ?
I detect pressed with below function but how can I detect the holding ?

func _on_20_Points_input_event(viewport, event, shape_idx):
    if (event is InputEventScreenTouch && event.pressed ):  # I want to detect press hold in 2 sec. How can I do this ?
:bust_in_silhouette: Reply From: Moreus

https://docs.godotengine.org/en/stable/classes/class_time.html
get_ticks_msec ( ) get_ticks_usec ( )

when input is first time pressed you save time, and check if actual time is bigger more than 2 sec.

your you just add delta :wink:

Moreus | 2023-03-12 18:44

But how can I detect still pressed my main problem is this. If the touch position is change than _input_event working but if there is same point I dont have any trigger to test. How can I manage this ?

if (event is InputEventScreenTouch && event.pressed): this only work once. After 2 sec how can I detect still pressed ?

I am trying like this :

func _on_20_Points_input_event(viewport, event, shape_idx):
	if (event is InputEventScreenTouch && event.pressed):
		print("Here")
		print(event.position)
		if get_event_position == event.position:
			print("After timer:",event.position)
			GlobalVar.points += 20
			refresh_points()
			ani.play("Points_added")
			position_list.append(get_event_position)
			my_timer.stop()
		else:
			if !(event.position in position_list):	
				get_event_position = event.position
				print("before timer:", get_event_position)
				my_timer.connect("timeout", self, "_on_20_Points_input_event",[viewport, event, shape_idx])
				my_timer.start()

I think this is not good method.
I need to find I still in this area without relased because when I pressed I will not relase.

duyci | 2023-03-12 20:07