- Home /
Nested Coroutines Waiting for Each Other
I'm currently developing a round based strategy game and run into some problems with using Coroutines. The game has nested rounds, meaning that one game round consists of several fighting rounds which respectively consist of several action rounds and so on.
I want to use coroutines to realise this game logic. My first attempt looks something like this:
void GameStart(){
StartCoroutine(StartGameRound());
}
IEnumerator StartGameRound(){
foreach(...){
yield return StartCoroutine(StartActionRound());
yield return null; //Give control back to unity
}
}
IEnumerator StartActionRound(){
foreach(...){
yield return StartCoroutine(PollForAction());
yield return null; //Give control back to unity
}
}
IEnumerator PollForAction(){
while(!ActionChosen){
yield return null; //Give control back to unity
}
}
I chose this structure so that each method is able to give the control back to the main execution in order to prevent freezing the game. However, as soon as i hit the first "yield return null" in the method "PollForAction" unity gives the control back to the previous coroutines. I would like the other coroutines to wait for each other before executing further. A fix could look like this:
IEnumerator StartGameRound(){
yield return StartCoroutine(StartActionRound);
while(!finished)
yield return null;
}
IEnumerator StartActionRound(){
//ActionRound Logic
finished = true;
}
But to be honest I feel a bit unwell with the resulting structure of this game logic code. It looks a bit smelly. I feel I use too many unnecessary coroutines and the overall code starts to look quite complex for a relatively simple logic. However, I can't figure out a more 'neat' solution for my problem.
That is why I turn to this community: Are there any better approaches or even best practices to realise this game logic?
Answer by cjdev · Aug 22, 2015 at 11:11 PM
To be honest, I think you're getting caught up in the way your game's theme is structured, rather than the way your game's code should be structured. While it is true that each fighting round has a duration and each sub-round in turn has a duration, that tree like structure does not have to be represented as a time-delayed construct in your code. Rather than using nested coroutines you can simply keep track of the lowest level of round and use a timer, even just counting in Update would work, to account for duration.
In order to preserve the concept of nested rounds, if needed, you can create classes for each one with fields that contain their sub-rounds. With this method you can keep track of any current round by checking the current lowest round and it's parent, parent's parent, etc. With a more object-oriented approach it makes it easier to encapsulate the functionality you need for each level of the rounds without having the complicated nest of functions.
Your answer
Follow this Question
Related Questions
C#, is this how you use a coroutine properly? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Need Help fixing shooting script in c# 1 Answer
A beginner scripter needs help with spreadsheet lookup 1 Answer