93 posts
Posted 29 March 2014 - 10:41 PM
Sorry for the second post within ten minutes :P/>
I'm making a cell phone, and that last question was for the contacts aspect of the phone. This time, I need to be constantly receiving messages, while still being able to do the other things of the phone (List contacts, add them, remove them, view them, view messages, send messages, delete messages, compose messages)
I'm thinking the term for what I need is "corountines", but I don't know how to use those, what they are (To the full extent), or if it's even what I need. Please help :(/>
Also, this is completely different, I just don't want to have to create another thread, but I'm (trying to) create the messaging aspect for the phone, and I need to know how to tell if the message was sent by me, or from the other person. I know I could just alternate back and forth, me to them to me to them, but that isn't always the case. It could be me twice, then they send me 4 messages, then I send 1, so I need to know how to do this. Um, if you don't mind, pros, could I give you my current code, and politely request the full messenger be implemented? I know this isn't exactly what this section is for, but I have no idea how to create messengers, and have them work.
Edited on 29 March 2014 - 11:09 PM
1281 posts
Posted 29 March 2014 - 11:59 PM
You don't need coroutines for this, you need events obviously. How exactly do you expect to handle user input without?
http://computercraft.info/wiki/Os.pullEvent Also, this is completely different, I just don't want to have to create another thread, but I'm (trying to) create the messaging aspect for the phone, and I need to know how to tell if the message was sent by me, or from the other person. I know I could just alternate back and forth, me to them to me to them, but that isn't always the case. It could be me twice, then they send me 4 messages, then I send 1, so I need to know how to do this. Um, if you don't mind pros, could I give you my current code, and politely request the full messenger be implemented? I know this isn't exactly what this section is for, but I have no idea how to create messengers, and have them work.
Seriously… wat? Do you intend to send messages to yourself here or what? If you send a message, then obviously that message is from you and you should know as much. If you receive a message, it's not from you.
797 posts
Posted 30 March 2014 - 12:03 AM
It would probably be easier for you to use the parallel API rather than coroutines:
parallel.waitForAny( function1, function2 )
i.e. you could have:
parallel.waitForAny( myProgram, receiveMessages )
They would both run at the same time, so you could get messages and run your program together.
As for writing code for you, I don't think anybody will want to do that, but it's always a good idea to post your code so people can offer better advice.
You can also look at the wiki of the parallel API (
http://computercraft.../Parallel_(API))
Hope this helped :)/>
Edited on 29 March 2014 - 11:03 PM
93 posts
Posted 30 March 2014 - 12:07 AM
Okay here's why I say coroutines: there is an ENTIRE programming running, switching functions and what not, while it's constantly receiving rednet messages. If I use parallel, then after one of the functions finished running, it would break out of it. Trust me, my favorite API is the parallel API. :)/> That's why I needed to know this. Also, I need to know who sent the message because I'm printing it differently than you might think. But I think I have an idea in how to do this.
Edited on 29 March 2014 - 11:08 PM
1281 posts
Posted 30 March 2014 - 12:32 AM
seems to me you have no idea how your favorite API works…
local tMessages = {}
parallel.waitForAll(
function()
while true do
if #tMessages > 1 then
local message = table.remove(tMessage,1)
print("OMG WE GOT A MESSAGE: "..message)
end
--other program functionality here
end
end,
function()
while true do
_derp,tMessages[#tMessages+1] = rednet.receive()
end
end
)
But using parallel to handle a rednet messages while you're already handling user input defeats the whole point. You might aswell handle them together using events.
93 posts
Posted 30 March 2014 - 03:09 AM
Okay, I don't think you know what I want here. I want to able to use the whole entire program, which includes app change, while listening for messages, and recording them for later. So while I'm browsing my program's contacts, which is a phone for pocket computers, I need to be able to receive messages. I also need to be able to receive messages when I quit that and go to another program. Sure I could call parallel every time I use a different function, but that's stupid. And parallel .wairForAll requires both functions to break. I'm just gonna learn how to use coroutines on my own.
Okay, I might use parallel but I will have to figure out a way to modify how it works for what I want…
Edited by
1610 posts
Posted 30 March 2014 - 03:12 AM
Okay, I don't think you know what I want here. I want to able to use the whole entire program, which includes app change, while listening for messages, and recording them for later. So while I'm browsing my program's contacts, which is a phone for pocket computers, I need to be able to receive messages. I also need to be able to receive messages when I quit that and go to another program. Sure I could call parallel every time I use a different function, but that's stupid. And parallel .wairForAll requires both functions to break. I'm just gonna learn how to use coroutines on my own.
Okay, I might use parallel but I will have to figure out a way to modify how it works for what I want…
Blah.
parallel.waitForAny(rednetMessageHandler,loadfile("any program"))
93 posts
Posted 30 March 2014 - 03:28 AM
Okay, I don't think you know what I want here. I want to able to use the whole entire program, which includes app change, while listening for messages, and recording them for later. So while I'm browsing my program's contacts, which is a phone for pocket computers, I need to be able to receive messages. I also need to be able to receive messages when I quit that and go to another program. Sure I could call parallel every time I use a different function, but that's stupid. And parallel .wairForAll requires both functions to break. I'm just gonna learn how to use coroutines on my own.
Okay, I might use parallel but I will have to figure out a way to modify how it works for what I want…
Blah.
parallel.waitForAny(rednetMessageHandler,loadfile("any program"))
I had no idea you could do that! I know some of the standalone functions like error() and tonumber(string), but I didn't know there was a loadfile! Now I have a question for you: What would happen when that file was loaded? Wouldn't it tell the parallel that it has successfully completed a task? Or would it wait until that program was terminated or quit before telling parallel that it is finished? Anyway, I will be using this for my task.
I have one final request: Could someone teach me how to use coroutines? I would like to know how they work, since they seem pretty cool and I want to try and expand my knowledge of computercraft. That's one of the reasons why I was trying to stay away from parallel that way I could use coroutines and learn. Thank you in advance :)/>
Edited by
1281 posts
Posted 30 March 2014 - 03:51 AM
You don't get what im saying at all here. There's no need to "break" any of the functions in the parallel. You would have one function which controls what "app" is currently running, and one to handle rednet. If you intend to use craftOs while reciving rednet messages you'll have to overwrite pullEvent or run the shell in parallel with a rednet handler.
Loadfile returns the file as a function, provided there were no compilation errors. parallel dosen't take function calls, it takes function pointers. Meaning that if you used tonumber(50), it would try to execute 50 in parallel, not tonumber(50). This would obviously error though :P/>
As for coroutines, go read up on them and try them out. Ask if you have something specific you can't figure out.
Edited by
1610 posts
Posted 30 March 2014 - 01:44 PM
Okay, I don't think you know what I want here. I want to able to use the whole entire program, which includes app change, while listening for messages, and recording them for later. So while I'm browsing my program's contacts, which is a phone for pocket computers, I need to be able to receive messages. I also need to be able to receive messages when I quit that and go to another program. Sure I could call parallel every time I use a different function, but that's stupid. And parallel .wairForAll requires both functions to break. I'm just gonna learn how to use coroutines on my own.
Okay, I might use parallel but I will have to figure out a way to modify how it works for what I want…
Blah.
parallel.waitForAny(rednetMessageHandler,loadfile("any program"))
I had no idea you could do that! I know some of the standalone functions like error() and tonumber(string), but I didn't know there was a loadfile! Now I have a question for you: What would happen when that file was loaded? Wouldn't it tell the parallel that it has successfully completed a task? Or would it wait until that program was terminated or quit before telling parallel that it is finished? Anyway, I will be using this for my task.
I have one final request: Could someone teach me how to use coroutines? I would like to know how they work, since they seem pretty cool and I want to try and expand my knowledge of computercraft. That's one of the reasons why I was trying to stay away from parallel that way I could use coroutines and learn. Thank you in advance :)/>
loadfile essentially turns a file into a single function which can be run later. Since parallel.waitForAny takes a function, we can use loadfile to add a program into the loop. It will not exit prematurely or anything as loadfile simply loads it as a function, not calling the function.
Edited on 30 March 2014 - 11:46 AM
7508 posts
Location
Australia
Posted 30 March 2014 - 01:48 PM
loadfile essentially turns a file into a single function which can be run later. Since parallel.waitForAny takes a function, we can use loadfile to add a program into the loop. It will not exit prematurely or anything as loadfile simply loads it as a function, not calling the function.
really
shell.run or
os.run are better though, as
loadfile does not set the environment for the program
1610 posts
Posted 30 March 2014 - 02:05 PM
loadfile essentially turns a file into a single function which can be run later. Since parallel.waitForAny takes a function, we can use loadfile to add a program into the loop. It will not exit prematurely or anything as loadfile simply loads it as a function, not calling the function.
really
shell.run or
os.run are better though, as
loadfile does not set the environment for the program
True, but sometimes I prefer sharing the standard environment. You would also have to manually wrap the function with os.run.
93 posts
Posted 30 March 2014 - 03:10 PM
I know how to wrap, I had to learn this for my little RedOS project I have in my signature :P/>
Basically it's:
parallel.waitForAny(rednetMessageHandler,function() loadfile("any program") end)
1281 posts
Posted 30 March 2014 - 03:24 PM
His point was that you wouldn't have to define loadfile in an anonymous function, hell what you just posted wouldn't even call the loaded function… Using waitForAny would probably also cause some unwanted behavior.