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

Programming Help

Started by Asajz, 02 November 2014 - 12:42 AM
Asajz #1
Posted 02 November 2014 - 01:42 AM
I have been working on a program to create a 3x3 Tunnel while also placing torches, and returning ore/cobble to the start of the tunnel. However, after my program starts, it make it into the first block of the tunnel and then stops. Please Help

Code:
http://pastebin.com/YpBFGgMU

Further Explanation:
Now, having hopefully looked at my code, you will see the initial questioning the user receives. This process works correctly, then the instructions show, and the turtle digs out the first block. Then, it stops. It digs the first block of the tunnel; however, it does not move forward, prints the number '186', and the quits the program with no further notice.
Thank you for your time!
Bomb Bloke #2
Posted 02 November 2014 - 03:29 AM
The script is erroring, but I'm not sure why you're not seeing a full error. Which version of ComputerCraft are we talking about?

The problem seems to be that you're attempting to reference your tryForward() function on line 73 (in the digIn() function), though you don't define tryForward() until line 76.

When the Lua interpreter runs your script, as it hits each function declaration, it compiles the content to bytecode. When it compiles digIn(), it sees the tryForward() reference, and since tryForward() hasn't already been declared it assumes it should treat it as a global.

But when you do define tryForward(), you create it as a local. So when digIn() actually gets called, it ends up trying to call that global you referenced earlier - which is nil - and the script crashes out. This could be solved by moving the tryForward() declaration above the digIn() declaration, so the compiler will know about your local tryForward() function when it goes to build your digIn() function.

In general, you should always stick function declarations up above where you try to call them. See here for more info.

There are quite a few other issues within the script that could use fixing, but I suspect this'll solve your most immediate problem.
Edited on 02 November 2014 - 02:33 AM
Asajz #3
Posted 02 November 2014 - 08:22 PM
Ok, thank you. It has been a while since I have done Lua (or Modded Minecraft in general), and forgot about that detail… I'll try it out and see how it goes.