Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Create object and return ID with a coroutine

Discussion in 'Scripting' started by leonardjouen, Jan 19, 2020.

  1. leonardjouen

    leonardjouen

    Joined:
    Oct 5, 2018
    Posts:
    32
    Hi everybody,

    For my game, I need to have a function which create a gameobject and return his ID. But I use AssetBundle to load assets and get the gameobject to spawn, so i need to use an async system.
    So i don't know if its possible to wait for a coroutine before returning a value.
    Something like this :

    Code (CSharp):
    1. int CreateObject(string objName)
    2. {
    3.   GameObject obj = StartCoroutine(GetObjectInAssetBundle(objName));
    4.   return obj.GetComponent<ObjData>().id;
    5. }
    But this is not possible like this, coroutine don't return any value. So i don't really know how to do this.
    Thanks for your help!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,717
    Yes, the above approach won't work for Coroutines, as you found.

    What is traditionally done is to supply a delegate / callback that the coroutine will use to report when it gets the value it needs.

    A delegate is just a function that someone else can call without even knowing what it is.

    The coroutine starts looking something like:

    Code (csharp):
    1. IEnumerator Foo( System.Action<int> Callback)
    2. {
    3.   yield return new WaitForSeconds( 2.0f);
    4.  
    5.   // now we call back with our result
    6.   Callback( 2345);
    7. }
    You would make something to receive the data:

    Code (csharp):
    1. void GetData( int x)
    2. {
    3.  Debug.Log( "data is " + x);
    4. }
    and finally invoke the coroutine with that receiving function:

    Code (csharp):
    1. StartCoroutine( Foo( GetData));
    I didn't compile any of the above, just typed it in, apologies for any typos, but that's how it is done in async land.
     
    WaqasGameDev likes this.
  3. leonardjouen

    leonardjouen

    Joined:
    Oct 5, 2018
    Posts:
    32
    Thanks! But I don't really see how to do this, how i can return the value directly ?
    Like the CreateObject function :

    Code (CSharp):
    1. int CreateObject()
    2. {
    3. // Starting async func
    4. // Wait for async func
    5. return 1; // I really need to return a value from the function
    6. }
    The script execution must pause, waiting for the async process, and return the value, in the same function if its possible.
    Have you an idea on how to do this?

    Thanks!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,717
    IF you want to do it:

    1. asynchronously
    2. under Unity

    The please refer to my post above. That is actually how it's done. Really. No kidding. Give it a try.
     
  5. leonardjouen

    leonardjouen

    Joined:
    Oct 5, 2018
    Posts:
    32
    Yes, i get it finally, Thank you!
     
  6. WaqasGameDev

    WaqasGameDev

    Joined:
    Apr 17, 2020
    Posts:
    118
    May be there are better ways to get data out of a coroutine waiting on async operation but your solution solved this issue. Thanks for sharing!
     
    Kurt-Dekker likes this.