I’m developing a turn based board game, think chess
I currently have everything working fine, but the AI computations [using minimax] takes long and causes the game to lag [it’s blocking the main thread]
basically, what’s the best practice to have the AI compute movements without blocking the main thread ? should I use the coroutine yield pattern or do the computations on a different thread ?
This is pretty vague, so it’s hard to give a good answer, as there are so many issues that may be to blame. First and foremost, I would generally default to saying that if your AI computations are taking that long, they probably need to be optimized, and more than likely, a new algorithm needs to be used.
However, if you are using the optimal algorithms and you really need your AI to take this much time, then definitely put it into another thread. Just keep in mind that you will still need to wait for those threads to finish, considering it’s a turn based game, but at least that will free up the main thread for other things, such as UI responsiveness, idle animations, etc…
Just in case you aren’t familiar, here is the MSDN tutorial on using threads in C#: