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

[Question] Which is more efficient: Coroutine or another computer?

Started by TheGeek, 21 December 2012 - 10:29 AM
TheGeek #1
Posted 21 December 2012 - 11:29 AM
I'm working on a very complex project that controls many different computers with rednet messages. There is one command and control computer that all the other computers listen to for commands. This computer uses an os.pullEvent() function, which it waits either for redstone state changes or rednet messages. However, I would like this project to read from a file and send rednet messages based on the contents of the file, and not necessarily have to wait for an os.pullEvent(). I can either:

1. Come up with some convoluted way to set up an event queue to read from the file and send messages, possibly missing a critical rednet message from one of the computers or turtles.
2. Use a Coroutine to read the file and send messages, where I have no experience with coroutines.
3. Use another computer entirely to read from the file and send the rednet messages when the C&C computer sends a ready state.

I'm already using about 16-20 computers already, will one more computer slow things down at all? Or am I just a lazy coder who doesn't want to use proper event handling?
Lyqyd #2
Posted 21 December 2012 - 11:35 AM
Neither is necessary. Simply use a timer event to handle the instructions from the file. You may also need a variable to track the state of these operations, but it should be a relatively simple matter to integrate this extra functionality into your main loop. And I have no idea what sort of file operations you could even perform that would cause you to lose events.
TheGeek #3
Posted 21 December 2012 - 11:52 AM
Neither is necessary. Simply use a timer event to handle the instructions from the file. You may also need a variable to track the state of these operations, but it should be a relatively simple matter to integrate this extra functionality into your main loop. And I have no idea what sort of file operations you could even perform that would cause you to lose events.

Care to give a short code example?

EDIT: Read up on timer, looks like its one shot, IE, i need to reset it on every loop.
EDIT2: Perhaps I could have the file contents set the timerlength, say if I want the instructions in the file to wait some number of seconds before sending the next command. Would this work?
Something like:

timerlength = 0
--code before looping
while true do
if state == "runningfile" then
timerID = os.startTimer(timerlength) --will this work?
end
event, id, message, distance = os.pullEvent()
if event == "rednet_message" then
  --handle rednet
elseif event =="redstone" then
  --handle button presses
elseif event=="timer" and state == "runningfile" then
  --load file lines here?
  if fileline == "waitsomenumberofseconds" then
   timerlength = somenumberofseconds
  else
   timerlength = 0
  end
end
end
Lyqyd #4
Posted 21 December 2012 - 01:02 PM
Yep, that's workable. You'll probably want to load the entire file into a table of individual lines and keep track of which line you're on, so you can simply index the next line in the table each time the timer fires. Don't forget to set the timer again at the end of the timer handler.