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:
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.
Code: Select all
local player1, player2 function love.load() player1 = {joystick={ID = nil, GUID = nil}} player2 = {joystick={ID = nil, GUID = nil}} end
- 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