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

Help with Coroutines

Started by tommykent1210, 02 August 2012 - 04:25 PM
tommykent1210 #1
Posted 02 August 2012 - 06: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?
BigSHinyToys #2
Posted 02 August 2012 - 07: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.
tommykent1210 #3
Posted 02 August 2012 - 07:57 PM
What exactly does coroutine.yeild() do? Because it looks as if you are using it like OS.getEventRaw() :s
BigSHinyToys #4
Posted 02 August 2012 - 08:01 PM
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.
BigSHinyToys #5
Posted 02 August 2012 - 08:03 PM
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]