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

mining program not working

Started by supertrol, 31 July 2016 - 12:06 AM
supertrol #1
Posted 31 July 2016 - 02:06 AM
so, i was having fun with my making a program that would mine down check all 4 block around it and then(when it has found ore or something) it would place a block and go back up, dump the items and terminate. but i ran into this problem: the if statement which is inside a for loop which is inside a for loop didn't even run

here's the code:

amount = nil
gone = 0
done = false
toadd = 1
is = 0
compared = false

function check(amount)
for i = 1,4 do
  for i = 1,amount do
	  if turtle.compareTo(amount) == true then
	   is = is + toadd
	   print("hi")
	  end
  end
  turtle.turnRight()
end
end
function dropAll()
for i = 1,15 do
	 turtle.select(i)
  turtle.drop()
end
end
turtle.select(1)
while done == false do
turtle.digDown()
turtle.down()
gone = gone + 1
check(6)
if is < 4 then
  done = true
  end
  is = 0
end
turtle.digDown()
turtle.select(16)
turtle.placeDown()
for a = 1,gone do
turtle.up()
end
turtle.turnRight()
turtle.turnRight()
dropAll()
turtle.turnRight()
turtle.turnRight()

The_Cat #2
Posted 31 July 2016 - 11:03 AM
So you are using the compareTo predefined function. I don't think this is what you want.
As in your code you are saying compare the current selected slot through slot 1 - 6. Click here for the wiki
You want to compare the block that is not in your inventory.
Check out the inspect method. Click here for the wiki
So when you inspect a block you can get its name.

Depending on what you want: The way I'd do it is make a table with all the ores (that you want) and every time the turtle inspects you can compare the name of the block inspected to this table.
supertrol #3
Posted 31 July 2016 - 12:50 PM
i know that but that's not the problem becuase is always stays 0 even if the block it compares to is the same a in the inventory
supertrol #4
Posted 31 July 2016 - 05:38 PM
thank you!! when i did what you said i worked perfectly, and i noticed that i forgot to put grass inside the turtle(derp)
The_Cat #5
Posted 31 July 2016 - 06:18 PM
Ok, let me re-write what i said.
So you said you wrote a program which checks all 4 blocks around the turtle. This is incorrect, as you are using the 'compareTo' method this is ONLY for the inventory of the turtle. For example when turtle.compareTo(2) is called it gets the currently selected slot and compares that slot to slot 2, to see if they are the same, if so it will return true. You basically need to use inspect for what you are wanting to do.

Let me tell you what your 'check' function is doing:

function check(amount)
for i = 1,4 do --# Loop 4 times in order to turn right, this is fine.
  for i = 1,amount do --# Looping to the amount (which is 6 in your program)
		  --# This is saying if slot 1 (current selected slot) is the same as slot 1 ( 2, 3, 4, 5, 6. as its in a loop) then it will be true. nothing is being got from what is in front of the turtle.
		  if turtle.compareTo(amount) == true then
		   is = is + toadd
		   print("hi")
		  end
  end
  turtle.turnRight()
end
Edited on 31 July 2016 - 04:19 PM
supertrol #6
Posted 02 August 2016 - 10:17 PM
looks like my use of the compareTo function was very incorrect but anyways i got it working (thank you for that) and btw how do i make a variable that can be used in any other program without first specifying what program it is from?
Edited on 02 August 2016 - 08:17 PM
The_Cat #7
Posted 03 August 2016 - 09:37 AM
looks like my use of the compareTo function was very incorrect but anyways i got it working (thank you for that) and btw how do i make a variable that can be used in any other program without first specifying what program it is from?
A way of achieving this would be to write in to a file then read from the file in the program. Click here for the wiki
Edited on 03 August 2016 - 07:38 AM