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

Help function "selection"

Started by zylion, 09 March 2014 - 08:25 AM
zylion #1
Posted 09 March 2014 - 09:25 AM
Before telling you my problem, I just want to warn you that my English isn't very good… ^^'
I'm making my own program to replace a wall with a mining turtle, but I could only make a wall of 64 blocks if I didn't change the turtle's selection, so I made a function to change the selection when there is only ONE block in the selection, here's my code, but they say there's a problem :(/> .


function selection()
turtle.select(2)
turtle.getItemCount(2) == 1
while true do
turtle.select(3)
turtle.getItemCount(3) == 1
while true do
turtle.select(4)
end
end
end


They say that there's a mistake at Ln 3, can you help me, please ?
twormtwo #2
Posted 09 March 2014 - 04:10 PM
It appears that your problem is you aren't using "if … then" statements.

If your code was to run it would say select slot 2, then check if the item count is 1, but don't bother doing anything about it. Now indefinitely select slot 3, then check if the item count is 1, but don't bother doing anything about it, then indefinitely select slot 4.
This is clearly not what you want. The mistake on line 3 is that you aren't doing anything with the equality statement's result (true or false). If you check the lua manual about if then statements and while loops, you can see what your issue is.

The spoiler has my solution, but try it yourself before looking at my solution. If you just copy and paste code, then you won't get any better!
SpoilerTry something like this:

function selection()
  selection = 2
  while true do
    turtle.select(selection)
    if turtle.getItemCount(selection) <= 1 then
	  selection = selection + 1
    else
	  return
    end
    if(selection > 16)
	  print("ERROR: No more blocks!")
	  return
    end
  end
end

What this does is it selects slot 2, checks if the number of blocks in slot 2 is greater than 1, and if so, returns from the funciton, otherwise it selects slot 3, and so on, until it gets to slot 16 and if there are one or less blocks in slot 16 it prints "ERROR: No more blocks!"