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

Lua Beginner Issue

Started by sirlawnsalot, 14 November 2016 - 09:30 PM
sirlawnsalot #1
Posted 14 November 2016 - 10:30 PM
I have just started fooling around with turtles and i have been having a blast! I have made a couple of basic programs, but i have had some trouble with this one. the idea is to build a huge platform in the sky.

The issue i am having is that the slot variable seems to increment regardless of what turtle.place() returns

What I would like is for the code to check if the inventory slot is empty, and if so move to the next slot.
What is happening is for every block placed it moves to the next inventory slot regardless and the loop terminates.

Thank you for your time! :D/>


blockPlaceLimit = 21
rednet.open("right")
function go()
local counter = 0
local slot = 1

turtle.select(1)

while counter < blockPlaceLimit and slot < 16 do
  turtle.place()												-------------------
  if turtle.place() == false then					   Trouble Area
   slot = slot + 1												<-------
   turtle.select(slot)									   ---------------------
  end
   turtle.back()
   counter = counter + 1


  rednet.send(12, "=======================")
  rednet.send(12, counter)
  rednet.send(12,"========================")
  rednet.send(12,"========================")
  rednet.send(12, slot)
  rednet.send(12,"========================")

end
end

go()
Dog #2
Posted 15 November 2016 - 02:05 AM
One thing that is happening (whether you realize it or not) is that you're actually trying to place twice. First when you call turtle.place() and then again when you check to see if it returns false. In order to return a value, the action has to be carried out - and the value is returned at the time (or right after) the action is carried out - the value isn't stored anywhere unless you capture it in a variable. What is probably happening is that you are successfully placing a block, then failing to place a second time which leads to the slot number getting incremented and the new slot being selected.

TL;DR - Eliminate the first turtle.place() and see if just having the if statement gives you the results you are looking for.

EDIT:
Almost forgot - to get the number of items in a slot, take a look at turtle.getItemCount()
Edited on 15 November 2016 - 01:14 AM
Bomb Bloke #3
Posted 15 November 2016 - 03:08 AM
You'll likely also want to have the turtle move backwards only if a block has been placed. The keyword "else" may help you with that.

What I would like is for the code to check if the inventory slot is empty, and if so move to the next slot.
What is happening is for every block placed it moves to the next inventory slot regardless and the loop terminates.

I wish more coders wrote decent explanations like this one.
sirlawnsalot #4
Posted 15 November 2016 - 04:56 AM
Wow I had no idea Turtle.place() was being called twice! I removed the first one and the program works as expected finally! I also added the else as suggested by Hobbyist and it is working great appreciate the help guys! I'm sure getItemCount() will come in handy as well. Thank's again!
Lupus590 #5
Posted 15 November 2016 - 03:20 PM
…as suggested by Hobbyist …

User names are in the blue bar above the image, Hobbyist Coder is Bomb Blokes title/nickname thing.