Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Creating rope at runtime looks unprofessional because of coroutines. How to fix?
#2
(06-05-2019, 06:35 PM)yyy-2c2p Wrote: Hello, really enjoying Obi Rope so far, but have run into a couple of issues. Im in the final steps of releasing my game, and needs to work some of these out before launch.

In my game I have a save system that counts how many ropes were in the room before you left, then when you come back it loads all those ropes back into the room. My problem with this is that the only documentation I found for creating ropes uses coroutines, and this makes spawning all weird. Instead of being instantiated all at once the ropes are spawned one after another, then it takes some time for them to gain the variables that I have given them, such as their length, they also take time to attach the start and end prefabs and be affected by physics. It can be a solid couple of seconds after i start the game until the ropes are done doing their thing, and it looks super weird and un-professional the entire time. 

How can I fix this? Is there a way to create snakes without coroutines?

Here is my code
Code:
   public IEnumerator makinSnakesLoop(int totalSnake){
    for(int x =0; x<totalSnake; x++){
    GameObject gg = Instantiate(snake, Vector3.zero, Quaternion.identity);
    yield return gg.GetComponent<ObiRope>().StartCoroutine(gg.GetComponent<ObiRope>().GeneratePhysicRepresentationForMesh());

       gg.GetComponent<ObiRope>().AddToSolver(null);


      gg.GetComponent<length>().length2 = newController.ss.lengths[x];
      gg.GetComponent<length>().exp = newController.ss.exp[x];
      gg.GetComponent<length>().goal = newController.ss.goals[x];
    }
   }

Thank you.

No need to wait for coroutines to be done over multiple frames, if you don't want/need asynchronous initialization. Simply advance the coroutine to completion before continuing execution:

Code:
IEnumerator coroutine = gg.GetComponent<ObiRope>().GeneratePhysicRepresentationForMesh();
while (coroutine.MoveNext());

Note that this is not some Obi-specific trickery. It's part of the concept of "coroutine", that you don't seem to understand well. They're not magic async code, nor do they run in a separate thread. They're generally used to distribute lengthy calculations over multiple frames, but you do not *have* to use them like this. Instead of telling Unity to advance the coroutine over several frames (which is what StartCoroutine does), you can run it synchronously by calling MoveNext() until it finishes.

Obi exposes potentially long initialization methods as coroutines, in case you want to show a loading bar, wait for multiple coroutines to be completed, etc. Think of it as "professional courtesy" Guiño. But otherwise, they're regular methods that can be run synchronously just like any other method.
Reply


Messages In This Thread
RE: Creating rope at runtime looks unprofessional because of coroutines. How to fix? - by josemendez - 06-05-2019, 07:03 PM