Jump to content




Need help with the turtle.inspect() command


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

#1 tvc

  • Members
  • 35 posts

Posted 25 November 2014 - 01:11 AM

So I'm building a large mining program...( Yes I Know there are like 900 i could use but you know pride)

But im running into issues with a if then loop. The turtle is doing both the if and the else. Ill Post a copy of my code here

The area of the code is between line 138 and 165. Ill also include those lines here
Spoiler


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 25 November 2014 - 01:17 AM

This chunk here:

        for k,v in pairs(p2) do
         if v == "ComputerCraft:CC-Turtle" then
          sleep(.1)
         else
          turtle.dig()
          turtle.forward()
          tCheck = false
         end
        end

Assuming the p2 table contains multiple entries (and IIRC it always will), so long as at least one of them is "ComputerCraft:CC-Turtle" it'll sleep for a tenth of a second... and so long as at least one of them is anything else, it'll dig and go forward.

Presumably what you want to do is make sure NONE of the entries are "ComputerCraft:CC-Turtle" before you attempt to dig:

        local test = false

        for k,v in pairs(p2) do
         if v == "ComputerCraft:CC-Turtle" then
          test = true
          break
         end
        end

        if test then
         sleep(.1)
        else
         turtle.dig()
         turtle.forward()
         tCheck = false
        end


#3 tvc

  • Members
  • 35 posts

Posted 25 November 2014 - 01:22 AM

View PostBomb Bloke, on 25 November 2014 - 01:17 AM, said:

This chunk here:

		for k,v in pairs(p2) do
		 if v == "ComputerCraft:CC-Turtle" then
		  sleep(.1)
		 else
		  turtle.dig()
		  turtle.forward()
		  tCheck = false
		 end
		end

Assuming the p2 table contains multiple entries (and IIRC it always will), so long as at least one of them is "ComputerCraft:CC-Turtle" it'll sleep for a tenth of a second... and so long as at least one of them is anything else, it'll dig and go forward.

Presumably what you want to do is make sure NONE of the entries are "ComputerCraft:CC-Turtle" before you attempt to dig:

		local test = false

		for k,v in pairs(p2) do
		 if v == "ComputerCraft:CC-Turtle" then
		  test = true
		  break
		 end
		end

		if test then
		 sleep(.1)
		else
		 turtle.dig()
		 turtle.forward()
		 tCheck = false
		end

perfect it work thank you very much

#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 25 November 2014 - 04:00 AM

Actually, come to think of it, looking at the docs for turtle.inspect() we can see the name of the key that'll have the data we're interested in. Thus we can shrink the code further:

        if p2.name == "ComputerCraft:CC-Turtle" then
         sleep(.1)
        else
         turtle.dig()
         turtle.forward()
         tCheck = false
        end

No need for a "for" loop.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users