#That s why I mentioned a timer if you go
1 messages · Page 1 of 1 (latest)
how would I create a timer tho, I looked up async timer c# and nothing useful really came up.
am I supposed to add Time.deltaTime or something similar?
I usually use Stopwatch. You'd probably need a class(static?) variable. Start it when the task begins and restart it after yields. But only yield if it has reached 16 ms( or whatever time you want to dedicate every frame).
google C# stopwatch for the api docs
that it?
Yes.
Actually using TimeSpan might be "more proper", since stopwatch is more for debugging purposes, but it's not that significant.
let me take a look at that
So I looked it up and from what I can tell it returns an interval between 2 dates.
Should I do a DateTime.Now on the start of iteration and check if it's more than a given amount of ms at the end of that iteration?
DateTime startingTime = DateTime.Now;
foreach (var px in expandPixels)
{
List<Vector2Int> availablePixels = new List<Vector2Int>();
#region GetAvailablePixels
if (_unusedCoordinates.Contains(new Vector2Int(px.Key.x + 1, px.Key.y))) availablePixels.Add(new Vector2Int(px.Key.x + 1, px.Key.y));
if (_unusedCoordinates.Contains(new Vector2Int(px.Key.x, px.Key.y + 1))) availablePixels.Add(new Vector2Int(px.Key.x, px.Key.y + 1));
if (_unusedCoordinates.Contains(new Vector2Int(px.Key.x - 1, px.Key.y))) availablePixels.Add(new Vector2Int(px.Key.x - 1, px.Key.y));
if (_unusedCoordinates.Contains(new Vector2Int(px.Key.x, px.Key.y - 1))) availablePixels.Add(new Vector2Int(px.Key.x, px.Key.y - 1));
#endregion
if (availablePixels.Count <= 1) remove.Add(px.Key); //Doing this check first because the element has to be removed even if it's 0
if (availablePixels.Count <= 0) continue;
int numToChange = UnityEngine.Random.Range(1, availablePixels.Count);
for (int i = 0; i < numToChange; i++)
{
int randPixel = UnityEngine.Random.Range(0, availablePixels.Count);
add.Add(availablePixels[randPixel], px.Value);
_unusedCoordinates.Remove(availablePixels[randPixel]);
availablePixels.RemoveAt(randPixel);
}
TimeSpan ts = DateTime.Now - startingTime;
if (ts.TotalMilliseconds > 16) await Task.Yield();
}
something like that