Search Unity

Invoke vs Coroutine

Discussion in 'Scripting' started by FenrirWolf, Feb 10, 2010.

  1. FenrirWolf

    FenrirWolf

    Joined:
    Feb 10, 2010
    Posts:
    37
    Just wondered, if there was any benefit to using Coroutines over Invokes.

    I would assume that they both use Unity's own internal task management system to trigger events at a certain time, and therefore they should be equivalent, speed-wise.

    Anyone know for sure? I know, measure and be sure, but it's not a huge issue for me. It's more curiosity that prompted this question, than any real need.
     
  2. VeganApps

    VeganApps

    Joined:
    Jun 30, 2006
    Posts:
    263
    I guess the only difference is that you need one line of code less ( or even an own method less ) if you use Invoke.. -The logic behind Invoke is probabely like a WaitForSeconds + Coroutine method.
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    InvokeRepeat at least serves a purpose and thats that you can have the same script run over and over again at a fixed time interval
     
  4. FenrirWolf

    FenrirWolf

    Joined:
    Feb 10, 2010
    Posts:
    37
    I would assume so, since we get methods for canceling all Invokes and Coroutines.

    There's an obvious use for InvokeRepeating, but I was curious if Invoke or a method that yielded with WaitForSeconds might be different, implementation-wise.
     
  5. MadRobot

    MadRobot

    Joined:
    Jul 12, 2011
    Posts:
    339
    I find Invoke useful when I have an object with a limited lifetime.

    For example
    Code (csharp):
    1. void Start() {
    2.     // do stuff to initialize the object ...
    3.     Invoke ("SelfDestruct", lifeTime);
    4. }
    As opposed to this
    Code (csharp):
    1. void Update() {
    2.     elapsedTime += Time.deltaTime;
    3.     if (elapsedTime >= lifeTime) SelfDestruct();
    4. }
    Or even this
    Code (csharp):
    1. void Start() {
    2.     destructTime = Time.time + lifeTime;
    3. }
    4.  
    5. void Update() {
    6.     if (Time.time > destructTime) SelfDestruct();
    7. }
    Of course if its for something like a projectile (which may meet a pre-lifetime demise), then I just include the following
    Code (csharp):
    1. void OnDestroy() {
    2.     if (IsInvoking ("SelfDestruct")) CancelInvoke ("SelfDestruct");
    3. }
    I'm not sure if that CancelInvoke really needs to be done, but I like to clean up after myself.

    Patrick

    Edit: And I like resurrecting dead threads... heh Sorry guys. I was just looking for the diff between Invoke and Coroutines and threw in my 2 cents before I realized how old the thread was.
     
    Tonymotion, SolarFalcon and akuno like this.
  6. magg

    magg

    Joined:
    Sep 29, 2013
    Posts:
    74
    Or just use:

    Code (csharp):
    1.  
    2. Destroy(gameObject, lifeTime);
    3.  
     
  7. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    Invokes are dumbed-down coroutines. A coroutine can be called with inputs. If you're not comfortable writing functions, that doesn't seem like much, but once you are, it's a huge advantage.

    You can also get "handles" to coroutines, used to cancel. For example you could run "glow" twice, on thingA and thingB, getting a handle to each: A=StartCoroutine(glow(thingA,2); B=StartCoroutine(glow(thingB. 6));. Now you can cancel either of them, using A or B to say which one. You don't need this as often, but it's good when you do, and it's also a standard coding trick.
     
  8. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Reahreic likes this.
  9. Privateer

    Privateer

    Joined:
    Jun 5, 2016
    Posts:
    23
    I find that Invokes are error-prone (string calling) and limiting (no parameters).
    After adding the appropriate "ActionInSeconds" coroutines (and several others) to my MonoBehaviour wrapper, calling such an action is just one line of code.
     
  10. ecv80

    ecv80

    Joined:
    Oct 1, 2017
    Posts:
    28
    I feel like Invokes are perfect for running functions once asynchronously after a delay. I'm not sure if one can yield within an invoked function, but if one needs to yield often then that's what coroutines are for anyways, whether they're both handled internally the same or not (which I don't know).

    I actually have a couple of simple functions I implemented with coroutines in a new project which just need to be run after a while and just once, and after checking an old project where I used Invoke for this purpose I'm totally going to switch them to Invokes as I find them more readable and neat.
     
  11. Reahreic

    Reahreic

    Joined:
    Mar 23, 2011
    Posts:
    254
    I'd be curious to see your boilerplate as I've been thinking of doing something similar with a series of coroutines that I seem to use quite often.
     
  12. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    Well, a long time ago (around 2014) I created the CoroutineHelper on the wiki. It's probably similar to what he had in mind. You could use the Run class anywhere to easily run delegates independently of the caller. Note that when I wrote this we did not have a StopCoroutine method (well we had, but only the string version).