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

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.
Post Reply
Benni88
Prole
Posts: 10
Joined: Mon Oct 24, 2011 10:34 am

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

Post 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,
Attachments
main.love
(500 Bytes) Downloaded 146 times
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

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

Post 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!
Attachments
obnoxious-wastrel.love
(481 Bytes) Downloaded 161 times
When I write def I mean function.
Benni88
Prole
Posts: 10
Joined: Mon Oct 24, 2011 10:34 am

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

Post by Benni88 »

thanks very much for your help. i appreciate you listing the issues with it aswell rather than just fixing it.

much obliged.
Post Reply

Who is online

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