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

Wrong behavior in if clause

Started by Avarion, 02 January 2015 - 09:05 PM
Avarion #1
Posted 02 January 2015 - 10:05 PM
I've a problem with my if clause. It seems it executes code even if it should not.


while true do
		if turtle.detect == false then turtle.forward()
		else
				data = turtle.inspect()
				if data.name == "minecraft:torch" then turtle.dig()
				else break
				end
		end
		data = turtle.inspectDown()
		if data.name ~= "minecraft:stone" then
				turtle.digDown()
				turtle.select(16)
				turtle.placeDown()
		end
end


There is empty space in front of the turtle and the detect returns a false. Nevertheless the system does the inpect in line 4.
Edited on 02 January 2015 - 09:27 PM
TheOddByte #2
Posted 02 January 2015 - 10:10 PM
Make sure data isn't nil when checking

local data = turtle.inspect()
if data and data.name == "minecraft:torch" then
    -- code
end
Avarion #3
Posted 02 January 2015 - 10:16 PM
Make sure data isn't nil when checking

Ok. The problem still persists. The strange thing is since the detect returns false the if clause should not execute the inspect. Strangely it does.

Since I've deleted the code above because I've thought I had misunderstood the error message here is the code again:


while true do
	if turtle.detect == false then turtle.forward()
	else
		data = turtle.inspect()
		if data.name == "minecraft:torch" then turtle.dig()
		else break
		end
	end
	data = turtle.inspectDown()
	if data.name ~= "minecraft:stone" then
		turtle.digDown()
		turtle.select(16)
		turtle.placeDown()
	end
end

Lyqyd #4
Posted 02 January 2015 - 10:37 PM
You forgot the parentheses on turtle.detect.
Avarion #5
Posted 02 January 2015 - 10:39 PM
You forgot the parentheses on turtle.detect.

Argh. Thanks. Stupid error.

I'm sorry I had bothered you with this. :(/>
TheOddByte #6
Posted 02 January 2015 - 11:31 PM
- Snip -
There's nothing to be sorry for, this is what AaP is for, asking for help.
Avarion #7
Posted 03 January 2015 - 12:34 AM
There's nothing to be sorry for, this is what AaP is for, asking for help.

Thanks. And Thanks to you all the Bot made a smooth stonefloor in my new crafting hall.

There are many functions missing at the moment (Checking Fuel, doing ceiling and walls as well, replanting torches, configuration via GUI) but for now it has done what I needed.

Thats the code now:


ende = "no"
data = {}
turtle.select(16)
if turtle.getItemCount() > 0 then material = "yes" end
for x=10,0,-1 do
    print("Noch " .. x.. " Runden")
    ende = "no"
    while ende ~= "yes" do
        if turtle.detectDown() == false then
            turtle.placeDown()
            if turtle.getItemCount() == 0 then material = "no" end
        else
            if turtle.compareDown() == false then
                turtle.select(1)
                turtle.digDown()
                turtle.select(16)
                turtle.placeDown()
                if turtle.getItemCount() == 0 then material = "no" end
            end
        end    
        if turtle.getItemCount() == 0 then
            ende = "yes"
            break
        end
        if turtle.detect() == false then turtle.forward()
        else
            antwort, data = turtle.inspect()
            if data.name == "minecraft:torch" then
                turtle.select(1)
                turtle.dig()            
            else
                ende = "yes"
            end
        end
    end
    if material == "yes" then
        turtle.turnRight()
        if turtle.detect() == true then
            ende = "yes"
            break
        end
        turtle.forward()
        turtle.turnRight()
    end
    ende="no"
    turtle.select(16)
    while ende ~= "yes" do
        if turtle.detectDown() == false then
            turtle.placeDown()
            if turtle.getItemCount() == 0 then material = "no" end
        else
            if turtle.compareDown() == false then
                turtle.select(1)
                turtle.digDown()
                turtle.select(16)
                turtle.placeDown()
                if turtle.getItemCount() == 0 then material = "no" end
            end
        end    
        if turtle.getItemCount() == 0 then
            ende = "yes"
            break
        end
        if turtle.detect() == false then turtle.forward()
        else
            antwort, data = turtle.inspect()
            if data.name == "minecraft:torch" then
                turtle.dig()            
            else
                ende = "yes"
            end
        end
    end    
    if material == "yes" then
        turtle.turnLeft()
        if turtle.detect() == true then
            ende = "yes"
            break
        end
        turtle.forward()
        turtle.turnLeft()
    end
end