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

Problem With Coroutine

Started by hasunwoo, 27 July 2013 - 07:24 AM
hasunwoo #1
Posted 27 July 2013 - 09:24 AM
i made redstone pulser program with lua but it doesn't show me anything help me and i am first with coroutine please fix this program it is really simple program
http://pastebin.com/m3C72S8d
  • function TerminalHandler()
  • while true do
  • local interval12 = loaddata("interval")
  • write(interval12)
  • write("interval for timer : ")
  • local interval = read()
  • local interval1 = {}
  • interval1[1] = interval
  • savedata(interval1,"interval")
  • end
  • end
  • function timer()
  • while true do
  • local interval20 = loaddata("interval")
  • local interval = interval20[1]
  • sleep(interval)
  • rs.setOutput("left",true)
  • sleep(interval)
  • rs.setOutput("left",false)
  • end
  • end
  • function process()
  • local c1 = coroutine.create(TerminalHandler)
  • local c2 = coroutine.create(timer,interval)
  • local eventData = {}
  • while true do
  • –# resume our routine giving it the event data
  • coroutine.resume(c1, unpack(eventData))
  • if coroutine.status(c1) == "dead" then
  • c1 = coroutine.create(TerminalHandler)
  • end
  • –# resume our routine giving it the event data
  • coroutine.resume(c2, unpack(eventData))
  • if coroutine.status(c2) == "dead" then
  • c2 = coroutine.create(timer)
  • end
  • eventData = { coroutine.yield() } –# you could also use os.pullEventRaw here, but again, it is just a wrapper of coroutine.yield, so why not just use the top level function
  • end
  • end
  • function savedata(table,id)
  • local file = fs.open(id,"w")
  • file.write(textutils.serialize(table))
  • file.close()
  • end
  • function loaddata(id)
  • local file = fs.open(id,"r")
  • local data = file.readAll()
  • file.close()
  • return textutils.unserialize(data)
  • end
  • process()
Apfeldstrudel #2
Posted 27 July 2013 - 12:48 PM
Code tags? :D/>
LBPHacker #3
Posted 27 July 2013 - 12:56 PM
Code tags? :D/>
You have eyes, you have a pastebin ID, you could have helped. If you have no idea what happens, don't comment.

The problem is (apart from the hideous indentation) that the program is not failsafe. At first, the file called interval doesn't exist. Both coroutines try to open the nonexistent file, and both of them die because of the error which occurs when they try to call the readAll method of the handle, which is nil. Save the file first if it doesn't exist, you can read it after that.

EDIT: Forgot to mention. interval is a string. sleep accepts only numbers.
sleep(tonumber(interval))
hasunwoo #4
Posted 27 July 2013 - 07:26 PM
Code tags? :D/>/>/>
You have eyes, you have a pastebin ID, you could have helped. If you have no idea what happens, don't comment.

The problem is (apart from the hideous indentation) that the program is not failsafe. At first, the file called interval doesn't exist. Both coroutines try to open the nonexistent file, and both of them die because of the error which occurs when they try to call the readAll method of the handle, which is nil. Save the file first if it doesn't exist, you can read it after that.

EDIT: Forgot to mention. interval is a string. sleep accepts only numbers.
sleep(tonumber(interval))
I really thanks about your help but one question how can i use coroutine.yield() to transfer variable "interval" to coroutine timer() so i can transfer variable easier please help??? Also i need to update "interval" using TerminalHandler() coroutine.
Bubba #5
Posted 27 July 2013 - 09:45 PM
I really thanks about your help but one question how can i use coroutine.yield() to transfer variable "interval" to coroutine timer() so i can transfer variable easier please help??? Also i need to update "interval" using TerminalHandler() coroutine.

I've written a tutorial on just that here. It should help you out considerably.
hasunwoo #6
Posted 28 July 2013 - 11:15 AM
I really thanks about your help but one question how can i use coroutine.yield() to transfer variable "interval" to coroutine timer() so i can transfer variable easier please help??? Also i need to update "interval" using TerminalHandler() coroutine.

I've written a tutorial on just that here. It should help you out considerably.
I really appreciate with this tutorial i fully understanded coroutine!!!!