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

Crash when attempting to loop code.

Started by Talik13, 01 September 2013 - 07:07 PM
Talik13 #1
Posted 01 September 2013 - 09:07 PM
Hello!

So my problem here is that I can't get my computer to loop continuously without giving me some kind of error. I want the program to continuously run because it's looking to execute whenever it gets redstone input (its for track switching and minecart management).

I've looked over the internet to see what others have done and I still haven't gotten anywhere. I was hoping someone with more experience in Lua or specifically Computercraft Mod would know how to set up the base code so the computer will constantly look for redstone input before executing a program (or have a program that continuously loops while looking for redstone input) without crashing.

Thanks!!
Lyqyd #2
Posted 01 September 2013 - 09:43 PM
Split into new topic.

Please post your full code so that we can diagnose the issue.
Inumel #3
Posted 01 September 2013 - 10:20 PM
Im just going to take a stab in the dark here, as Lyqyd said you have posted no code. But if you are looping continuously checking for a signal, IE

while true do
  x = rs.getInput()
end

Then you should switch to

local event = os.pullEvent("redstone")

It pauses the computer and waits for a signal change on any side, While its not as precise as the former method, it doesn't cause a too long without yielding error..
GopherAtl #4
Posted 01 September 2013 - 11:27 PM
how is it not as precise? It suspends the program until a redstone event occurs, and then resumes, generally on the next server tick.

CC doesn't allow program to actually run continuously, because hundreds of computers in a world doing so would quickly bog down even the fastest servers. That's what the "failure to yield" error is about. Programs should be event driven, by using os.pullEvent to wait for events. In this case, the only event you care about is redstone inputs, so you listen for "redstone" events, by putting the pullEvent line Elgriton gave you at the start of your loop. Inside the loop, just after your "while true."
Inumel #5
Posted 02 September 2013 - 08:09 AM
how is it not as precise? It suspends the program until a redstone event occurs, and then resumes, generally on the next server tick.

I simply meant it doesn't tell you which side was changed to what state, but of course you can fix that with a bit of code.. Perhaps precise wasn't the correct word there.