- Home /
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.
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.
Your answer
Follow this Question
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