Search Unity

Slower update method?

Discussion in 'Scripting' started by ADNCG, Oct 3, 2015.

Thread Status:
Not open for further replies.
  1. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    I'm in need of a slower update method as I have a bunch of scripts that don't need to run every frames but I'd still like them to run in a coordinated manner and frequently enough.

    My idea was to repeat a method that broadcasted the "slower update" event and have my scripts listen to it. Before I go on and implement it everywhere, I was wondering if there is a more efficient way of doing this?
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,750
  3. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
  4. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    In your main Game script (or whatever will be always available)

    Code (csharp):
    1.  
    2.   float rate = 0.2f; // run every .2 sec
    3.   List<MonoBehaviour>  delayedUpdatedStuff = new List<MonoBehaviour>();
    4.  
    5.  void Start(){
    6.       InvokeRepeating("SlowUpdate", 0.0f, rate);
    7.  }
    8.  
    9. void SlowUpdate() {
    10.      foreach(MonoBehaviour m in delayedUpdatedStuff){
    11.           m.SendMessage("DelayedUpdate"); // call your delayed update function... sendMessage can be changed out but Who knows what you have going on so this is easiest
    12.      }
    13. }
    14.  
    Then whenever something is enabled or disabled... add or remove from the list...
     
    Kiwasi likes this.
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If you want to guarantee no "skips" in the framerate, you probably want to guarantee that a particular number of these functions are being executed in any given frame. The methods suggested above all will create longer-then-average frames every X seconds because all of your objects will be updating at once.

    You'll need a master object to coordinate them. Here's an example:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4. public class SlowUpdatingClass : MonoBehaviour {
    5. public List<SlowUpdatingClass> all;
    6. void OnEnable() {
    7. if (all == null) all = new List<SlowUpdatingClass>();
    8. all.Add(this);
    9. }
    10. void OnDisable() {
    11. all.Remove(this);
    12. }
    13. public void SlowUpdate() {
    14. //your stuff here
    15. }
    16. }
    17.  
    18. .....
    19. //different file
    20. public class SlowUpdatingManager : MonoBehaviour {
    21. private int currentIndex = 0;
    22. public int objectsPerFrame = 5;
    23. void Update() {
    24. if (SlowUpdatingClass.all != null) {
    25. int count = 0;
    26. while (count < objectsPerFrame) {
    27. SlowUpdatingClass.all[currentIndex].SlowUpdate();
    28. count++;
    29. currentIndex = (currentIndex + 1) % SlowUpdatingClass.all.Count;
    30. }
    31. }
    32. }
    33. }
    34. }
     
    Last edited: Oct 7, 2015
    danidina330, LMan, ADNCG and 2 others like this.
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'd probably subscribe to an Event or use ExecuteEvents rather then SendMessage. It will cut out some of the performance cost.
     
    novashot likes this.
  7. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    Sorry for the late reply, had 2 very busy days. Thank you for using some of your time to help me.

    Now this is really well thought out. With all the control this provides, I might end up using a similar logic at quite a few places. Once again, this is very much appreciated, thank you.
     
  8. paymanbehnami

    paymanbehnami

    Joined:
    Aug 21, 2018
    Posts:
    5
    Hi
    private int paus= 60;
    void Update()
    {
    if ((Time.frameCount % paus == 0))
    {
    functionme();
    }
    }
    private void functionme()
    {
    print("ok");
    }



    paus If it is more, the execution time will be slower
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    If you're going to post code, even if you are necro-posting a seven-year-old thread, could you please take the time to do it correctly at least?

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You may edit your post above.

    It's not hard and it turns the snail snot code above into something that might be read by someone. The way it is above it will never even be considered code.
     
Thread Status:
Not open for further replies.