gamepads || joysticks - how to tell what is plugged in

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
zell2002
Citizen
Posts: 75
Joined: Sun Feb 23, 2014 9:22 pm

gamepads || joysticks - how to tell what is plugged in

Post by zell2002 »

Hi,
I've looked through the joystick API, but I'm a bit confused about how I go about doing this.
I have an xbox 360 controlled plugged in, a ps2 (with usb converter) plugged in, and a ps3 controller
obviously they all show different button ids for each button press (the ps2 right stick on the Y is reversed too :3 )

what is the best practise for syncing all the controllers with the correct button mappings ? I assume it has something to do with this :
http://love2d.org/wiki/love.joystick.setGamepadMapping

But I really dont get it atm lol
Any pointers/advice would be greatly appreciated !
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: gamepads || joysticks - how to tell what is plugged in

Post by micha »

SDL2 contains a database of common gamepads and controllers. If your controller is in this database, then you can avoid using the button numbers altogether and directly use the GamepadButton-labels. See here: isGamepadDown.

To get started, first check, if your controller is recognized as a gamepad:

Code: Select all

if yourJoystick:isGamepad() then
  print('Recognized as a gamepad')
else
  print('not recognized as a gamepad')
end
If so, then you should be able to use the isGamepadDown function:

Code: Select all

function love.draw()
  if yourJoystick:isGamepadDown('a') then
    love.graphics.print('pressing A',10,10)
  end
end
(You have to assign the joystick to the variable "yourJoystick" first.)

Disclaimer: I haven't tested this code, but from how I understand the wiki, this is how it should work.
zell2002
Citizen
Posts: 75
Joined: Sun Feb 23, 2014 9:22 pm

Re: gamepads || joysticks - how to tell what is plugged in

Post by zell2002 »

ok that makes sense
360 controller is (of course) true for isGamepad()
ps2 via usb converter is false
ps3 controller is true

how could i add my ps2 controller so its buttons can be seen also as A etc...
Im writting a multiplayer game to play when friends come over, and have many ps2 controllers, so this is a must for me atm lol

cheers for the info ! spot on stuff
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: gamepads || joysticks - how to tell what is plugged in

Post by bartbes »

You can write a mapping, and set it with setGamepadMapping, it's perhaps easier to configure the controller in steam big picture mode and launch your game from there, that automatically passes along the mappings set.
zell2002
Citizen
Posts: 75
Joined: Sun Feb 23, 2014 9:22 pm

Re: gamepads || joysticks - how to tell what is plugged in

Post by zell2002 »

i have no idea what 'steam big picture mode' is lol
and i havent launched any of my .love through steam tbh so not 100% sure how that would work out


i think the gamepadmapping has finally clicked though, something like this:

Code: Select all

love.joystick.setGamePadMapping(ps2_guid, "a", "button", 5);
i assume, i'd have to run this at the start every time ?
is it possible for me to add to that SDL2 db ? :)
Post Reply

Who is online

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