Two X360 pads assigned same ID (not GUID) if re-plugged -- help?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
benreed
Prole
Posts: 6
Joined: Fri Oct 21, 2016 3:07 pm

Two X360 pads assigned same ID (not GUID) if re-plugged -- help?

Post by benreed »

Hey guys,

I decided to spend today working on what I thought would be a simple problem. (Apparently I was wrong.) I wanted to see if I knew how to basically account for unplugging/replugging game controllers, succesfully identifying the exact same physical device, and logically assigning information about those devices to two tables representing two players.

The problem I'm having is that for two of the controllers I'm debugging with, I expected to be able to use Joystick:getID() to figure out whether I'm looking at two different physical instances of the same physical type of controller. (I'm using the returned id value and not the returned instanceid value because the docs suggest that the ID of a physically unique device should never change over the lifetime of the game.)

The two different Xbox 360 controllers I'm debugging with are given different IDs if they're both connected at the same time, but if I unplug one and plug in the other, the game retrieves the same ID for both of them, so I can't just match the ID value from the function call against what I put in the player table and conclude "okay, this is the exact same device as before".

I've also tested a modded joystick that registers itself as an Xbox 360 controller (I think, need to verify GUID), but I haven't had any problems using Joystick:getID() to recognize that device as a completely different physical instance and type from the 360 controllers.

Here's how I've modeled things logically so far:
  • In love.load(), I initialize my player tables like so:

    Code: Select all

    local player1, player2
    
    function love.load()
      player1 = {joystick={ID = nil, GUID = nil}}
      player2 = {joystick={ID = nil, GUID = nil}}
    end
    
    As their names suggest, I'm going to assign the values of the joystick table members based on what I get back from Joystick:getID() and Joystick:getGUID(). I'm not actually using the GUID for anything right now, I just have a field defined in case I do need that information later on.
  • In love.joystickadded(), I inventory the connected joysticks (I know there will be at least one), compare the ID values I retrieve in that call against the ID values in my tables, and try to figure out that way if these are the first controllers ever connected, if I've connected that exact same controller before, etc. Here's what I have so far:

    Code: Select all

    function love.joystickadded()
        local joystick_count = love.joystick.getJoystickCount()
        local joysticks = love.joystick.getJoysticks()
        
        -- If P1's ID is nil, this is the first ever call to joystickadded(),
        --  so I'll give P1 the very first joystick ID (maybe the only one)
        --  I can find
        if player1.joystick.ID == nil then
          player1.joystick.ID = joysticks[1]:getID()
          
        -- I can also conclude here that P2's ID is also nil, so if
        --  there's more than 1 joystick connected, I'll assign
        --  the second ID I find to P2
        if joystick_count > 1 then
          player2.joystick.ID = joysticks[2]:getID()
        end
      
      -- If P1's ID is not nil, I conclude that joystickadded()
      --  happened at least once before  
      else
        -- Look thru all connected joysticks and compare IDs
        --   against the player table values
        for i, joystick in ipairs(joysticks) do
          print("Joystick "..i.." ID: "..joystick:getID())
          if joystick:getID() == player1.joystick.ID then
            print("P1: That's my joystick!")
          else
            print("P1: That's not my joystick.")
          end
        end
      end
    end
    
So I'm not sure what I'm logically doing wrong here. I know I can't easily overcome this problem just by compound-keying (in traditional database sense) the player's joystick with ID & GUID, since two 360 controllers have the same GUID by definition, so having the same ID will still screw things up. And I don't think the instanceid value can help me out, either, since I think that will undermine my ability to recognize the same controller re-connecting. Can you guys see what I'm missing?
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Two X360 pads assigned same ID (not GUID) if re-plugged -- help?

Post by zorg »

In short, this is your issue:
benreed wrote:The two different Xbox 360 controllers I'm debugging with are given different IDs if they're both connected at the same time, but if I unplug one and plug in the other, the game retrieves the same ID for both of them, so I can't just match the ID value from the function call against what I put in the player table and conclude "okay, this is the exact same device as before".
Or in other words, the first parameter of [wiki]Joystick:getID[/wiki], that should return an unique identifier for both your controllers (of the same type) doesn't, as it can't tell the difference between the two, after unplugging and reconnecting, correct?
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
benreed
Prole
Posts: 6
Joined: Fri Oct 21, 2016 3:07 pm

Re: Two X360 pads assigned same ID (not GUID) if re-plugged -- help?

Post by benreed »

Yes, that's it. Sorry for being so long-winded; I just wanted to try and spell out everything I thought of/did previously to solve the problem. I'm trying to solve my problems myself and I actually value plain-English correction (or suggested further reading) more than code solutions. I just wanna make it plain to people that I don't want handouts and I'm really trying to solve this problem on my own, as much as I possibly can.

And having said that, I might have spotted the problem, based on what you elbowed me in the ribs about: getID() returns two values, maybe I got the wrong return value. I assumed that they were returned in the order the docs list them, which was id and then instance, but either I was incorrect about the order, or my single variable wasn't enough to catch the one I wanted. Maybe adding a dummy variable to catch the other returned value will let me figure out which one I really need? I'll try it now and edit this post if I figure it out.

Bear in mind that I'm not new to programming, but very new to Lua. The main scripting language I have experience with is Python, which is far more structured. I respect the amount of freedom Lua's object model (or relative lack thereof) gives to the Lua programmer, but having so few "rules" about what I can and can't do with tables is honestly challenging for me. Makes it harder for me to visualize stuff. I'm getting better at it, but it's probably the most challenging language I've ever worked with that wasn't named C++. :P

EDIT 2: No, select() alone wasn't enough. I actually looked at both the return values (there ARE just two, right? That's what the docs say, anyway), spit the first one into "foo", the second one into "bar", then did my plugging/unplugging. "foo" was always "1" for both controllers, while the instanceid constantly increased by 1 every time I alternated which controller was plugged in.

So I'm still not able to find enough information to differentiate the two controllers using either/both return values of Joystick:getID(). Not sure where to go from here, but I'll keep Googling, I guess.
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Two X360 pads assigned same ID (not GUID) if re-plugged -- help?

Post by zorg »

Well, in that case, it should mean that the order of the returned id-s is correct, and there's some other issue.
Edit: Found this just now: https://www.reddit.com/r/love2d/comment ... d_same_id/
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 4 guests