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 BenjaFriend22 · Feb 16, 2017 at 03:52 PM · c#coroutinecoroutinesreturn

How to wait for a co routine to finish when calling from a method

I need to parse some string's to int's, but this is pretty slow. I am also doing it for a fairly large amount of strings, and I have to split the strings too. So I put that whole process into a co routine in order to avoid frame drops.

I don't think this is a duplicate question because I said : "I haven't found a better way to do it that doesn't involve 2 co-routines. "

Here is my code right now:

     public IEnumerator ParseIPv4(string ipToCheck)
     {
     // Let everyone know that our parse is not done yet
     parseComplete = false;

     // Break out of the 
     if (ipToCheck == null) yield break;

     // Two variables the make sure that what we parse is actually an integer
     bool check;
     int x;

     // Split the IP address at periods first
     stringValues = ipToCheck.Split('.');

     // Make an integer array that has the same length as what we split
     tempIntValues = new int[stringValues.Length];
     
     // Loop through the string array and parse the strings to integers
     for(int i = 0; i < stringValues.Length; i++)
     {
         // Parse string to int, storing what I get the in integer array
         check = int.TryParse(stringValues[i], out x);

         // If what we parsed is an integer and it worked:
         if (check)
         {
             // Set the value in the integer array
             tempIntValues[i] = x;
         }
         else
         {
             // What we parsed was not an int, return false
             Debug.Log("This is not an integer!!");
             yield break;
         }

         // Make it so that the loop waits for the end of the frame 
         yield return null;
     }

     // Let everyone know that the parse is complete
     parseComplete = true;
 }

 public bool CheckIPv4(string IpAddress)
 {
     // Start to parse the string to ints
     StartCoroutine(ParseIPv4(IpAddress));

     // Wait for the chceking to be done
     while (!parseComplete)
     {
         print("We are checking this IP!  " + Time.deltaTime);
     }

     // Now we can move on to do other stuffs
     
 }

I am wondering if this is a good way to do this, or if it would be better to make the "CheckIPv4" method a co-routine as well. The reason that I did NOT do that right now is because I don't want to allocate a whole bunch of memory. This is a class that will be used on multiple objects, so it could end up being a lot if I double the amount of co-routines.

What I want to know is if this is a good way of doing this or not, it feels sloppy to me, but I haven't found a better way to do it that doesn't involve 2 co-routines.

If I am totally wrong about the memory of co-routines, then tell me.

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 Tourist · Feb 16, 2017 at 04:00 PM

If your method needs to pause while the routine is processing, there is no need for a routine since it would freeze the game. Make your method a routine is the best way.

However it is possible to get the routine processes to the end. Instead of calling StartCoroutine, get the IEnumerator object returned by your routine, and call MoveNext until this function returns false. Warning : if your routine calls another one inside, you need to properly to recursively call the enumerators by checking if the IEnumerator.Current is an enumerator and call MoveNext on it before calling MoveNext on its parent.

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

9 People are following this question.

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

Related Questions

Had difficulties implementing intro to Coroutines from unitypatterns.com. Help? 1 Answer

Can't get while loop to execute more than once in a coroutine before yielding [Solved] 2 Answers

Coroutine Won't Stop Using IEnumerator 1 Answer

Toggling bools automatically using coroutines 1 Answer

Multiple Cars not working 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