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

[Lua][Question] A question regarding Lua (or, can I do this?)

Started by joaopada, 13 April 2013 - 11:34 PM
joaopada #1
Posted 14 April 2013 - 01:34 AM
Hi. I just installed computer craft, and messed around with turtles. I was able to make a program that will get Flint for me.



http://pastebin.com/5TvXRhQV
That's the code. It's VERY simple, I am really a beginner with ComputerCraft so I made the code with what I know.

Currently it only has one issue. When it changes to slot 3, it keeps going back and forth from slot 3 to slot 2.
I know why this happens, but I must ask you if it is possible to assign a function to a "while" loop, so when the turtle changes to slot 3 it keeps placing and digging the gravel.

Question 2: How do I make it so when the turtle detects that the current slot has no gravel, and then moves on to the next one?
Question 3: And how do I make it so when the turtle uses up all gravel in all slots, it goes get gravel at a chest?


I'm sorry for so many questions, but I would really appreciate it if you could tell me how to make them possible. Thank you!
Engineer #2
Posted 14 April 2013 - 01:53 AM
The issue is what you are having, when you dig the gravel up again, the gravel goes into the first available slot, wich is slot 2.

Question 2:
you can use the turtle.getItemCount(slotNumber) function. This will return 0 when the slot is empty.

Question 3:
Well combined with the previous question I made the following script, with explanation:

if turtle.getItemCount(2) == 0 then -- Check if the slot is empty
   -- Face the chest
   turtle.suck() -- this will grab the first available stack in the chest
end -- end the if statement.

Those questions being answered, I made the following script for you:

while true do
	if turtle.getItemCount(1) == 0 then
		turtle.suck()
		if turtle.getItemCount(1) == 0 then
			print("Out of gravel")
                        break
		end
	end

	turtle.placeUp()
	turtle.digUp()
end

This is assuming that you face the chest continueusly. Eventually its inventory will fill up with flint, if it can always grab gravel, if you have questions about this, please ask it in this topic.
Edited on 14 April 2013 - 12:13 AM