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*, out x);*
// If what we parsed is an integer and it worked:
if (check)
{
// Set the value in the integer array
tempIntValues = 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.