Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the benefits of coroutines?

I've been learning some lua for game development. I heard about coroutines in other languages but really came up on them in lua. I just don't really understand how useful they are, I heard a lot of talk how it can be a way to do multi-threaded things but aren't they run in order? So what benefit would there be from normal functions that also run in order? I'm just not getting how different they are from functions except that they can pause and let another run for a second. Seems like the use case scenarios wouldn't be that huge to me.

Anyone care to shed some light as to why someone would benefit from them?

Especially insight from a game programming perspective would be nice^^

like image 373
Isaiah Avatar asked Aug 28 '11 23:08

Isaiah


1 Answers

Coroutines in a game: Easy to use, Easy to screw up when used in many places.

Just be careful and not use it in many places. Don't make your Entire AI code dependent on Coroutines.

Coroutines are good for making a quick fix when a state is introduced which did not exist before.

This is exactly what java does. Sleep() and Wait() Both functions are the best ways to make it impossible to debug your game. If I were you I would completely avoid any code which has to use a Wait() function like a Coroutine does.

OpenGL API is something you should take note of. It never uses a wait() function but instead uses a clean state machine which knows exactly what state what object is at. If you use coroutines you end with up so many stateless pieces of code that it most surely will be overwhelming to debug.

Coroutines are good when you are making an application like Text Editor ..bank application .. server ..database etc (not a game). Bad when you are making a game where anything can happen at any point of time, you need to have states.

So, in my view coroutines are a bad way of programming and a excuse to write small stateless code.

But that's just me.

like image 133
user1505148 Avatar answered Dec 07 '22 21:12

user1505148