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

[LUA] Autocrafting Iron

Started by cockatoo2, 21 September 2014 - 04:55 PM
cockatoo2 #1
Posted 21 September 2014 - 06:55 PM
Hello guys and I am a noob with crafty turtles. Just thought I should start with that. I need someone to help me make a program that takes items from a chest above the crafty turtle, crafts the item and then puts it into the chest below. It will try to take 4 broken iron ores (Sky factory), and put them in the shape of a crafting bench, 2X2, and craft iron ore gravel. It will then be put into the chest below. But it won't even take items and try to craft if it can't even take the 4 items from the chest above.

Can someone PLEASE help me.
Lyqyd #2
Posted 21 September 2014 - 07:04 PM
Please post your current code and describe what you're stuck on, or list the full error message you're receiving.
cockatoo2 #3
Posted 22 September 2014 - 12:27 AM
Ok my current code is:

while true do
  recipes = {
    ["3004"] = {
	  count = 4,
	  map = {
	    [1] = true, [2] = true, [3] = false,
	    [5] = true, [6] = true, [7] = false,
	    [9] = false, [10] = false, [11] = false
	  }
    }
  }
  function craftRecipe(recipe)
    turtle.select(1)
    for k,v in pairs(recipe.map) do
	  turtle.select(k)
	  if v then
	    turtle.suckDown()
	    turtle.dropDown(turtle.getItemCount(k)-1)
	  end
    end
    turtle.select(1)
    turtle.craft()
    turtle.dropDown()
  end
  try = turtle.suckUp(4)
    craftRecipe(recipes["3004"])
    sleep(5)
  
end
and the error I am getting is: run:23: attempt to call nil

What am I doing wrong.
lebuildman #4
Posted 22 September 2014 - 02:41 AM
First of all: for k,v in ipairs(…

in pairs() don't seems to work…
Dog #5
Posted 22 September 2014 - 03:28 AM
lebuildman's information is incorrect; pairs() works fine. If recipe.map points to an ordered table, then ipairs would probably be a good way to parse the table, otherwise it's a bit more situational.

Based on what you posted, I don't see a problem on line 23. The only possibility I can see is that slot 1 could be empty; I don't know what happens if you call dropDown() on an empty slot. I can see some other possible problems, but they aren't on or directly related to line 23.
Edited on 22 September 2014 - 01:50 AM
Bomb Bloke #6
Posted 22 September 2014 - 03:48 AM
The only line I see which could throw an "attempt to call nil" is 22, the "turtle.craft()" call - and that's only if the turtle isn't a crafty turtle.

If it is, try breaking and replacing it. Make sure you set a label first.
cockatoo2 #7
Posted 22 September 2014 - 04:27 AM
It is a crafty turtle and if you were to code this can you show me how you would code it and explain it?
Dog #8
Posted 22 September 2014 - 05:38 AM
Did you try breaking and replacing the turtle (after labeling it) as Bomb Bloke suggested?

As for suggestions as to how to approach coding it (not sure if that was directed solely at Bomb Bloke or anyone) - I'm not familiar with crafty turtles, so I wouldn't know where to start to write a program for one. I can help troubleshoot your current code (from a functional standpoint) but starting from scratch would not be something I'd be very helpful with.
Edited on 22 September 2014 - 03:41 AM
cockatoo2 #9
Posted 22 September 2014 - 01:41 PM
I haven't tried breaking and replacing but I will try. And anyone can help me fix the code or write it new
Cycomantis #10
Posted 25 September 2014 - 12:10 AM
The code runs fine there are no errors in the code itself. I've made a couple of changes to the logic and it works great. Here is my updated version with comments.


while true do
  recipes = {
	["3004"] = {
		  count = 4,
		  map = {
			[1] = true, [2] = true, [3] = false,
			[5] = true, [6] = true, [7] = false,
			[9] = false, [10] = false, [11] = false
		  }
	}
  }
  function craftRecipe(recipe)
	turtle.select(1)
	for k,v in pairs(recipe.map) do
		  turtle.select(k)
		  if v then
			--turtle.suckDown() --remove you want mats from chest above the turtle.
			turtle.suckUp() --to suck mats from chest above
			--could also just change to turtle.suckUp(1), and remove the drop
			turtle.dropUp(turtle.getItemCount(k)-1) --changed to Up to place excess material in supply chest
		  end
	end
	turtle.select(1)
	turtle.craft()
	turtle.dropDown()
  end
  --try = turtle.suckUp(4) --remove you don't need this you do it in the craftRecipe() function
	craftRecipe(recipes["3004"])
	sleep(5)
end
Edited on 24 September 2014 - 10:11 PM