*SOLVED* Changable controls?

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
User avatar
WolfNinja2
Party member
Posts: 150
Joined: Wed Oct 24, 2012 10:10 pm

*SOLVED* Changable controls?

Post by WolfNinja2 »

I'm making a game and would like to make settings changeable. So if the player doesn't want to use the "w" key to move up they might change it. I'm at a complete loss as to how to do this and would appreciate any help! Thanks!
Last edited by WolfNinja2 on Sun Feb 08, 2015 6:25 am, edited 2 times in total.
Muris
Party member
Posts: 131
Joined: Fri May 23, 2014 9:18 am

Re: Changable controls?

Post by Muris »

The way how I would go with this, or see this being done is using love.keypress/release. Like when you click a button you have a variable that checks a state for something like keyToChange or whatever, which would be a string pointing in the tables key. Then it changes that value. I can try to figure out writing some example. You probably would have to implement your own version of it, but just to try to show how I would do it. Probably it is not the best way but maybe one way at least. As for how long it takes to write an example, I have no idea.

This is a quick code what I had in my mind. As usual there is hardly any comments, just drop a question if there is something you wanna have more clarification. You can ignore most of the code, like whats buttons etc, it was just quick and dirty implementation to have some kind of buttons to click.

Code: Select all

local settings = {
  leftKey = "a",
  rightKey = "d",
  upKey = "w",
  downKey = "s",
  crouchKey = "lctrl",
  centerOfScreen = love.graphics.getWidth()/2
}

local buttons = {
	bLeft = { "Left:", "leftKey", 20, 80 },
	bRight = { "Right:", "rightKey", 80, 80 },
	bUp = { "Up:", "upKey", 50, 50 },
	bDown = { "Down:", "downKey", 50, 80 },
}


local keyToChange = nil
local fontHeight
function love.load()
	fontHeight = love.graphics.getFont():getHeight()
end

function love.draw()
	local letter
	for i,v in pairs( buttons ) do
		letter = settings[v[2]]
		love.graphics.rectangle("line", v[3], v[4], 30, 30 )
		love.graphics.print( letter, v[3] + ( 30 - love.graphics.getFont():getWidth(letter) ) / 2, v[4] + ( 30 - fontHeight ) / 2)
	end
	if keyToChange then
		love.graphics.print("Change key to: " .. keyToChange )
	end

	local downButton
	local xPos = 10
	local yPos = 120
	love.graphics.print( "Holding down:", xPos, yPos )
	yPos = yPos + fontHeight
	for i,v in pairs( buttons ) do
		downButton = settings[v[2]] 
		if love.keyboard.isDown( downButton ) then
			love.graphics.print( downButton, xPos, yPos )
			xPos = xPos + love.graphics.getFont():getWidth(downButton)
		end
	end
end

function love.update(dt)
end

function love.mousepressed( x,y, button ) 
	if button ~= 'l' then return end
	keyToChange = nil
	for i,v in pairs( buttons ) do
		if v[3] <= x and x <= v[3] + 30 and v[4] <= y and y <= v[4] + 30 then -- if click was within bounds
			keyToChange = v[2]
		end
	end

end

function love.keypressed( key )
	if key == "q" or key == "escape" then
		love.event.quit()

	elseif keyToChange then -- if keyToChange has been decided
		for i,v in pairs( settings ) do
			if v == key then -- swap keys
				print( "Swapping keys:", i, keyToChange )
				settings[i] = settings[keyToChange]
			end		
		end
		settings[keyToChange] = key
		keyToChange = nil
	end

end

function love.keyreleased( key )
end
Edit random note: pressing Q quits, so if you try to change button to Q it just exits the program.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 228 guests