P
P
Pragma Games2021-06-09 13:03:40
Unity
Pragma Games, 2021-06-09 13:03:40

How to use coroutines and sleep well at night?

After I found out about the existence of coroutines, I started using them everywhere, a coroutine that calls several more coroutines at once, and so on, I just drowned in a sea of ​​coroutines. The question is, how expensive is it to use coroutines for the processor? When is it better not to use them, when is it better to use them and why? In terms of resource costs, why are they better or worse, for example, this

float Timer = 5.0f;

void update()
{
    Timer -= Time.deltaTime;
}


I have read that coroutines cannot be used in loops since yield return new WaitForSeconds(wait); creates a durable object in memory, what does durable mean? In my understanding, this is an object that will survive the garbage collector several times.

And in general, it’s normal that I have several coroutines active in one object at once, but there are a lot of such objects, or should it be simplified?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Kachan, 2021-06-09
@MANAB

More objects - more memory clogging - garbage collector is called more often - more often microfreezes in the game.
Well, if you have already encountered coroutines from coroutines and the complexity of their debugging, then it probably means that they are already overdone and it is worth reducing their use or simplifying them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question