Executing methods regularly but independent of frame rate with coroutines

Optimization principal 4: Call methods as few times as possible. While it is very simple to put logic into Update() and have it regularly executed each frame, we can improve the game performance by executing logic as occasionally as possible. So, if we can get away with only checking for some situations every five seconds, then great performance savings can be made to move that logic out of Update().

A coroutine is a function that can suspend its execution until a yield action has completed. One kind of yield action simply waits for a given number of seconds. In this recipe, we use coroutines and yield to show how a method can only be executed every five seconds. This could be useful for non-player characters to decide whether they should randomly "wake up", or perhaps choose a new location to start moving towards.

Executing methods regularly but independent of frame rate with coroutines

How to do it...

To implement methods at regular intervals independent of the frame rate, perform the follow steps:

  1. Add the following C# script class TimedMethod in the Main Camera:
    // file: TimedMethod.cs
    using UnityEngine;
    using System.Collections;
    
    public class TimedMethod : MonoBehaviour {
      private void Start() {
        StartCoroutine(Tick());
      }
      
      private IEnumerator Tick() {
        float delaySeconds = 5.0F;
        while (true) {
          print("tick " + Time.time);
          yield return new WaitForSeconds(delaySeconds);
        }
      }
    }

How it works...

When the Start() message is received, the Tick() method is started as a coroutine. The Tick() method sets the delay between executions (variable delaySeconds) to 5 seconds. An infinite loop is then started, where the method does its actions (in this case just printing out the time); finally, a yield instruction is given, which causes the method to suspend execution for the given delay of 5 seconds. After the yield instruction has completed, the loop will execute once again, and so on.

You may have noticed that there is no Update() method at all. So, although our game has logic being regularly executed, in this example, there is no logic that has to be executed every frame. Fantastic!

There's more...

The following are some details you don't want to miss:

Have different actions happening at different intervals

Coroutines can be used to have different kinds of logic being executed at different regular intervals. So logic that needs frame-by-frame execution goes into Update() and logic that works fine once or twice a second, might go into a coroutine with a 0.5-second delay; logic that can get away with less occasional updating can go into another coroutine with a 2- or 5-second delay, and so on. Effective and noticeable performance improvements can be found by carefully analyzing (and testing) different game logic to identify the least frequent execution that is still acceptable.

See also

  • The Spreading long computations over several frames with coroutines recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.22.164