←  Ask a Pro

ComputerCraft | Programmable Computers for Minecraft

»

Too Long Without Yielding

Abstract's Photo Abstract 22 Feb 2012

I need some help with my program. What it should do is,output redstone to the left, if redstone comes from the front, stop outputting redstone to the right wait and reboot. this is my code:
startup:
while true do
shell.run"redside"
end
redside:
redstone.setOutput("right", true)
if redstone.getInput("front", true)
then redstone.setOutput("right", false)
sleep(10)
os.restart()
end
but if I reboot now, it works for only a couple of seconds and a error pops up which says 'Too long without yielding'. What did I do wrong?
Quote

luza's Photo luza 22 Feb 2012

you forgot some brackets.

while true do
shell.run("redside")
end
Quote

Casper7526's Photo Casper7526 22 Feb 2012

When your code just loops endlessly without yielding for a few seconds, you receive that error.

You should use a pullevent to only check when redstone is changed instead.

startup:
redstone.setOutput("right", true)
while true do
event, param = os.pullEvent()
   if event == "redstone" and redstone.getInput("front") == true then
   redstone.setOutput("right", false)
   sleep(10)
   redstone.setOutput("right", true)
   end
end
Quote

Abstract's Photo Abstract 22 Feb 2012

View PostCasper7526, on 22 February 2012 - 03:28 PM, said:

When your code just loops endlessly without yielding for a few seconds, you receive that error.

You should use a pullevent to only check when redstone is changed instead.

startup:
redstone.setOutput("right", true)
while true do
event, param = os.pullEvent()
   if event == "redstone" and redstone.getInput("front") == true then
   redstone.setOutput("right", false)
   sleep(10)
   redstone.setOutput("right", true)
   end
end
[string "redside"]:4: 'then' expected
Don't know why I've typed everything like you
EDIT: works, had a spelling mistake <_</>
Quote