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

Building program for a turtle

Started by wulfbear_joe, 20 April 2014 - 12:44 PM
wulfbear_joe #1
Posted 20 April 2014 - 02:44 PM
Hey there!

First of all you should know that I'm new to the forum and new to computercraft, I haven't done any programming at all. I just tried to learn Computercraft as I am a passionate FTB player.
The problem I now have is the following: I tried to make a program which builds rectangular platforms in a size that you can specify. I tried doing this with two functions and loops ( here's the pastebin: http://pastebin.com/RfAit5aw). However, the way I setup the local variables causes the turtle to ask for the variables everytime the loop begins. I thought about moving the definition of the variables from the function to the main program but I'm not sure if this would work. Does anyone know how I could fix the code and make the program more efficient?

Thanks,
wulfbear_joe
CometWolf #2
Posted 20 April 2014 - 08:35 PM
That would work just fine. Don't be afraid to try things out, it's the only way to learn.
wulfbear_joe #3
Posted 20 April 2014 - 11:02 PM
Ok just tried that out. Here's the changed version (http://pastebin.com/4gtfekjy), but when I run the program it tells me that "the "for" limit must be a number". Does the function not know the local variable if it's not defined inside the function or am I doing something else wrong?
CometWolf #4
Posted 21 April 2014 - 10:05 AM
I was half expecting it to do that already to be honest. The function read() will always return a string, even if you input just numbers, it will still be a string. Use tonumber to convert it to a number.

local num = tonumber(read())
wulfbear_joe #5
Posted 21 April 2014 - 11:55 AM
I cleaned up the code and tried the tonumber (http://pastebin.com/CjC3MqEJ) but I am getting the same error: http://www.directupload.net/file/d/3599/emsnxgup_png.htm
Moreover, I don't understand why the read() function did output a number in the very first edition of the code ( see first post); the code was working there but it always asked for the variable every single loop.
Edited on 21 April 2014 - 09:57 AM
Bomb Bloke #6
Posted 21 April 2014 - 12:43 PM
This has to do with the way function definitions and localised variables work.

When the Lua VM runs your script, about the first thing it'll do is see your definition of the "build()" function. It starts to convert that to byte-code.

When the build() function refers to the "x" variable, Lua looks to see if a local version has been declared yet. Since it hasn't (it happens lower down, after the "build()" definition), Lua decides to treat it as a global variable.

So, when you later declare a version of "x" that's local to your script, that ends up being a different version of "x" in relation to the one you referred to in your "build()" function. Since you never assign a value to that global "x", the script errors out when you try to construct a loop from it.

The solution is a forward declaration: Tell Lua you want to localise "x" to your script before you actually start "using" it. Eg:

local x  -- "x" is now declared as local, though we haven't given it a value yet.

local function build()  -- Functions can and usually should be localised too!
  for i = 1, x do       -- Since a local "x" has been declared already, that's where this will point.

  etc...

end

etc...

--Main Program
print("Starting...")
term.write("Enter depth: ")
x = tonumber(read())  -- "x" is now already local, no need to re-declare it as such here.

etc...

Some more details can be found here.

You'll find that, when localising functions, you'll need to declare each one before any that call them - this is for the exact same reasons as the issue you've run into here.
wulfbear_joe #7
Posted 21 April 2014 - 01:09 PM
Thanks for the suggestion and the link mate, works perfectly now!