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
0
Question by gdennis · Dec 17, 2014 at 10:12 PM · c#visualization

Waiting for a method to signal it is done before launching it again

I am making a visualization in C# where there are a lot of gamemove related 'actions'. An action can be 'walk to', 'say to' or 'pick up' etc etc. If the story I want to visualize goes like this for example:

  • Player walks to NPC1.

  • Player says to NPC1.

In this case i obviously want the first action (sentence) to be completed before starting the second one. My code looks as follows:

 void Update () {
     parseScenario ();
 }

The definition for the parseScenario function is the following: (small theoretical background (since the implementation is a bit different): entry is a list which contains a number of gamemoves (sentences) to complete. Look at this like a parent list, since each of these lists can contain a number of other gamemoves (sentences) to complete, this is indicated by a _next field which is the next one to do)

 public void parseScenario () {
     foreach (string e in entry) {
         parseEntryLine(e);
     }
 }

The parseEntryLine method looks like this:

 public void parseEntryLine (string gm_label) {
     currentGM = findGameMoveByLabel (gm_label);
     if (currentGM.isBrick ()) {
         //Interprete it and perform the animation for it
         animateExpr(currentGM);
 
         //Check if it has a next
         if (currentGM._next != null) {
             parseEntryLine (currentGM._next);
         }
     }
 }


And the animateExpr method is just a function which contains a really big switch statement. Based on what the contents of the 'action verb' is, I start a method which performs the action in here.

Now the problem is that obviously when I run it like this the entire story just gets executed immediately.

I cannot figure out how to wait for an action to complete before starting a new one. Please note that these actions do not have a fixed timespan. (Some require input from the player as well).

I hope the question is clear, if not please ask me to make it more clear.

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
Best Answer

Answer by Kiwasi · Dec 17, 2014 at 10:25 PM

You would typically do this in one of several ways. Listed in my order of preference.

  • Using a coroutine. Simply yield every frame until the task is done

  • Using a call back. Pass the method a delegate to call when it is completed

  • Using bool checking. Set a bool as a flag to identify when each task is done

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 gdennis · Dec 18, 2014 at 10:15 AM 0
Share

Thank you @Bored$$anonymous$$ormon! I thought coroutines might be the way to go. But I cannot figure out how to use it properly. Does everything else wait for the complete function to finish before executing the next line of code?

What are the changes required to the animateExpr function ?

avatar image Kiwasi · Dec 18, 2014 at 10:26 AM 0
Share

Control gets passed back to the Unity engine. In essence this means all of the other Update methods are called.

Here is some pseudo code

This starts the coroutine chain. You only want to call it once

 void Start (){
     StartCoroutine(ParseScenario());
 }

We can yield the result of StartCoroutine. This means that each string will wait for the last string to be processed before resu$$anonymous$$g. The yield return null at the end covers you in case there are no strings in entry.

 public IEnumerator parseScenario () {
      foreach (string e in entry) {
          yield return StartCoroutine(parseEntryLine(e));
      }
      yield return null;
 }

Do the same thing on parseEntryLine.

$$anonymous$$aking a guess at animateExpr

 public IEnumerator animateExpr(...){
     while (doingStuff){
         // Do stuff
         // Use this to wait a frame
         yield return null; 
         // Exit the loop when you are done
     }
 }

If you do use a while loop be sure to save the game before hand. Unity will freeze unrecoverably if you ask it to do an infinite loop.

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

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

Unhandled exceptions while deploying on Visual Studio 1 Answer

Beam of light for flashlight coding issue 2 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