Jump to content




Help with Coroutines


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

#1 tommykent1210

  • New Members
  • 17 posts

Posted 02 August 2012 - 05:25 PM

I'm fairly new to LUA, and I'm making my own OS. It's fairly simple right now, but I want to make it more complex over time. The one thing I REALLY working is coroutines, but I'm not entirely sure how to do them.


Basically, I have a function that saves all events from os.pullEventRaw() to a table (acting kind of like a stack). This is so that if any addon programs written by other decide to use sleep() then it doesnt mess up all of the events :ph34r:/>

I then have another function, delegateEvents(), that takes one event form the stack and handles it.


Within delegateEvents() I want to create a new thread, and start it. This thread will be whatever function delegateEvents() delegates to handle the event (whether it is rednet, key, char etc...). The function delegated should hopefully run it's course and then close itself. Is this possible in LUA?

#2 BigSHinyToys

  • Members
  • 1,001 posts

Posted 02 August 2012 - 06:16 PM

I don't know if this will be of any assistance. It is something along the lines of what you are trying to make But a bit complex and badly written.
http://pastebin.com/DxdiFF6R
In this you can see that i have several programs running at the same time doing different thinks It might be a bit hard to understand.

I never meant to release this code it is conceded scrap to me so I declare the above linked code OPEN SOURCE.
basically use it as you see fix.

#3 tommykent1210

  • New Members
  • 17 posts

Posted 02 August 2012 - 06:57 PM

What exactly does coroutine.yeild() do? Because it looks as if you are using it like OS.getEventRaw() :s

#4 BigSHinyToys

  • Members
  • 1,001 posts

Posted 02 August 2012 - 07:01 PM

View Posttommykent1210, on 02 August 2012 - 06:57 PM, said:

What exactly does coroutine.yeild() do? Because it looks as if you are using it like OS.getEventRaw() :s
t
Yes it is basically that this is a quote from the BIOS file
function os.pullEventRaw( _sFilter )
return coroutine.yield( _sFilter )
end
function os.pullEvent( _sFilter )
local event, p1, p2, p3, p4, p5 = os.pullEventRaw( _sFilter )
if event == "terminate" then
  print( "Terminated" )
  error()
end
return event, p1, p2, p3, p4, p5
end
[edit] it passed back control so other program can run then you send it events to carry into that program.

#5 BigSHinyToys

  • Members
  • 1,001 posts

Posted 02 August 2012 - 07:03 PM

View PostBigSHinyToys, on 02 August 2012 - 07:01 PM, said:

View Posttommykent1210, on 02 August 2012 - 06:57 PM, said:

What exactly does coroutine.yeild() do? Because it looks as if you are using it like OS.getEventRaw() :s
Yes it is basically that this is.Here a quote from the BIOS file
function os.pullEventRaw( _sFilter )
return coroutine.yield( _sFilter )
end
function os.pullEvent( _sFilter )
local event, p1, p2, p3, p4, p5 = os.pullEventRaw( _sFilter )
if event == "terminate" then
  print( "Terminated" )
  error()
end
return event, p1, p2, p3, p4, p5
end

[edit] it passed back control so other program can run then you send it events to carry into that program.

[This was a failed attempt at an edit Sorry forums moderator please remove this post]





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users