2 gamepad issue

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
Pardaleco
Prole
Posts: 10
Joined: Mon Dec 31, 2018 7:12 pm

2 gamepad issue

Post by Pardaleco »

I am having an issue while trying to use 2 gamepads for my local multiplayer game (2 players).

Only 1 controller works fine, everything works as it should, but when I connect my 2nd controller it just doesn't work. It is recognized and it is a gamepad but none of its inputs work.

Am I doing something wrong with the assignment of its index? below I show some simple code that I was using to test it.

Thank you!

Code: Select all

function love.load()
    joysticks = love.joystick.getJoysticks()
    joystick1 = joysticks[1]
    joystick2 = joysticks[2]

    position = {x = 400, y = 300}
    speed = 300

end
 
function love.update(dt)
    if not joystick1 then return end
 
    if joystick2:isGamepadDown("dpleft") then
        position.x = position.x - speed * dt
    elseif joystick2:isGamepadDown("dpright") then
        position.x = position.x + speed * dt
    end
 
    if joystick1:isGamepadDown("dpup") then
        position.y = position.y - speed * dt
    elseif joystick1:isGamepadDown("dpdown") then
        position.y = position.y + speed * dt
    end
end
 
function love.draw()
    directionx = joystick1:getGamepadAxis("leftx")
    directiony = joystick1:getGamepadAxis("lefty")
    love.graphics.circle("fill", position.x, position.y, 50)
    love.graphics.print(tostring(directionx), 100,100)
    love.graphics.print(tostring(directiony), 100,150)
end<e>
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: 2 gamepad issue

Post by raidho36 »

That's probably because your gamepad "1" appears as 2nd when two of them are connected.
Pardaleco
Prole
Posts: 10
Joined: Mon Dec 31, 2018 7:12 pm

Re: 2 gamepad issue

Post by Pardaleco »

raidho36 wrote: Thu Nov 28, 2019 11:08 pm That's probably because your gamepad "1" appears as 2nd when two of them are connected.
How would I fix that?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: 2 gamepad issue

Post by raidho36 »

Pardaleco wrote: Fri Nov 29, 2019 4:57 am How would I fix that?
You don't. You can work around it, however. For example, your input settings manager can remember exactly which controller the bindings belong to (by GUID or by USB VID/PID values) so it would pick up that specific controller. Or you can prompt user to specify which controller should control what object. If all else fails, you can simply detect which controller generates input events and set that to primary controller, etc.
User avatar
Felix_Maxwell
Prole
Posts: 24
Joined: Wed Dec 04, 2019 3:15 pm

Re: 2 gamepad issue

Post by Felix_Maxwell »

The YouTuber Recursor has done a 50+ video series involving building up a brilliant class-based engine from zero- it's pretty amazing. One of the episodes is about the GamePadMgr class he makes, and it's amazing. Functions and output every time a controller is connected and disconnected, loads in bindings from 150+ controller brands in a .txt file, and I think sticks all the 'joystick/gamepad' objects in a table. I'll find the link and maybe post the code/assets after I get home from work
User avatar
D0NM
Party member
Posts: 250
Joined: Mon Feb 08, 2016 10:35 am
Location: Zabuyaki
Contact:

3 gamepads issue ))

Post by D0NM »

fyi
there is an open source DB of gamepad mappings
# Game Controller DB for SDL in 2.0.10 format
# Source: https://github.com/gabomdq/SDL_GameControllerDB

To load the DB use this function:

Code: Select all

love.joystick.loadGamepadMappings( "somePAth/gamecontrollerdb.txt" )
---------------------

back to the topic.
It is very easy to swap 2 gamepads in your code.
Or add a game menu like "Swap Controls 1 <-> 2"

also if u plug/unplug the 2nd gamepad during the game
u should use other joystick functions to redefine/re-init your came controls.

Code: Select all

function love.joystickadded(joystick)
    connected[joystick] = joystick
    --print(joystick:getGUID().." added joystick "..joystick:getName().." with "..joystick:getButtonCount().." buttons")
    bindGameInput()
end

function love.joystickremoved( joystick )
    --print("removed joystick "..joystick:getName())
    connected[joystick] = nil
end
I think you should wrap Love2d Joystick functionality with your own functions/methods.
Our LÖVE Gamedev blog Zabuyaki (an open source retro beat 'em up game). Twitter: @Zabuyaki.
:joker: LÖVE & Lua Video Lessons in Russian / Видео уроки по LÖVE и Lua :joker:
Post Reply

Who is online

Users browsing this forum: No registered users and 36 guests