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 Meech93 · Mar 01, 2018 at 12:12 AM · state-machineturn-basedco-routine

Best way to implement a turn system? Co-Routines or State-Machine?

For reference, I'm trying to recreate a concept similar to games like worms, territory war, or pocket tanks. The flow of the game should go back and forth from player 1 to player 2, taking timed turns, however, the timer should be able to be ended early if a player has finished their turn.

I have tried co-routines to setup a turn timer, however, I can't find a way to end the timer early if a player has finished with their turn. Would a state machine be a better way of handling this?

The flow should be similar to this:

  1. Start player 1's turn

  2. After x seconds OR if player 1 ends turn before time is up

  3. Start player 2's turn

  4. After x seconds OR if player 2 ends turn before time is up

  5. Continue this until win conditions are met

What I have currently (crude outline) is:

 IEnumerator Timer()    {
     while (true)
           {
             yield return new WaitForSeconds(60);
             CheckWin();
             EndTurn();
           }    }
 
    void Start () {
         StartCoroutine(Timer());
     }
     
     void Update () {
     }
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
1

Answer by FuzzyLogic · Mar 01, 2018 at 12:30 AM

A coroutine is the appropriate solution. You can stop a coroutine with the StopCoroutine() method. StartCoroutine() returns an ID that refers to the coroutine that was activated, which you can pass to StopCoroutine() when you want to kill it.

  Coroutine timerID = null;

 IEnumerator Timer()    {
     // while (true) { // infinite loops are BAD!
     while (timerID != null) {
          yield return new WaitForSeconds(60);
          if (CheckWin() || EndTurn()) {
              timerID = null;
          }
      }
  }
  
 void Start () {
     timerID = StartCoroutine(Timer());
 }
      
 void Update () {
     if (EndTurnButtonPressed()) {
         StopCoroutine(timerID);
         CheckWin();
         EndTurn();
     }
 }
Comment
Add comment · Show 2 · 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
avatar image Bunny83 · Mar 01, 2018 at 03:27 AM 0
Share

There's no need to use Update at all. You can read input inside a coroutine as well. The whole sequence of actions can be handled inside a coroutine. Usually you don't want any input to interrupt the execution of the current turn. By placing the input checking inside the coroutine you can avoid unwanted interruptions

 while (true)
 {
     while (!Input.GetButtonDown($$anonymous$$eyCode.Return))
         yield return null;
     // this will execute when the Return key is pressed
 }

Though for a "timed" input handler you may want to create a CustomYieldInstruction that combines a timer with some input checking. Though it highly depends on what kind of input the user interacts with.

avatar image FuzzyLogic Bunny83 · Mar 01, 2018 at 05:50 AM 0
Share

Because of delays with yields, a coroutine is probably not the best place to handle input if you want it to be responsive. Generally you want input to be polled as often as possible for twitch reaction.

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

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

rpg turn based attack enemy is not attacking 1 Answer

Where can I find Memory Game Demo package? 1 Answer

Mecanim-Statemachine caches old version? (was: One Transition blocks other Transitions) 0 Answers

Transition Between Different Player States In Animator 0 Answers

Mecanim - State Behavior - Select motion clip to play at runtime 0 Answers


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