Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by ShawnMcCoo1 · Mar 30, 2014 at 05:04 PM · coroutines

Error using coroutines for game phases.

Goal

My goal is to use coroutines for handling flow of game state for a multi-player card game.

Problem

I don't seem to understand the nature of coroutines correctly. In order to model my domain without the trouble of involving MonoBehavior any more than necessary, I opted for a CoroutineRunner class that takes the pain so that the rest of the code doesn't have to.

Hello, I am working to learn to use coroutines for managing sequences of events. For now, I'm trying to prototype this functionality but I am running into some problems.

I hope that this example is clear enough. I have a Game object that calls Phases that call Events. Again, this is a proof of concept for me to get my head wrapped around programming phases of a turn-based game using coroutines for state.

The problem that I'm facing at the moment is the error: MissingMethodException: The best match for method RunCoroutine has some invalid parameter. Naturally, I can see that the CoroutineMethod type-hint is not the same as IEnumerator. But, I'm unclear as to what the nature of CoroutineMethod actually is. Any insight would be appreciated.

I would also love some insight as to ramifications of using this approach for managing phases of gameplay turns, etc. Any information would be appreciated.

CoroutineRunner

A MonoBehavior class that can be used to run coroutines when not using MonoBehavior derivatives in the domain.

I got this code from this blog post.

 public class CoroutineRunner : MonoBehaviour
 {
     public delegate IEnumerator CoroutineMethod();

     private IEnumerator RunCoroutine(CoroutineMethod coroutineMethod)
     {
         return coroutineMethod();
     }

     public Coroutine StartCoroutineDelegate(IEnumerator coroutineMethod)
     {
         return StartCoroutine("RunCoroutine", coroutineMethod);
     }
 }

Game

This class represents the game itself as a container for phases.

 public class Game : MonoBehaviour
 {
     private CoroutineRunner _runner;

     public void Start()
     {
         Debug.Log("Starting game...");
         _runner = gameObject.GetComponent<CoroutineRunner>();
         StartCoroutine(Routine());
     }

     private IEnumerator Routine()
     {
         var phase = new GameSetupPhase(_runner);

         Debug.Log("Beginning new event...");
         yield return StartCoroutine(phase.Routine());
         Debug.Log("end");
     }
 }

GameSetupPhase

The represents a phase of the game.

 public class GameSetupPhase
 {
     private readonly CoroutineRunner _runner;

     public GameSetupPhase(CoroutineRunner runner)
     {
         _runner = runner;
     }

     public IEnumerator Routine()
     {
         Debug.Log("Defining sequence of phase...");
         var e = new InitializeGameStateEventRoutine(_runner);
         yield return _runner.StartCoroutineDelegate(e.Routine());
         Debug.Log("phase sequence complete...");
     }
 }

InitializeGameStateEventRoutine

This represents the concept of a low level thing that needs to be done. Players need to draw 3 cards or something like that.

 public class InitializeGameStateEventRoutine {
     private CoroutineRunner _runner;

     public InitializeGameStateEventRoutine(CoroutineRunner runner)
     {
         _runner = runner;
     }

     public IEnumerator Routine(CoroutineRunner runner) {
         Debug.Log("Initializing game state...");
         yield return new WaitForSeconds(5f);
     }
 }

Ultimately, I'd like to be able to set up the game, get players in, etc. Then, just do something like game.Start(); Then, the game knows to loop through a number of Phases, each time calling phase.Start(); or something like that. Then, each of the phases does the same until it's all done.

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 CatchCo · Mar 30, 2014 at 08:03 PM

The exception is trying to tell you that one of the methods you are calling does not have the correct Method signature as your delegate.

To be specific, your issue is at line 9 of the InitializeGameStateEventRoutine class.

You'll have to change

``` public IEnumerator Routine(CoroutineRunner runner) { ... } ```

to

``` public IEnumerator Routine() { ... } ```

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

21 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

Related Questions

WWW download in background during position animation 2 Answers

Time.time undependable in a coroutine? 1 Answer

How do I script enemy AI movement using Coroutines? 1 Answer

Trouble Resuming after Yielding while Inside Coroutine 1 Answer

Using Time efficiently regardless of current frame rate. 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