User Define keys in a game

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.
User avatar
tido170
Prole
Posts: 13
Joined: Sat Dec 12, 2009 1:42 am

User Define keys in a game

Post by tido170 »

Hi all,

I'm a new lover with some but limited programming experience.
Here is my question:
How can I make the user define the keys he wants to use in the game?

More specificly, I need a line of coding for this:
detect if the key stored in a variable is pressed

I mean I would have variable = "d" and I want to check if it is press without knowing it is in fact "d" I'm looking for

thanks for your time!
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: User Define keys in a game

Post by thelinx »

You can make a table called bindings or something similar:

Code: Select all

bindings = {
    [love.key_w] = goForward,
    [love.key_s] = goBack,
    [love.key_a] = steerLeft,
    [love.key_d] = steerRight -- the values are functions!
}
Note that if you're using LÖVE 0.6.0, use "w", "s", "a" and "d" instead of the constants.
Then allow the user to change those. Here's a sample function. Just pass the argument from the keypressed function to it somehow:

Code: Select all

function bind(act, key)
    bindings[key] = act
end
Then to do an action when the key is pressed:

Code: Select all

-- this goes in keypressed(key)
if bindings[key] then bindings[key]() end
Hope this will help!
(note that all code in this post is untested, you may have to screw around to get it to work)
Last edited by thelinx on Sat Dec 12, 2009 2:23 pm, edited 1 time in total.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: User Define keys in a game

Post by bartbes »

Not to mention this keeps the old bindings intact, you may want to remove the old one.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: User Define keys in a game

Post by Robin »

The code TheLinx supplied only works if goForward and his friends return a function...
You probably want to use this:

Code: Select all

bindings = {
    [love.key_w] = goForward,
    [love.key_s] = goBack,
    [love.key_a] = steerLeft,
    [love.key_d] = steerRight
}
And as bartbes pointed out, you want to clear the previous binding:

Code: Select all

function bind(act, key)
    for key,value in pairs(bindings) do
        if value == act then
            bindings[key] = nil
            break
        end
    end
    bindings[key] = act
end
Help us help you: attach a .love.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: User Define keys in a game

Post by thelinx »

Robin wrote:The code TheLinx supplied only works if goForward and his friends return a function
oops :shock:
User avatar
tido170
Prole
Posts: 13
Joined: Sat Dec 12, 2009 1:42 am

Re: User Define keys in a game

Post by tido170 »

Thanks all, I'm gonna try that!

and we can get 0.6? How can we get it, I only see 0.5 on the main page?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: User Define keys in a game

Post by Robin »

Help us help you: attach a .love.
User avatar
tido170
Prole
Posts: 13
Joined: Sat Dec 12, 2009 1:42 am

Re: User Define keys in a game

Post by tido170 »

AHHH it wont work!

I know I'm doing something wrong but I tried many different things (never worked with lua so I'm just trying stuff)

Here is what I had to test this:

In Function load()

Code: Select all

	bindings = {
		[love.key_w] = Up,
		[love.key_s] = Down,
		[love.key_a] = Left,
		[love.key_d] = Right
		}
end
then

Code: Select all

function keypressed(key)
	if bindings[key] then bindings[key]() end

end
then you said right, up, down and left had to refer to a function so I did that

Code: Select all

function Right()
--whatever
end
and so...
I also tried creating a bindings function but it wont get pass the "if bindings[key] then" part.

what is wrong?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: User Define keys in a game

Post by Robin »

Could you upload the whole main.lua? I have an idea as to what went wrong, but we can't really know if we can't look at the whole source. ;)
Help us help you: attach a .love.
User avatar
tido170
Prole
Posts: 13
Joined: Sat Dec 12, 2009 1:42 am

Re: User Define keys in a game

Post by tido170 »

well here is my main.lua.

The condition "if bindings[key] then" just never turns out true
There isn't much more than I already gave you:

Code: Select all

function load()

	-- Includes
	love.filesystem.require("lua/button.lua")
	love.filesystem.require("lua/states.lua")

	-- Resources
	color =	 {	background = love.graphics.newColor(0,0,0),
				main = love.graphics.newColor(63,193,245),
				text = love.graphics.newColor(76,77,78),
				overlay = love.graphics.newColor(255,255,255,235) }
	font = {	default = love.graphics.newFont(love.default_font, 24),
				large = love.graphics.newFont(love.default_font, 32),
				huge = love.graphics.newFont(love.default_font, 72),
				small = love.graphics.newFont(love.default_font, 22) }
	graphics = {back_ville = love.graphics.newImage("images/back-ville.png"),
				Menu_Building = love.graphics.newImage("images/Menu-Building.png"),
				Menu_MM = love.graphics.newImage("images/Menu-MM.png")}

	-- Animation
	local cheveux = love.graphics.newImage("images/cheveux.png")
	anim_cheveux = love.graphics.newAnimation(cheveux, 21, 24, 0.08)
	
	-- Variables
	audio = true
	full = false
	device1="keyboard"
	device2="keyboard"
	device3="keyboard"
	device4="keyboard"
	bouton="kosse"
	what="kosse"
	player=2
	state = Intro.create()	-- current game state
	BackY = 200
	-- Setup
	love.graphics.setBackgroundColor(color["background"])
	
	-- Pour vérifier si il y a des joysticks
	joysticks = { }
	joystickCount = love.joystick.getNumJoysticks()
	if  joystickCount > 0 then

    for i = 0, joystickCount - 1, 1 do
        local joystick = { }
        joystick.axes = love.joystick.getNumAxes( i )
        joystick.balls = love.joystick.getNumBalls( i )
        joystick.buttons = love.joystick.getNumButtons( i )
        joystick.hats = love.joystick.getNumHats( i )
    end
	end
	
	-- User define keys
	bindings = {
		[love.key_w] = Up,
		[love.key_s] = Down,
		[love.key_a] = Left,
		[love.key_d] = Right
		}

end

function draw()
	state:draw()
end

function update(dt)
	state:update(dt)
end

function mousepressed(x, y, button)
	state:mousepressed(x,y,button)
end

function keypressed(key)
	if love.keyboard.isDown(love.key_q) then
		love.system.exit()
	end
	
	if bindings[key] then bindings[key]() end
	
	state:keypressed(key)
end

function Right()
	backy=300
end

function Left()

end

function Up()

end

function Down()

end
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Google [Bot] and 4 guests