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

Error

Started by MarcelW, 25 March 2017 - 10:20 PM
MarcelW #1
Posted 25 March 2017 - 11:20 PM
Hello all,
i have a Problem with my computer.
I become the following Error:

startup: 4: Too long without yielding

Thats my code:

local rs = redstone
while true do
if rs.getInput("right") == true then
sleep(5)
rs.setOutput("left", true)
sleep(2)
rs.setOutput("left", false)
end
end

Sorry for that bad englisch. I Speak German.
Bomb Bloke #2
Posted 26 March 2017 - 12:18 AM
Your script must yield ("pause", basically) at least once every ten seconds or ComputerCraft will terminate it.

The usual way to do this is to call os.pullEvent(), or to call a function that does it for you, like sleep(). Your script does call sleep(), but only if there is a redstone signal on the right - the rest of the time your loop runs flat-out without ever yielding.

We can have it yield until redstone state changes occur by waiting for redstone events:

while true do
	if rs.getInput("right") then
		sleep(5)
		rs.setOutput("left", true)
		sleep(2)
		rs.setOutput("left", false)
	end
	
	while not rs.getInput("right") do os.pullEvent("redstone") end  -- Yield until redstone turns on again.
end

Note that "rs" is already equal to "redstone" by default. You don't need to manually assign it.