Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nico_K · Aug 22, 2015 at 11:02 PM · c#coroutinebeginnerbestpractices

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges