This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
asdfko0's profile picture

attempt to call nil irregularity

Started by asdfko0, 26 October 2013 - 12:03 AM
asdfko0 #1
Posted 26 October 2013 - 02:03 AM
Title: attempt to call nil irregularity

This was intended for the "ask a pro" section, but I can't post any new topics yet.

I have a program I'm writing that is a menu of different functions to build and mine resources as needed, and I'm experiencing a strange issue.

I'm not sure the best way to ask this question, so I'll just jump right in; My menu system has a variable named work, and certain menu items set this to be a specific function (such as "buildHut" which builds a 7x7x5 hut). Once a function has been assigned to "work", it gets called and the function runs.

This is working as expected for the most part. But the strange thing is that if I run my program and select an option it invariably fails with error "attempt to call nil". Rerun the program and select the same option and it runs perfectly.

So… why would selecting the same option in the program twice result in two different outcomes?

Here's the code if anyone wants to play around with it. Currently the only working menu choices are buildHut (Buildings > Hut > Build Hut > Yes) and tunnel (Resource gathering > Mining > Tunnel > Yes)
http://pastebin.com/rc7zadub

Any insight or suggestions would be appreciated. Thanks in advance!
Lyqyd #2
Posted 26 October 2013 - 04:13 AM
Split into new topic.

I haven't looked at the code, but if it doesn't work the first time but works the second time, you're probably trying to call a function before you've declared it. Be sure to declare all of your functions before you try to call them. If you reboot between the first run and the second, does it fail the second time too?
asdfko0 #3
Posted 26 October 2013 - 10:46 AM
Ah, that makes sense. I was able to reorganize my code and now it works every time. Thanks for your help!