Jump to content




Wrong behavior in if clause


  • You cannot reply to this topic
6 replies to this topic

#1 Avarion

  • Members
  • 29 posts

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 by Avarion, 02 January 2015 - 10:27 PM.


#2 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

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


#3 Avarion

  • Members
  • 29 posts

Posted 02 January 2015 - 10:16 PM

 TheOddByte, on 02 January 2015 - 10:10 PM, said:

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



#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 02 January 2015 - 10:37 PM

You forgot the parentheses on turtle.detect.

#5 Avarion

  • Members
  • 29 posts

Posted 02 January 2015 - 10:39 PM

 Lyqyd, on 02 January 2015 - 10:37 PM, said:

You forgot the parentheses on turtle.detect.

Argh. Thanks. Stupid error.

I'm sorry I had bothered you with this. :(

#6 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 02 January 2015 - 11:31 PM

 Avarion, on 02 January 2015 - 10:39 PM, said:

- Snip -
There's nothing to be sorry for, this is what AaP is for, asking for help.

#7 Avarion

  • Members
  • 29 posts

Posted 03 January 2015 - 12:34 AM

 TheOddByte, on 02 January 2015 - 11:31 PM, said:

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






3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users