User Define keys in a game
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
User Define keys in a game
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!
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!
Re: User Define keys in a game
You can make a table called bindings or something similar:
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:
Then to do an action when the key is pressed:
Hope this will help!
(note that all code in this post is untested, you may have to screw around to get it to work)
Code: Select all
bindings = {
[love.key_w] = goForward,
[love.key_s] = goBack,
[love.key_a] = steerLeft,
[love.key_d] = steerRight -- the values are functions!
}
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
Code: Select all
-- this goes in keypressed(key)
if bindings[key] then bindings[key]() end
(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.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: User Define keys in a game
Not to mention this keeps the old bindings intact, you may want to remove the old one.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: User Define keys in a game
The code TheLinx supplied only works if goForward and his friends return a function...
You probably want to use this:
And as bartbes pointed out, you want to clear the previous binding:
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
}
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.
Re: User Define keys in a game
oopsRobin wrote:The code TheLinx supplied only works if goForward and his friends return a function
Re: User Define keys in a game
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?
and we can get 0.6? How can we get it, I only see 0.5 on the main page?
Re: User Define keys in a game
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()
then
then you said right, up, down and left had to refer to a function so I did that
I also tried creating a bindings function but it wont get pass the "if bindings[key] then" part.
what is wrong?
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
Code: Select all
function keypressed(key)
if bindings[key] then bindings[key]() end
end
Code: Select all
function Right()
--whatever
end
and so...
what is wrong?
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: User Define keys in a game
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.
Re: User Define keys in a game
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:
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
Who is online
Users browsing this forum: Amazon [Bot], Bing [Bot] and 5 guests