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

Press any key to end program help

Started by thatonedinoguy, 24 September 2014 - 03:07 PM
thatonedinoguy #1
Posted 24 September 2014 - 05:07 PM
So I'm trying to test my programming skills and learn some more about how lua works, but I've encountered a snag and I can't find a solution to it. What I'm trying to do is make a test program that once running uses parallel API to run a repeating loop of turtle.turnLeft as well as a bit of code that allows me to stop the turtle once I press any key. My problem is that usually the code will only run turnLeft once or the loop won't run at all, or I won't be able to input a key. Can I get some help solving this?
Kouksi44 #2
Posted 24 September 2014 - 06:51 PM
Post your Code then we can hell you .
KingofGamesYami #3
Posted 24 September 2014 - 06:57 PM
He wants something extremely simple…


local function turn()
  while true do
    turtle.turnLeft()
  end
end

local function onKeyPress()
  while true do
    local event = os.pullEvent()
    if event == "key" then
      return
    end
  end
end
parallel.waitForAny( turn, onKeyPress )
Bomb Bloke #4
Posted 24 September 2014 - 11:45 PM
It's important to note that the above code uses this structure:

parallel.waitForAny( turn, onKeyPress )

… as opposed to this structure:

parallel.waitForAny( turn(), onKeyPress() )

There's a big difference between passing the functions to the parallel API, and passing the results of function calls to the parallel API. The former "works", the latter does not.
thatonedinoguy #5
Posted 25 September 2014 - 05:17 AM
Thank you very, very much KingofGamesYami and Bomb Bloke. You've helped me learn something valuable. I guess you realize that I'm kinda new to all of this, but everyone has their start right? Again, thank you very much.

Post your Code then we can hell you .

I'm not sure I want to be hell'd o_o