Page 2 of 3

Re: User Define keys in a game

Posted: Sun Dec 13, 2009 3:56 pm
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?

Re: User Define keys in a game

Posted: Sun Dec 13, 2009 4:11 pm
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.

Re: User Define keys in a game

Posted: Sun Dec 13, 2009 4:28 pm
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.

Re: User Define keys in a game

Posted: Sun Dec 13, 2009 4:29 pm
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.

Re: User Define keys in a game

Posted: Sun Dec 13, 2009 4:43 pm
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. ;)

Re: User Define keys in a game

Posted: Sun Dec 13, 2009 10:07 pm
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

Re: User Define keys in a game

Posted: Mon Dec 14, 2009 1:26 am
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.

Re: User Define keys in a game

Posted: Mon Dec 14, 2009 1:58 am
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...

Re: User Define keys in a game

Posted: Mon Dec 14, 2009 2:26 am
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.

Re: User Define keys in a game

Posted: Mon Dec 14, 2009 2:33 am
by tido170
works perfectly!