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

[Lua][Question] How can I have a Turtle stop waiting for user input on a redstone signal?

Started by Eunomiac, 14 April 2013 - 04:03 AM
Eunomiac #1
Posted 14 April 2013 - 06:03 AM
(My first post, hopefully I've done everything right – please let me know if I can make future posts clearer, and thanks in advance for your help!)

I have some turtles set up in an XP farm, where they attack and collect XP orbs (using the XP Upgrade from MiscPeripherals). I'd like them to switch between two modes, controlled by the color of RedPower bundled cable input:
  • While Bundled Input == Red: "Auto-Enchant Mode" — Continuously use the XP they've collected to enchant books whenever they hit level 30.
  • While Bundled Input == Blue: "Manual Enchant Mode" — Hold onto their XP until I come along and stick an item in Slot 2, then tell them (via "level = tonumber(read())") what level I want them to enchant it to. Upon receiving my input, they enchant the item I've put in Slot 2 accordingly.
My problem is, because the turtles stop at "level = tonumber(read())" and wait indefinitely for my input, I can't get the turtles to change back to Auto-Enchant Mode (since they aren't getting to the part of the loop that checks redstone inputs).

So, is there a way to have turtles "break" out of waiting for user input upon receiving a redstone event (i.e. when the blue signal switches to red, they stop waiting and bounce back to Auto-Enchant Mode)? Perhaps a way to run two programs in parallel, where I could have one simply watching redstone inputs and "directing" the flow of the second program accordingly?
Engineer #2
Posted 14 April 2013 - 06:11 AM
You want them to wait for a signal to wait for user input. The easiest way for this is with using os.pullEvent()

Replace this with the part where it will wait for user input"

while true do
  local evt = {os.pullEvent()} -- Store all the params of os.pullEvent() in a table
  if evt[1] == "key" and evt[2] == keys.enter then
     input = tonumber(read())
     break
  elseif evt[1] == "redstone" then
     -- switch to auto enchant
     break
  end
end

Be sure to ask questions if you dont understand this code!