C#, coroutines, WWW, return values question

Let’s say I have a C# function

IEnumerator Start () {
  //go fetch some data, and build an array of it
  ...
  //now, do something interesting with each 
  foreach(info in array){
    GameObject obj = create_go_from_data(info);
    // pass the constructed GO with sendmsg
    mything.SendMessage("ImportantFunction", obj);
   }

  GameObject create_go_from_data(DataClass info){
     GameObj obj = new GameObject();
     // load up a texture
     obj.MyTexture = load_texture(info.texture_id());
    // now, return our obj
    return obj;
  }

  Texture load_texture(string id){
    //check to see if we have this texture, and if not, load from web.
    return tex;
  }
}

Now, I can’t get something like the above to work because there is no synchronous loading from web (GetURL is deprecated and returns a string.) The above also doesn’t work because my load_texture method is supposed to return a texture, not an IEnumerable. Am I missing something here? How would you accomplish this? Are GameObjects passed by reference or value?

i think i can get around the inability to return what i need by storing the destination object in the call stack of the load_texture function, but then i think the for… loop will kick off each of its iterations before the load_texture is done. i don’t want this to happen, i want each one of the loop iterations to be blocking on each www request.

Thanks,

Aaron

Almost everything is passed by reference. Simple things like numbers and even strings are passed by value (if you want to pass them by reference, wrap them in an array or class).

So you can pass the GameObject into your create_go_from_data function. That way it can have the return type IEnumerator.

If you want to wait for it to finish, use something like:

yield return StartCoroutine( create_go_from_data( info, go ) );

Thanks for the clarification!