#That s why I mentioned a timer if you go

1 messages · Page 1 of 1 (latest)

gilded kestrel
#

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?

brisk ivy
#

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

gilded kestrel
#

that it?

brisk ivy
#

Yes.

#

Actually using TimeSpan might be "more proper", since stopwatch is more for debugging purposes, but it's not that significant.

gilded kestrel
gilded kestrel
#
            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

gilded kestrel
#

yeah I'd say that worked fine but I am not sure if it's the most optimal way

#

One more thing, I am guessing that 16ms is for 60Hz refresh rate.
If I wanted to do it for the user's refresh rate my guess is that I should do (16 / 60) * RR
Is that right?

gilded kestrel
#

well now ig I'll have to figure out why it's nmot adding some pixels to a Dictionary and that's it

#

maybe some more optimizations and I should be good