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

[Lua] Too long without yielding and coding help.

Started by andrew28, 24 March 2014 - 11:32 PM
andrew28 #1
Posted 25 March 2014 - 12:32 AM
So, I've been trying to make a code that receives a pulse of redstone from the back and then pulses front for 28 seconds then resets. But it says too long without yielding
Heres my code:

while true do
local coins = redstone.getInput("back")
if coins == true then
rs.setOutput("front", true)
sleep(28)
rs.setOutput("front", false)
local coins = false
end
end
Any solution, if yes please respond with a code!
Lyqyd #2
Posted 25 March 2014 - 12:37 AM
So, your code only really needs to run through the loop every time a redstone input changes. You could throw an os.pullEvent("redstone") at the beginning. A working version that replicates the behavior of the above code would be:


while true do
  while rs.getInput("back") do
    rs.setOutput("front", true)
    sleep(28)
  end
  rs.setOutput("front", false)
  os.pullEvent("redstone")
end

Of course, I don't think that that's entirely what you want it to do, but fixing the behavior of the code is an exercise left to the reader.
andrew28 #3
Posted 25 March 2014 - 01:17 PM
One more thing, if there are multiple redstone inputs, can the time be extended maybe like sleep(tokens*28)
Bomb Bloke #4
Posted 25 March 2014 - 02:54 PM
Say you defined a variable as 0, eg "sleepTime", every time the loop repeats. For each input that's active, you add 28 to it. After checking each input, if the variable is more than 0 you enable the output and run "sleep(sleepTime)".