- Home /
Yield to wait for delegate call
Hi all
I am writing a networked application where hierarchies of objects are synchronized between client and server. Individual objects are sent from the server to the client on demand.
I have a number of places in my application where I call a method (e.g. requestObject(id)) which will return it's result via a delegate (e.g. respondDataDelegate). This is to get around the 1-directional limitation of RPCs.
Is it possible for me to write some kind of requestObjectImmediate(id) method that calls the request method, waits for the response delegate, and returns the result? I presume this would use the "yield" keyword somewhere, but other than that I'm not sure where to start.
Can anyone offer any suggestions?
Thanks,
Ves
Answer by karljj1 · Dec 02, 2013 at 07:08 PM
You can run coroutines within each other.
E.G
IEnumerator DoSomething()
{
// Do stuff
yield StartCoroutine( Request( 123 ) ); // Wait for Request to finish
// Do some more stuff
}
IEnumerator Request( int id )
{
// doing some stuff
}
Hi $$anonymous$$arljj1
The problem is the request finishes immediately, as all it is doing is calling an RPC. What I need is some kind of mechanism that can make the request and wait for the response, all within the same method.
Not sure if it can be done in one method but you could do it in 2. Have a flag in your request function that gets set when you get a response. $$anonymous$$G
IEnumerator Request( int id )
{
gotResponse = false;
// SEND RPC
while( !gotResponse )
{
yield return 0; // Wait
}
}
[RPC]
void Response()
{
gotResponse = true;
}