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

Determining which slot a turtle currently has selected

Started by phaedryx, 17 February 2013 - 03:13 AM
phaedryx #1
Posted 17 February 2013 - 04:13 AM
I've looked over the api http://computercraft...iki/Turtle_(API) and I can see how to select a slot, but not how to later know what the currently selected slot is. I've randomly tried things like turtle.getSelected() and turtle.slotNum, but haven't figured it out :/

How can I get the slot number?
Lyqyd #2
Posted 17 February 2013 - 04:45 AM
Split into new topic.

You cannot get that information. You will either need to override the select function to keep track of it, or simply set the correct slot each time the slot could have changed unexpectedly.
phaedryx #3
Posted 17 February 2013 - 05:15 AM
Seems odd to have a setter without a getter.

Hmm, I guess what I really want is something like this:

function foo(bar, baz)
– store current slot
– do my awesome thing involving different slots
– be a good citizen and restore current slot to what it was before
end

but that sounds like it is impossible?

I'm new to lua, but is it possible for me to add something like this:

function turtle.getSelected()
return turtle.internalVariableKeepingTrackOfCurrentSlot
end

?
Dlcruz129 #4
Posted 17 February 2013 - 05:23 AM
You could overwrite the turtle.select() function.

 local oldSelect = turtle.select
local selected
function turtle.select(slot)
  selected = slot
  oldSelect(slot)
end

function turtle.getSelectedSlot()
  return selected
end
phaedryx #5
Posted 17 February 2013 - 05:41 AM
@Lyqyd Thanks for your help

@Dlcruz129 I see, save the original and use it in the new version. Thanks, that's awesome.