Jump to content




Problem With Coroutine


  • You cannot reply to this topic
5 replies to this topic

#1 hasunwoo

  • Members
  • 16 posts

Posted 27 July 2013 - 08: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()


#2 Apfeldstrudel

  • Members
  • 161 posts

Posted 27 July 2013 - 11:48 AM

Code tags? :D

#3 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 27 July 2013 - 11:56 AM

View PostXyexs, on 27 July 2013 - 11:48 AM, said:

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))


#4 hasunwoo

  • Members
  • 16 posts

Posted 27 July 2013 - 06:26 PM

View PostLBPHacker, on 27 July 2013 - 11:56 AM, said:

View PostXyexs, on 27 July 2013 - 11:48 AM, said:

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.

#5 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 27 July 2013 - 08:45 PM

View Posthasunwoo, on 27 July 2013 - 06:26 PM, said:

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.

#6 hasunwoo

  • Members
  • 16 posts

Posted 28 July 2013 - 10:15 AM

View PostBubba, on 27 July 2013 - 08:45 PM, said:

View Posthasunwoo, on 27 July 2013 - 06:26 PM, said:

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!!!!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users