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

[Lua] [Turtle] Unintended behavior with turtle.select()

Started by civilwargeeky, 10 April 2013 - 04:29 PM
civilwargeeky #1
Posted 10 April 2013 - 06:29 PM
So, I have a logger program that works fine, until it finishes, where it drops different items in several different places. The underlying problem seems to be that the turtle drops items before switching to a designated slot, causing problems. I tried adding sleep(0.5) after my turtle.selects, but it had no effect. The first issue is the basic drop of wood to a chest. I have
if turtle.detect() then --If there is a chest
for i=2, 14 do --Slots excluding charcoal and saplings
turtle.select(i)
if not turtle.compareTo(1) then --If item is not a sapling
turtle.drop() --Drop to chest
end
end
end
Expected: Turtle selects slots 2 - 14, dropping wood into chest
Actual: Turtle selects slots 1- 14, dropping wood and saplings into chest.

Next when it tries to get fuel from a furnace
turtle.select(16) --Fueling slot
turtle.dropDown() --To remove any misplaced items
turtle.suck() --Suck charcoal from furnace
Expected: Turtle selects slot 16, drops any items, and sucks charcoal
Actual: Turtle drops saplings from slot 1 (selected), switches to slot 16, and (assumingly) sucks from furnace

There might be some other problems, but I didn't test that much because I was so frustrated with this not working right. Why on earth is it doing this? This hasn't happened for me in previous versions. I used this on a server, but it is a pretty good server with only one other person on it at the time, no lag at all.
jag #2
Posted 10 April 2013 - 07:10 PM
Are you playing around with the parallels API, because that can cause unexpected problems.

Otherwise, I have no clue…
Sora Firestorm #3
Posted 10 April 2013 - 07:54 PM
It's actually a bug in the newest version of ComputerCraft.

It _will_ be fixed, but in the mean time, make sure the slot you are
trying to drop have something in them. (By using something like turtle.getItemCount(<SLOT>) within an if)
Neekow #4
Posted 11 April 2013 - 03:35 AM
not really a fix but why not trying:

for i = 1,13
  turtle.select(i+1)
  if not turtle.compareTo(1) then
	turtle.drop()
  end
end

dunno if it's working … but hope this will force the selection 2-14
Sora Firestorm #5
Posted 11 April 2013 - 12:27 PM
It won't work, trust me. Just change the condition to

turtle.getItemCount(i) > 0 and not(turtle.compareTo(1))

This will then cause it to check first if the slot has any items,
as Lua's or and and operators do something known as short-circuiting,
where you only evaluate the other expression if necessary.
(In this case, not even trying compareTo if getItemCount isn't at least one.)
civilwargeeky #6
Posted 11 April 2013 - 04:18 PM
Well. Using "turtle.getItemCount(i) and turtle.select(i) and turtle.compareTo(1) then" got the wood dropping section to work, and I fixed other sections as well. Thank you very much.