Page 1 of 1

Testing Controller 'game'

Posted: Fri Feb 19, 2021 12:26 pm
by Reenen
I'm creating a controller 'test' game. Which essentially will show you if your controller is giving issues. (Mine is)

My only issue is that I cannot see currently if either of the triggers are being pressed.

My code is:

Code: Select all

local keysPressed = {}

function love.keypressed(k)
  keysPressed[k] = true
end

function love.keyreleased(k)
  keysPressed[k] = nil
end


function love.gamepadpressed(joystick, button)
  keysPressed[button] = true
end

function love.gamepadreleased(joystick, button)
  keysPressed[button] = nil
end


function love.draw()
  love.graphics.print("Keys pressed:", 10, 10)
  local y = 30
  for k,_ in pairs(keysPressed) do
    if (k == " ") then st = "Space" else st = k end
	love.graphics.print(st, 20, y)
    y = y + 15
  end
end
But this shows nothing when I press the triggers. I guess I can't check the triggers with 'gamepadpressed' so I need another command, and I'm not very good at Love so I don't know what it is. If someone could point me to where I can check the % that a trigger is down (or int value whatever they measure it in) I'd appreciate it.

Re: Testing Controller 'game'

Posted: Fri Feb 19, 2021 1:31 pm
by Reenen
I figured it out. (Reading documentation works wonders!)

Code: Select all

local keysPressed = {}
local stick = nil

function love.keypressed(k)
  keysPressed[k] = true
end

function love.keyreleased(k)
  keysPressed[k] = nil
end

function love.joystickadded(joystick)
  stick = joystick
end


function love.gamepadpressed(joystick, button)
  keysPressed[button] = true
end

function love.gamepadreleased(joystick, button)
  keysPressed[button] = nil
end


function love.draw()
  love.graphics.print("Keys pressed:", 10, 10)
  local y = 30
  for k,_ in pairs(keysPressed) do
    if (k == " ") then st = "Space" else st = k end
  	love.graphics.print(st, 20, y)
    y = y + 15
  end

  local y2 = 30
  if stick ~= nil then
    love.graphics.print("Controller Axes Positions:", 200, 10)
    axis = {"leftx","lefty","rightx","righty","triggerleft","triggerright"}
    for k,v in pairs(axis) do
      love.graphics.print(v .. " : " .. stick:getGamepadAxis(v),200,y2)
      y2 = y2 + 15
    end
  end
end
There is obviously an assumption there on the controller axes (I hardcode the names in there), but it is working for my purposes.