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

Confused about stopping while loops and bool checks

Started by Zixinus, 02 February 2015 - 07:21 PM
Zixinus #1
Posted 02 February 2015 - 08:21 PM
I want to make an improved excavate program (I sadly have no other idea what I should program). I have done my first semester as CS student and learned C# (we've gone as far as simple sorting algoritms). I tried using Computercraft to keep myself in practice.

I am confused about how when Computercraft does a check for a Bool condition.

The program should keep digging down until it finds bedrock under itself.

The goal is to have a program that stops when it detects either bedrock or mossy cobblestone (I want to save the spawner and loot). This program can be a base for it. However, while the turtle does start digging, it gets stuck in an infinite loop when it hits bedrock.

My code looks like this roughly:


local success, data= turtle.inspectDown()
local bedrockname="minecraft:bedrock"
local IsBedrock= data.name==bedrockname
--Main Program
while IsBedrock==false do
turtle.digDown()
turtle.down()
end

I know that the turtle can accurately detect with this code when IsBedrock is true. My question is, what am I doing wrong thta makes this program get stuck?
Lyqyd #2
Posted 02 February 2015 - 09:07 PM
When you assign a value to a variable, it hangs on to that value. So, your code example checks to see if the block below the turtle is bedrock, then assigns the resulting boolean to a variable and never changes it again. If it wasn't bedrock before the loop started, the turtle will continue that loop forever (it's essentially `while true do` at that point). You need to move the inspectDown call and the name check inside the loop. Something like this might work:


local success, details = turtle.inspectDown()
while (not success) or (success and details.name ~= "minecraft:bedrock") do
  turtle.digDown()
  turtle.down()
  --# no local keyword here so it just changes the upvalues, so the loop can check the updated values.
  success, details = turtle.inspectDown()
end
Zixinus #3
Posted 02 February 2015 - 09:31 PM
Ah, that makes sense. It works that way.

So I should call that local again to make it check for that Boolean variable?
Lyqyd #4
Posted 03 February 2015 - 12:59 AM
So I should call that local again to make it check for that Boolean variable?

I'm really not sure what you're asking here. Functions are called, variables can be local, but we don't have any local variables containing functions in that script.
Bomb Bloke #5
Posted 03 February 2015 - 05:46 AM
Or, to put it another way, this:

local success, details = turtle.inspectDown()

… initialises two new local variables ("success" and "details"), and sets them to the values returned by calling the function pointed to by the key "inspectDown" which exists within the "turtle" table.

Later, when you call the function again within your loop, you specifically do not want to re-initialise the variables as new locals - you want to re-use the old ones, because those're what the "while" loop will be looking at each time it decides whether to repeat or not - and so the local keyword is omitted:

success, details = turtle.inspectDown()

See this guide for more details on "local".
Edited on 03 February 2015 - 04:48 AM
Zixinus #6
Posted 03 February 2015 - 10:24 AM
So I should call that local again to make it check for that Boolean variable?

I'm really not sure what you're asking here. Functions are called, variables can be local, but we don't have any local variables containing functions in that script.

Sorry, I misused the terminology.

What I meant was: if I want to recheck the assigned Bool variable of IsBedrock, I need to write out the variable (success, data = turtle.inspectDown() ) every time I want it to check.

Do the loops check for true/false condition at every substep or only when the code they are given is executed (like with C# in Visual Studio)?