I'm a little confused. I have a routine that loads an object from a file (using WWW and yield), and after it's loaded, I want to modify the game object that it loads into.
What's the best way to do this?
What I have now looks something like this:
void MakeObjectFromFile (url)
{
GameObject newObj = GetObjectViaWWW (url); // returns a gameobject with the thing in it
newObject.GetComponent(Collider).isTrigger = true;
}
GameObject GetObjectViaWWW (url)
{
make a game object, various setup
StartCoroutine(WaitForResponse(www));
}
public IEnumerator WaitForResponse(WWW www)
{
yield return www;
// check for errors
if (www.error == null)
etc.
}
If I understand correctly, GetObjectViaWWW will return immediately, so GetComonent(Collider) will probably fail because the coroutine is still just getting started.
I don't really want to do the post-processing in the WaitForResponse routine. Mainly, I haven't found a coding pattern I like for that, if one exists.
So is there one? Is there a preferred clean way of 'waiting for the operation to complete' when it's called in a subroutine like this? Should I set a semaphore and check it in an Update function or something?
It feels like I want to use a callback function. Which is sorta what a coroutine is, right? Can I pass a function into this somehow, essentially a callback from a callback?