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
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 »

Hm... odd...
Could you place the line

Code: Select all

print(key, bindings[key])
before

Code: Select all

if bindings[key] then bindings[key]() end
for me, and tell what gets printed when you press the keys W, A, S and D?
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 »

It could be because the functions Up, Down, Left and Right possible haven't been defined when you create that table.

Try putting the table in the end of the file, and see what you get.
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 »

TheLinx wrote:It could be because the functions Up, Down, Left and Right possible haven't been defined when you create that table.

Try putting the table in the end of the file, and see what you get.
Except that shouldn't be the case, as bindings is defined in load(), which is called after the whole of the file has been interpreted.
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 »

I got it to work, sorry for wasting your time. It was supposed to move the background when I pressed "d" but my function didn't work so I though the binding didn't.
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 »

tido170 wrote:I got it to work, sorry for wasting your time.
I wouldn't say it was a waste of time.
tido170 wrote:It was supposed to move the background when I pressed "d" but my function didn't work so I though the binding didn't.
Ah, not implemented behavior was one thing I didn't think of. ;)
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 »

I'm having an odd problem while trying to bind a new key... here is where I'm having a problem:

the "unbinding" works well. then I try to bind another1 like that:

Code: Select all

bindings[key] = what
what = "P1Right", the name of the function I want to be binded.

When I run it, the binding seems to work. then I press the newly binded key to call the function and here is the error message I get:
Error lua/states.lua:58: attempt to call field '?' (a string value)
line 58 is: if bindings[key] then bindings[key]() end

I did print(key, bindings[key]) and this is what I get: 100 P1Right

So I though this is odd, I wonder what gets printed if I dont bind that way. so here is what I tried instead:

Code: Select all

bindings[key] = P1Right
that way it works well and this is what gets printed: 100 function: 01508C90

wouldn't they be supposed to print the same thing?? I dont get it
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: User Define keys in a game

Post by bmelts »

The problem here is what you're setting bindings[key] to.

When you say

Code: Select all

what = "P1Right"
bindings[key] = what
That's equivalent to

Code: Select all

bindings[key] = "P1Right"
Which is wrong, because "P1Right" is a string. You can't call a string as a function that way.

You need to set it equal to the function itself - in Lua, functions can be used just like any other type of variable.

This means that the difference between P1Right and "P1Right" is that the first one is a function, and the second is a string. You need to use the first one with bindings[key] instead of the second one.

Hope this makes sense!

EDIT: The same goes for when you print them. When you print "P1Right", it prints that string. But when you print P1Right, you're referring to a function. By default, Lua prints functions by printing "function: " followed by its address in memory - that's the 01508C90 you saw.
User avatar
tido170
Prole
Posts: 13
Joined: Sat Dec 12, 2009 1:42 am

Re: User Define keys in a game

Post by tido170 »

so is there a way to bind to the function that has the same name as what is in the string or I will have do check every possibility like that:

Code: Select all

if what == "P1Right" then
bindings[key] = P1Right
elseif what == "P1Left" then
bindings[key] = P1Left

and so...
User avatar
bmelts
Party member
Posts: 380
Joined: Fri Jan 30, 2009 3:16 am
Location: Wiscönsin
Contact:

Re: User Define keys in a game

Post by bmelts »

Code: Select all

_G[what]
This is the easiest way. Example:

Code: Select all

what = "P1Right"
bindings[love.key_d] = _G[what] -- equivalent to bindings[love.key_d] = P1Right
This only works, of course, if the functions are global. But in the case of your code, they are, so this should work.
User avatar
tido170
Prole
Posts: 13
Joined: Sat Dec 12, 2009 1:42 am

Re: User Define keys in a game

Post by tido170 »

works perfectly!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 5 guests