Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unscaled time invoke?

Discussion in 'Scripting' started by Marscaleb, Feb 20, 2015.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    1,036
    Is there any simple method to invoke a function but have it count on unscaled time?
    I have some functions that I need to invoke while the time scale is set to 0 (paused) but the normal invoke method uses scaled time, so when the game is paused like that it never gets invoked.

    I can arrange some functions in my update that can manually count the unscaled deltatime, but that seems like such a waste of time and extra code. Plus that extra code is going to start getting really complicated if I ever find I need to invoke more functions that use unscaled time.
     
  2. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    Its your lucky day! :D I have written classes to accomplish this very task. Call it like this, note that timeScale is set to 0, yet 5 seconds later, MyFunc runs.
    Code (CSharp):
    1.     void Start () {
    2.         Invoker.InvokeDelayed(MyFunc, 5);
    3.         Time.timeScale = 0;
    4.     }
    5.  
    6.     void MyFunc()
    7.     {
    8.         Debug.Log ("adfg");
    9.     }
     

    Attached Files:

  3. jupando

    jupando

    Joined:
    Nov 17, 2014
    Posts:
    9
    Hey, I really like this script! I'd really like to use it in my game, just wanted to check the license - would you be willing to add something similar to the MIT license to your script so it's clear what permissions you're giving for use?

    Here's the example:
    http://opensource.org/licenses/MIT
     
  4. gtzpower

    gtzpower

    Joined:
    Jan 23, 2011
    Posts:
    318
    Glad you like the script! I have re-uploaded it in my original post with the MIT license for you.
     
  5. dohaiha930

    dohaiha930

    Joined:
    Mar 27, 2018
    Posts:
    55
    gtzpower likes this.
  6. EvilKris

    EvilKris

    Joined:
    Nov 18, 2014
    Posts:
    27
    Thanks! I used it too!
     
    gtzpower likes this.
  7. blockh34d

    blockh34d

    Joined:
    Sep 9, 2015
    Posts:
    3
    This helped me, thanks for sharing. I added the following for my purposes, in case it helps anyone else:

    Code (CSharp):
    1.  
    2.     static List<InvokableItem> scratchInvokeList = new List<InvokableItem>();
    3.     public static void CancelInvoke(Invokable func)
    4.     {
    5.         scratchInvokeList.Clear();
    6.         scratchInvokeList.AddRange(Instance.invokeList);
    7.         foreach (var i in Instance.invokeList)
    8.         {
    9.             if (i.func == func)
    10.             {
    11.                 //Debug.Log("Purging invoker item");
    12.                 scratchInvokeList.Remove(i);
    13.             }
    14.         }
    15.         Instance.invokeList.Clear();
    16.         Instance.invokeList.AddRange(scratchInvokeList);
    17.     }
    18.  
     
    gtzpower likes this.
  8. NextGamesStudio

    NextGamesStudio

    Joined:
    Aug 2, 2023
    Posts:
    1
    An amazing script. Helped me in 2023 thank you so much!!
     
    gtzpower likes this.