I'm trying to make a rednet messaging program that can receive messages at all times; can someone show me how to write a function that operates in the background?
Background Message Receiving
Started by ProjectXMark1, Nov 11 2012 06:18 AM
4 replies to this topic
#1
Posted 11 November 2012 - 06:18 AM
#2
Posted 11 November 2012 - 07:42 AM
Here's an example for how to use the parallel API.
This program will listen for events while you are "stuck" in the read() of inputFunc():
What inputFunc() does:
parallel.waitForAny basically means: "Wait for any of these functions to yield/return"
parallel.waitForAll basically means: "Wait for all of these functions to yield/return"
If, for example, you'd change the code above to use waitForAll, then after entering "exit" the program will still be running eventListener().
That is because waitForAll is still waiting for it to yield/return.
So you still have to press END to completely exit out of the program.
And vice versa, i.e. if you first press END, then you'd still have to enter "exit" to end the program completely.
Another thing to keep in mind is that you only pass the function names without brackets!
Otherwise the functions would first be run and their return value be passed to parallel.waitForAny().
But we want to pass the references to the functions, not their return values! Like so:
parallel.waitForAny( inputFunc, eventListener )
This program will listen for events while you are "stuck" in the read() of inputFunc():
Spoiler
What inputFunc() does:
- Returns from the function if "exit" is entered.
- Marks the location of an entered space, whenever the space key is pressed.
- Returns from the function if the END key is pressed.
parallel.waitForAny basically means: "Wait for any of these functions to yield/return"
parallel.waitForAll basically means: "Wait for all of these functions to yield/return"
If, for example, you'd change the code above to use waitForAll, then after entering "exit" the program will still be running eventListener().
That is because waitForAll is still waiting for it to yield/return.
So you still have to press END to completely exit out of the program.
And vice versa, i.e. if you first press END, then you'd still have to enter "exit" to end the program completely.
Another thing to keep in mind is that you only pass the function names without brackets!
Otherwise the functions would first be run and their return value be passed to parallel.waitForAny().
But we want to pass the references to the functions, not their return values! Like so:
parallel.waitForAny( inputFunc, eventListener )
#3
Posted 11 November 2012 - 08:17 PM
Espen you just fixed my entire program. Thanks!
#4
Posted 18 November 2012 - 06:53 AM
Is the "--Start coroutines" bit just calling the two functions? Can I run a function inside the function or call the function again without yielding the parent function?
Just checking.
Just checking.
#5
Posted 18 November 2012 - 07:56 AM
ProjectXMark1, on 18 November 2012 - 06:53 AM, said:
Is the "--Start coroutines" bit just calling the two functions? Can I run a function inside the function or call the function again without yielding the parent function?
Just checking.
Just checking.
For example, if you'd call inputFunc() from within itself, then when you return from it the first time, you'll land back in the inputFunc() at the point where it called itself. And only if THAT returns now, does it yield.
Every recursive call is nested deeper and deeper. It's like a Matryoshka doll, i.e. everytime you let inputFunc() recursively call itself, you open the next layer of the doll, and on and on.
Now if you want to come back to the outermost layer, you first have to close the last one opened (return), then the second-last, etc. ... until you're at the first layer.
If you return from that as well, then you're finally yielding.
Hope this wasn't more complicated that I set it out to be. I'm not feeling the explain-flow today.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











