Incorrect/Inconsistent touch index handle among functions

Create issue
Issue #100 closed
Juno Nguyen created an issue

love.touch.getTouch() and love.touchpressed()/love.touchreleased() are currently inconsistent in the way they handle touch ID.

love.touchpressed() and love.touchreleased() start their touch index count from 0, while love.touch.getTouch() starts from 1. To elaborate:

love.touchpressed(2) will return data for touchIndex == 2, or while two fingers are touching the screen. Similarly, love.touchpressed(1) will return data for the (only) single touch.

However, this is how love callback functions work:

function love.touchpressed(id, x, y)
    if id == 0 then  -- single touch
        doSingleTouchStuff()
    elseif id == 1 then -- multi touch, a second finger starts touching
        doMultiTouchStuff()
    end
end

While this is easy to fix, it is causing frustration for those who are not aware of it, especially when documentation is still early and rough.

Expected result:

function love.touchpressed(id, x, y)
    if id == 0 then
        returnError() -- invalid touch index, similarly to love.touch.getTouch(0)
    elseif id == 1 then -- one touch
        doSingleTouchStuff()
    elseif id == 2 then -- two touches
        doMultiTouchStuff()
    end
end

Alternate option would be to consistently starts touch count from 0 (which personally makes less sense to me), but the key point is to maintain consistence across functions.

Comments (9)

  1. Alex Szpakowski

    love.touch.getTouch takes an index as an argument and returns an id (different from an index.) love.touchpressed, love.touchmoved, and love.touchreleased have an id as their first argument.

    The id is just a number to identify a particular touch press for its lifetime – it's not guaranteed to be any particular number or come in any particular order. Any consistency with them is just an implementation detail or a coincidence (e.g. the id's in iOS are usually some large positive number.)

    Here's some code to demonstrate:

    local activetouches = {}
    
    function love.touchpressed(id, x, y)
        activetouches[id] = true
    end
    
    function love.touchreleased(id, x, y)
        activetouches[id] = nil
    end
    
    function love.update(dt)
        for index = 1, love.touch.getTouchCount() do
            local id, x, y = love.touch.getTouch(index)
            -- note that the id is a return value from love.touch.getTouch, not the index used to get the return value.
            assert(activetouches[id] ~= nil)
        end
    end
    

    Here's some example code for doing something when multiple fingers touch the screen:

    function love.touchpressed(id, x, y)
        if love.touch.getTouchCount() == 2 then
            -- the second finger of two has just touched the screen.
        end
    end
    
  2. Juno Nguyen reporter
    function love.touchpressed(id, x, y)
        if love.touch.getTouchCount() == 2 then
            -- the second finger of two has just touched the screen.
        end
    end
    

    if this is how it is meant to work, then the perhaps the parameter id in love.touchpressed(id, x, y) is not useful as it seems, isn't it?

    Nevertheless, if this is confirmed "not a bug", do let me know, as the wiki will need to be noted accordingly.

  3. Alex Szpakowski

    Nevertheless, if this is confirmed "not a bug", do let me know, as the wiki will need to be noted accordingly.

    The wiki already mentioned it, but I just edited it now to be a bit more clear.

    if this is how it is meant to work, then the perhaps the argument id in love.touchpressed(id, x, y) is not useful as it seems, isn't it?

    What kind of use do you want that you feel is missing? If you think it should be an index rather than a generic identifier, what should happen in this situation?

    • First finger presses the screen

    • Second finger presses the screen

    • First finger stops pressing the screen

    • Index of second finger is...?

    • Another finger starts pressing the screen

    • Index of new finger is...?

  4. Juno Nguyen reporter

    I get the point. I was frustrated to wrangled a bit with id == 0. Will update the wiki to highlight this.

  5. Juno Nguyen reporter

    @MartinFelis id is probably fine, I probably just have not adapted to thinking the that way. The main problem is that the wiki did not highlight this well enough.

    But I do think id should not be the first parameters, as it breaks the usual convention when we work with love.mousepressed() and love.mousereleased(). And as mentioned above, since there is another way of detecting touch count, it is most likely less important than x and y.

    love.touchpressed(x, y, id) would make more sense to me.

  6. Martin Felis repo owner

    Yes, that makes sense IMO. If it gets adopted in the mobile-common branch it will be available here as well.

  7. Juno Nguyen reporter

    Touch id and touch index are different entities and work differently. The issue has been noted in related articles of the wiki and now closed.

  8. Log in to comment