David Gallagher
David Gallagher

Reputation: 91

Love 2D Joystick Input Management

I'm working on adding joystick control as an option to keyboard control. In short, any keyboard or joystick input is filtered through a Virtual Control class and returns a string. This string is reacted on by the main program. The system works, but I need to finesse the joystick aspect. For the keyboard portion, I have code that distinguishes between love.keyboard.isDown and love.keyboard.wasPressed, so that enter, jump and other one time entries are only acted upon once.

I originally had used joystick,isGamepadDown, which was fine for character running, but the same process used for selected choices on the pre-play menus caused enter to register multiple times. I am thinking that love.joystick.pressed is the way to handle this, but I cannot get correct sequence down. Could somebody please point me in the right direction? Thanks

in main.lua

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

    love.joystick.buttonsPressed = {}
end

function love.keyboard.wasPressed(key)
    return love.keyboard.keysPressed[key]
end

function love.joystickpressed(joystick, button)
    print(love.joystick.buttonsPressed[button])
    return love.joystick.buttonsPressed[button]
end

function love.update(dt)
    love.keyboard.keysPressed = {}
    for k, joystick in pairs(joysticks) do
        love.joystick.buttonsPressed = {}
    end
end

in Virtual Control.lua

function MenuInputs()
    for k, joystick in pairs(love.joystick.getJoysticks()) do
        if joystick:isGamepadDown('back') then
            return 'pause'
        end
        if joystick:isGamepadDown('dpdown') then 
            return 'down'
        end
        if joystick:isGamepadDown('dpup') then 
            return 'up'
        end
        if joystick:isGamepadDown('dpleft') then 
            return 'left'
        end
        if joystick:isGamepadDown('dpright') then 
            return 'right'
        end
        if love.joystickpressed(joystick, 'a') or love.joystickpressed(joystick, 'start') then
            return 'enter'
        end
    end

    if love.keyboard.wasPressed('space') then
        return 'pause'
    end
    if love.keyboard.wasPressed('s') or love.keyboard.wasPressed('down') then
        return 'down'
    end   
    if love.keyboard.wasPressed('w') or love.keyboard.wasPressed('up') then 
        return 'up'
    end
    if love.keyboard.wasPressed('a') or love.keyboard.wasPressed('left') then
        return 'left'
    end
    if love.keyboard.wasPressed('d') or love.keyboard.wasPressed('right') then
        return 'right'
    end
    if love.keyboard.wasPressed('enter') or love.keyboard.wasPressed('return') then
        return 'enter'
    end
end

Upvotes: 0

Views: 694

Answers (1)

David Gallagher
David Gallagher

Reputation: 91

I was missing a key step of using love.gamepadpressed to insert into a boolean under that button on every button press, and THEN using a created function (gamepadwasPressed) to return the boolean. Now it works perfectly.

function love.load()  
    love.keyboard.keysPressed = {}

    JOYSTICKS = love.joystick.getJoysticks()
    JOYSTICK1 = JOYSTICKS[1]
    JOYSTICK2 = JOYSTICKS[2]

    joystick1buttonsPressed = {}
    joystick2buttonsPressed = {}
end

function love.keypressed(key)
    if key == 'escape' then
        love.event.quit()
    end

    love.keyboard.keysPressed[key] = true
end

function love.keyboard.wasPressed(key)
    return love.keyboard.keysPressed[key]
end

function love.gamepadpressed(joystick, button) 
    if joystick == JOYSTICK1 then
        joystick1buttonsPressed[button] = true
    elseif joystick == JOYSTICK2 then
        joystick2buttonsPressed[button] = true
    end
end

function gamepadwasPressed(joystick, button)
    if joystick == JOYSTICK1 then
        return joystick1buttonsPressed[button]
    elseif joystick == JOYSTICK2 then
        return joystick2buttonsPressed[button]
    end
end

function love.update(dt)
    love.keyboard.keysPressed = {}
    joystick1buttonsPressed = {}
    joystick2buttonsPressed = {}
end

Upvotes: 1

Related Questions