Suterusu 1 Posted September 18, 2014 Share Posted September 18, 2014 Can anyone provide basic code usage of a Coroutine? I want to be able to call even.pull("modem"), but at the same time, do other stuff. Quote Link to post Share on other sites
0 Solution Wobbo 8 Posted September 19, 2014 Solution Share Posted September 19, 2014 Instead of coroutines, you might want to use event.listen. You can find information about event.listen on the OC wiki. MOD EDIT: Wiki link http://ocdoc.cil.li Quote Link to post Share on other sites
0 Suterusu 1 Posted September 20, 2014 Author Share Posted September 20, 2014 I noticed on the wiki this part here: Important: If you use event handling in driver mode, then event functions will be called from the environment of the OS event handler and variables local to your program will not be visible! Do you have any code examples that use event.listen? Quote Link to post Share on other sites
0 Wobbo 8 Posted September 20, 2014 Share Posted September 20, 2014 You won't have to worry about driver mode I guess (someone correct me if I'm wrong). At the end of the basic usage section on the wiki there is an example of using event.listen. It refers back to the example above. Quote Link to post Share on other sites
0 Suterusu 1 Posted September 21, 2014 Author Share Posted September 21, 2014 Thanks, I doubt I am using this correctly, but the event.listen() working as a "background" process if you will, works for what I need. Quote Link to post Share on other sites
0 Molinko 43 Posted September 22, 2014 Share Posted September 22, 2014 Thanks, I doubt I am using this correctly, but the event.listen() working as a "background" process if you will, works for what I need. Wobbo makes a point. Lua has no real parallelism. But, post some code. I like seeing what others are making Quote Link to post Share on other sites
0 Suterusu 1 Posted September 22, 2014 Author Share Posted September 22, 2014 So far I have ~450 lines... I am trying to write a "cell network" program to be able to send messages between nodes on a "tree" structure". I have all the logic for "routing" messages, etc. the part I am trying to deal with is the psudo concurrency of trying to send/recieve. Using event.listen() as a "background" listener has worked, sort of. I've been able to send and echo back messages from computers connected to different cell towers over my network using hop-by-hop of nodes with modem.send(). What I am trying to do with event.listen() is this (calling a function directly instead of an event handler): event.listen("modem_message", listen) function listen(msg, localAddress, remoteAddress, port, distance, data) local payload = serialization.unserialize(data) --more code here end That works fine, I can do other "stuff" in code, and when an event type "modem_message" is received, it calls listen() and passes it all the parameters. What I am stuck on however, is if another function wants to send, "modem.send()" to another host, the listen() function will recieve the reply. I don't want to call event.pull() within the function that used modem.send() in order to "wait" for a reply back because it will block and prevent further code execution. function nameLookup(name) local querry = {["type"] = "querry", ["name"] = name} sendData(querry, nameServer) --more code end function sendData(data, destination) local data = serialization.serialize(data) local payload = {["dstAddress"] = destination, ["srcAddress"] = myAddress, ["data"] = data} m.send(rootPathList[1], 16, serialization.serialize({["msgType"] = "data", ["payload"] = payload})) end So, from within nameLookup() we should be getting the reply data there, the stuff listen() gets when called by event.listen(), so we can parse it and return the function to the calling function. Quote Link to post Share on other sites
0 Molinko 43 Posted September 22, 2014 Share Posted September 22, 2014 Hmmm. That is a problem huh.. The first idea I have would actually involve coroutines like you suggested so sorry for not answering your initial question directly.. Didnt mean to make you go through hoops for help.. It seems like you need a call and response stack of some sort to manage connections. Here is what I know about coroutines in lua.. -- coroutines are made with functions like so local co = coroutine.create( function() print( "doing stuff" ) end ) coroutine.resume( co ) --> "doing stuff" coroutine.resume( co ) --> "error: cannot resume a dead coroutine" --shit.. --[[ basically the coroutine will only live as long as the function that it was created from still has code to execute. The first call to resume ran the function and now the function doesnt have anything else to run because it essentially returned nil. ]] --If you want a coroutine to constantly run then your function will have to do the same and handle that. co = coroutine.create( function( ... ) local var = { ... }[1] -- just to try some stuff with while true do print( "doing stuff" ) var = var + 1 -- here we can send data back to the caller of the coroutine via coroutine.yield -- also the caller can inject data back into to the coroutine with the subsequent call to resume var = coroutine.yield( var ) end end ) local var = coroutine.resume( co, 5 ) --> "doing stuff" var = 6 now.. print(var) local var = coroutine.resume( co, var*2 ) --> "doing stuff" var = 13 now... --[[The first time we resume a coroutine it will be passed the arguments and all the subsequent calls to resume will fall into our loop and be "returned" via our call to yield. Keep in mind we can yield all we want in our function.]] The lua PIL on coroutines has done a way better job of explaining this the I have. But the best thing you can do is just experiment with some really simple coroutines until you understand the potential of how powerful both resume and yield can be. Quote Link to post Share on other sites
0 Heiramus 1 Posted September 23, 2014 Share Posted September 23, 2014 Maybe you can try something like this: function listen() event.ignore("modem_message",listen) -- Do your thing event.listen("modem_message",listen) end Quote Link to post Share on other sites
0 Ketzak 0 Posted March 11, 2017 Share Posted March 11, 2017 Did you ever get this working? I'm trying to write a similar program. Rather, I'm trying to rewrite a program I wrote in Computer Craft that used coroutines to monitor redstone inputs and send them to a server, while updating outputs based on data from a server. Every way I try to implement it results in the computer eventually failing out for not yielding. Quote Link to post Share on other sites
0 Molinko 43 Posted March 11, 2017 Share Posted March 11, 2017 3 hours ago, Ketzak said: Did you ever get this working? I'm trying to write a similar program. Rather, I'm trying to rewrite a program I wrote in Computer Craft that used coroutines to monitor redstone inputs and send them to a server, while updating outputs based on data from a server. Every way I try to implement it results in the computer eventually failing out for not yielding. Tis a really old post.. If you're seeking help on a particular piece of code try making a new topic in Programming/Support Quote Link to post Share on other sites
0 payonel 21 Posted June 28, 2017 Share Posted June 28, 2017 Also see the new thread library which can solve the "I need to event.pull without blocking other parts of my code": http://ocdoc.cil.li/api:thread Quote Link to post Share on other sites
Can anyone provide basic code usage of a Coroutine?
I want to be able to call even.pull("modem"), but at the same time, do other stuff.
Link to post
Share on other sites