How to keep track of held touches on a touchscreen device with multitouch

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

I want to be able to keep track of where the user has their finger held on the screen, separately for every touched point.

One way I can imagine doing this is to guess that a drag or release event corresponds to the touched point that is the closest to the event. I don’t really have any problem with this method, but it looks like the touchscreen-related input event classes have some multitouch info included as the “index” member. The docs aren’t very clear on the meaning of the variable though, so would it be possible to use it as a cleaner way of keeping track of touches?

:bust_in_silhouette: Reply From: Zylann

Similarly to this question How to detect a screen touch continuously ? - Archive - Godot Forum, I would handle touch events in _input and memorize info in some member variables, so you’ll be able to know at any time where touches are.
Also, touches can be identified with their index variable InputEventScreenTouch — Godot Engine (stable) documentation in English

So if I have a touch event and a release event with the same index, I will know that they correspond to each other?

raymoo | 2017-02-19 05:34

Yes, that’s what I understand from the doc. I never tested it though^^

Zylann | 2017-02-19 17:30

I don’t see anything in the docs that guarantees this. Did you get that from the docs page for InputEventScreenTouch?

raymoo | 2017-02-19 18:14

Yes. I’m forced to infer it indeed, because if it’s not that, what could index possibly be? It says “in case of multitouch event”, so I’m pretty sure the goal of it is to tell them apart.

Zylann | 2017-02-19 18:29

Yes, the index refers to the touch that produced the original pressed event.

I don’t know what happens if touch “0” releases after touch 1 then comes another touch (the new one will be “2” or “0”?), check that before making the structure that will keep your touch data.

eons | 2017-02-19 20:09

I’ll probably just use a Dict so it won’t matter whether indices are recycled, as long as I clear the entries for released touches.

raymoo | 2017-02-22 20:31