InvokeRepeating VS Coroutine

InvokeRepeating VS Coroutine

For many, it is a lingering question. Should I use coroutine or InvokeRepeating? Performance wise it doesn’t matter much if it’s a relatively small/medium game. But technically you must know the difference.

 

Situation 1: Update

If you need to call a function every x seconds throughout the game non stop it’s better to call it using Update. For example you have a player which checks for enemy every 2 seconds and never stops checking. In that case it’s better to check the time and call the function from update. Calling update every frame does come with a performance overhead. But that costs less than half of that of Coroutine or InvokeRepeating. It’s understandable as if we do this with Coroutine or InvokeRepeating we are still counting down time every frame.

 

Situation 2: Coroutine/InvokeRepeating

Now if you have a situation where the Coroutine or InvokeRepeating won’t run during the full process, then it’s better not to go with Update. For example, if player drinks potion increase his size by twice in 3 seconds time. You can just use Coroutine or InvokeRepeating when user drinks potion and stop calling when player size reaches twice instead of doing this in Update. Again, it’s understandable as it’s won’t have an overhead per frame that way

 

We have already established when to use Coroutine or InvokeRepeating rather than using Update. Now let’s dive into both

 

Coroutine:

It has 1.1 times more overhead than InvokeRepeating which is very low and it’s negligible. You can call Coroutine like a function call rather than calling using a string. This it’s easier to call from outside of the class. Please note that if the script is disabled the Coroutine doesn’t stop as it doesn’t rely on the Monobehaviour. But if the gameobject the Coroutine is in is made inactive for some reason, the Coroutine will stop automatically and won’t resume when the gameobject is made active again.

 

InvokeRepeating:

It is called using its string name which is never a good thing. It cannot be directly called from outside class. It is more convenient in terms of calling as you don’t have to count the time yourself. An InvokeRepeating can be cancelled using CancelInvoke() which automatically cancels all InvokeRepeating inside same Monobehaviour. Multiple InvokeRepeating cannot be specifically stopped inside the same Monobehaviour.

 

In most cases I use Coroutines as I don’t have to refer it with a string. It’s easier to control. Each Coroutine inside a Monobehaviour can be controlled separately. It is easier to keep track of and highly customizable. 

Javier Heras Alcuaz

Videogames Developer ?? Unreal Engine Lead Programmer

10 个月

You can also use nameof(YourMethod) to get the method name as a string, always recommended for working with InvokeRepeating. InvokeRepeating(nameof(MyMethod));

回复
Csaba Szabo

Student at Stratford-upon-Avon College

2 年

Great explanations! However there is a way to cancel just a specific method you are invoking; simply add the method name as an argument when you call CancelInvoke(). Like this: CancelInvoke("method_name").

要查看或添加评论,请登录

Rakib Jahan的更多文章

社区洞察

其他会员也浏览了