Coroutines | Sololearn: Learn to code for FREE!
+ 5

Coroutines

I have been told many times coroutines are very useful.i also don’t really understand what they are.Can someone explain how they are useful in game development

5th Jul 2024, 3:41 PM
Hopefull Dev
4 Answers
+ 5
Coroutines are especially useful when your program is supposed to do "one action/ state after another": Do A, then B – and importantly, don't to do B unless A is completed. Usually, you would also be able to achieve these things without coroutines, but it may get rather complicated (lots of variables, lots of checks...) to handle the different states and ensure their correct order. This is a short intro to get an idea of the concept: https://gamedevbeginner.com/coroutines-in-unity-when-and-how-to-use-them/
5th Jul 2024, 4:50 PM
Lisa
Lisa - avatar
+ 5
Are you familiar with Threads? A coroutine is kind of like a thread (Note: It's not actually a thread, but it may help to think of it like that). Unity docs provides some useful information: https://docs.unity3d.com/Manual/Coroutines.html Coroutines are especially useful for managing tasks that occur over a *period of time*. You can do tasks over a period of time in the void Update() function, and use deltaTime to ensure something occurs after some amount of time. However, this will convolute the Update function and make it responsible for a lot of tasks. You will also need a new monobehavior object for every task if you don't want all instances of the class that uses the Update() function to run that task. Another benefit is for optimization, if something doesn't need to be checked every frame. For example, following the Unity docs, you want to make an indicator to the player when an enemy is nearby, but then you'd be checking the player's distance to all enemies every single frame. Instead, a coroutine can check this every x seconds. Let's say for sake of example that you want to build an MMO RPG game. In this game, there are skills, such as applying a burning effect to an enemy. This burn effect damages in intervals over a period of time. You can use a coroutine to manage this effect using the 'yield' keyword. IEnumerator ApplyBurn(Character target, float duration) { assert.IsNotNull (target); if (target != null) // Defensive programming { const float BURN_INTERVAL = 0.1f; target.isBurning = True; while (duration > 0) { duration -= BURN_INTERVAL; target.dealBurnDamage (this.someDamage); yield return new WaitForSeconds(BURN_INTERVAL); // Wait 0.1 seconds } target.isBurning = false; } } Now when we apply burning to a target we can call the coroutine with: StartCoroutine(ApplyBurn (enemy, this.mySkillDuration)); The function will apply burn damage, then wait 0.1 seconds and continue looping / waiting until the duration is over
5th Jul 2024, 4:55 PM
Rrestoring faith
Rrestoring faith - avatar
+ 3
Though I have no experience with coroutines, I can point you to other discussions found by the Search feature in Q&A: https://www.sololearn.com/Discuss/29133/?ref=app https://www.sololearn.com/Discuss/133548/?ref=app https://www.sololearn.com/Discuss/2218206/?ref=app
5th Jul 2024, 4:52 PM
Brian
Brian - avatar
+ 1
Thanks everyone very helpfull
5th Jul 2024, 10:30 PM
Hopefull Dev