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

Mining Program Help

Started by cwstra, 22 December 2012 - 09:52 AM
cwstra #1
Posted 22 December 2012 - 10:52 AM
I am having a strange issue with my mining program. It loops, and I can't figure out why.
Here's the code: http://pastebin.com/eaA69ppk
Doyle3694 #2
Posted 22 December 2012 - 10:58 AM
what do you mean with it loops?

Also, you don't need to have a local before every time you use a variable, actually, if you are inside a block(inside an if statement, a for loop, or anything really that is closed with an end.), the variable will be set to only that block if you use locals, and it will be what it was before that block in the rest of the code. also, use read() rather than io.read()

As well,

local mrad = io.read()
local mrad = tonumber(mrad)
local mrad = math.floor(mrad)
is very many lines, you can actually use functions inside functions. so that could very well be

local mrad = math.floor(tonumber(read()))
Orwell #3
Posted 22 December 2012 - 11:33 AM
if you are inside a block(inside an if statement, a for loop, or anything really that is closed with an end.), the variable will be set to only that block, and it will be what it was before that block in the rest of the code.
I'm not entirely sure that I get you right, but I think you're wrong. Everything declared without local will be accessible from anywhere in the same environment. It's precisely the keyword 'local' that declares them to their scope and makes them only accessible from the code block they were declared in.
Doyle3694 #4
Posted 22 December 2012 - 11:35 AM
yeah that's what I ment, was I unclear about it? Then I'm sorry, just read what Orwell said @OP for a better explanation
Orwell #5
Posted 22 December 2012 - 11:39 AM
yeah that's what I ment, was I unclear about it? Then I'm sorry, just read what Orwell said @OP for a better explanation
environment ~= code block. Didn't you say that non-local variables are limited to the if/while/for block they were declared in?
ChunLing #6
Posted 22 December 2012 - 11:48 AM
You guys are talking about:

if os.getComputerLabel() == nil then
  local turtleName = ("number " .. os.getComputerID())
else
  local turtleName = os.getComputerLabel()
end
Right?

Yeah, that doesn't do anything.

The program has a while loop. It loops. It looks like it's supposed to loop.
Orwell #7
Posted 22 December 2012 - 11:53 AM
I was rather talking about what Doyle said about globals being limited to the scope of conditionals and loops. I don't really get what he's saying there. As for the local variables in the conditional, yes that's wrong, as Doyle pointed out earlier.
Doyle3694 #8
Posted 22 December 2012 - 12:08 PM
I ment that locals in a block will only be the value you assign them in that block, so

local a = "outside"
for i = 1,1 do
   local a = "inside"
end
print(a)
would print "outside" even though it was assigned inside latest in the code, because the place it was assigned "inside" had a local, and so, when the block ended, that local variable was gone.

And yes, I see my explanation was bad. edited it abit, but it might still be bad, so just refer to this one.
Thanks for pointing it out Orwell, bad explanations can teach bad things
Orwell #9
Posted 22 December 2012 - 12:19 PM
I ment that locals in a block will only be the value you assign them in that block, so

local a = "outside"
for i = 1,1 do
   local a = "inside"
end
print(a)
would print "outside" even though it was assigned inside latest in the code, because the place it was assigned "inside" had a local, and so, when the block ended, that local variable was gone.

And yes, I see my explanation was bad. edited it abit, but it might still be bad, so just refer to this one.
Thanks for pointing it out Orwell, bad explanations can teach bad things
Aight, no problem, I see now that we're on the same page. :)/> Good example btw.
cwstra #10
Posted 23 December 2012 - 08:08 AM
Sorry about not being more specific; I was brain-tired and wasn't thinking right.

I have the two bottom-most sections of code set to repeat the basic mining functions with increasing height and width by adding one to the variable it plugs into the functions. But it keeps on getting stuck mining an eight block area; it keeps on mining radius one, height one. I don't understand why the last two variables are not being adjusted.

I also did understand the above discussion, and adjusted the in-game code accordingly.
Doyle3694 #11
Posted 23 December 2012 - 01:05 PM
Are you sure you removed all scoped in locals? because the one that we see on pastebin local chei = chei + 1 is what's making it not work
cwstra #12
Posted 23 December 2012 - 02:33 PM
Herp derp. Forgot to save changes. It works now. Thanks!