#InvokeRepeating and Execution stuff
1 messages · Page 1 of 1 (latest)
Mind if we jump to a thread? This is actually a really great time to share some stuff that could help break down a lot of stuff you'll be running into in the future
sure thing, I don't mind
First up, let's separate a few things:
InvokeRepeating() is a function within a Monobehaviour component, and has a lifecycle kinda tied to the Monobehaviour calling it. The basic idea is that you call it, add a timer, and it will keep calling it. You can technically Cancel the invoke but this is not common. And should generally be avoided. Let's hold there for just a second and explore some other options.
Generally, you'll see tutorials using InvokeRepeating on Start to quickly show off some logic. This is because it's relatively easy to get something going, but that function is doing quite a lot for you.
So let's imagine a scenario where we wanted some program to run, and repeat after an object is intializied in our game.
Unity gives us a lot of options for this! Things like Awake/Start/Update/OnEnable/OnDisable are all part of an objects lifecycle in a given frame of our game. This can be visualized better here
https://docs.unity3d.com/Manual/ExecutionOrder.html
^^ This, while it may seem a bit complicated if you're just starting your journey, is incredibly useful and common throughout working in Unity day to day. It will become something you can intuit, but a basic breakdown is this
Awake is called when the actual component object is initialized in engine regardless if it is enabled. See that little check mark box next to your custom script in the inspector? It doesn't matter if it's enabled/disabled. The moment that script is initialized and loaded in, whatever is in Awake will run.
Start is called based the first frame that script component is enabled.
Update simple runs the code every frame.
So in this case, we now have something in Start effectively mimicking it's own little Update cycle. If we wanted to control that and allow other folks to read that easily, you'd see something like this in the Update cycle
{
currentTimer += Time.deltaTime;
if (currentTimer >= myInterval)
{
DoACoolFunction();
currentTimer = 0;
}
}```
While this isn't the best example, we know exactly what to expect. We can also simply add a boolean flag like bool ShouldDoFunction to stop doing that function at any time. We have the options to add that to:
- Our If statement
or - Write
if(!ShouldDoFunction) returnat the first line of our Update loop to effectively guard against that code being run should our boolean change.
Again, giving us a little bit of control.
You'll also encounter a ton of posts dealing with timers where some folks will tell you to use Coroutines/IEnumerators.
IEnumerator DoThingEveryFewSeconds(float howManySeconds)
{
// Start routine
while (true) // I'm always true, so let's keep going
{
DoACoolFunction(); // Run my cool function
yield return new WaitForSeconds(howManySeconds); // Wait some time. Return to the beginning of DoThingEveryFewSeconds ^^
}
}```
We call StartCoroutine(DoThingEveryFewSeconds(1f)) just once somewhere in our program. And we will now have a kind of bastardized logic loop that will run every 1 second. We can stop this by either keeping a reference to the Coroutine itself, and stopping it explicitly with StopCoroutine(). Or calling StopAllCoroutines. You'll see this commonly called in an objects OnDisable component, as Coroutines, much like InvokeRepeating can be abused, and the lifecycle of these initialized objects can be a bit more elusive to deal with.
And to your exact question
InvokeRepeating()
in
void Start()
instead of
void Update()
I never understood the reason behind that. ```
If we called it in Update, we'd call InvokeRepeating *every single frame*. Start is as described above
This may be a lot, or a little. Happy to answer/clarify/shut up 🙂 But that's really it.
Hey no worries!! Glad it was useful