How to detect swipe using 3.0

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DodoIta
:warning: Old Version Published before Godot 3 was released.

Hello folks,
I’m trying to detect a swipe gesture using Godot 3.0 alpha2, but I’ve got no clue where to start from. Searching for ‘swipe’ in the current docs gets no results and I’m so not familiar with this kind of input.
So, can anyone give me a lead? Where to start from? Or is there already something I can use?

I see there are some helpers on Github to detect swipe, such as this for Godot 2.x : GitHub - arypbatista/godot-swipe-detector: Detect gestures and swipes in your game
There shouldn’t be much differences about this in Godot 3.0, some functions are renamed and look different but the functionnality is the same. Also remember it’s still alpha^^

Zylann | 2017-11-03 01:08

I actually know the existence of swipe detector, and I tried using that before asking this question.
I downloaded it and opened in 2.1.4, then used the “export to 3.0” tool. Of course I knew this was probably not working, so I opened the exported version with 3.0 and tried fixing every error, which I couldn’t do because there are some built-in scripts I couldn’t edit if some scenes were not loaded.
So I isolated the library by deleting the examples folder and only keeping the addons one, then I created another scene with just a Node2D to test it, I instanced SwipeDetector.tscn as a child of my new scene, created a script and connected a function to the signals to get the input points, but nothing.
The engine gives me no error, but no output either! So it’s pretty much not working and I’ve got no clue on how to fix it :frowning:

DodoIta | 2017-11-03 11:17

:bust_in_silhouette: Reply From: Lisandro Lorea

I have this in an autoload:

signal swipe
var swipe_start = null
var minimum_drag = 100

func _unhandled_input(event):
	if event.is_action_pressed("click"):
		swipe_start = get_global_mouse_pos()
	if event.is_action_released("click"):
		_calculate_swipe(get_global_mouse_pos())
		
func _calculate_swipe(swipe_end):
	if swipe_start == null: 
		return
	var swipe = swipe_end - swipe_start
	print(swipe)
	if abs(swipe.x) > minimum_drag:
		if swipe.x > 0:
			emit_signal("swipe", "right")
		else:
			emit_signal("swipe", "left")

It just emits a signal with one argument that can be “right” or “left”. If you don’t know how to start maybe this helps.
Basically, if you don’t care about the whole curve, your swipe is swipe_end - swipe_start where swipe_start and swipe_end are the return value of get_global_mouse_pos() when the user touches the screen and when she lifts her finger, respectively.

So simple and so perfect. That’s exactly what I needed to get started, thank you soooooo much!

Also, can I ask why using _unhandled_input instead of _input?

DodoIta | 2017-11-03 17:22

Perfect, thank you.

DodoIta | 2017-11-03 17:38

Just to add there, but that handling of the input could just be over an appropriate control node.

I was wondering though, because godot already has specific types of InputEvent with mouse motion and swipe motion, I would like to see them in use.

Guilherme Furst | 2018-01-31 15:54