Page 1 of 1

Attempt to call field 'keypressed'(a nil value) - noob help

Posted: Mon Oct 24, 2011 10:44 am
by Benni88
Hi there, I'm very new to scripting as a whole so please forgive me if i take any liberties.

I'm writing a basic random name generator as an intro to love 2D but i keep recieving the same error about the love.keypressed function.

I'm sure it's something super simple but i've checked the wiki and the forum posts but nothing explains how i might fix it in simple enough terms for me to understand.

Please view the attached .love and assist me with any help you can offer.

cheers,

Re: Attempt to call field 'keypressed'(a nil value) - noob h

Posted: Mon Oct 24, 2011 11:58 am
by kikito
Hi, there were several things wrong in your program.
  • love.keypressed is a callback function, like love.update and love.draw. You should put it on the same level
  • It isn't a good idea to use io at all from inside Löve. It isn't guaranteed to work.
  • This means that making the user type "quit" is kindof complicated. I've changed that by making the program exit when pressing escape. If you really want to check that the user types "quit", you have to use love.keypress to annotate that he pressed 'q', then 'u', etc in several variables. And reset those variables if he pressed any other keys (for example if he pressed "quuit" it shoudln't quit). As I said, it's kindof complicated
  • You were not concatenating strings properly. In Lua, you concatenate using two dots: a .. b . I've fixed that for you
  • It's easier if you replace the "insult" variable by a "message" variable, since you seem to want to print both on the same screen space
If you want to change the insult once per frame, I suggest you do it in love.update. Have love.keypress set up a variable called "start_insulting" to true or something similar, so initially the message "waits" until the user presses enter.

Regards!

Re: Attempt to call field 'keypressed'(a nil value) - noob h

Posted: Mon Oct 24, 2011 1:04 pm
by Benni88
thanks very much for your help. i appreciate you listing the issues with it aswell rather than just fixing it.

much obliged.