I’m currently using a TaskScheduler to spread expensive AI tasks across multiple frames. It’s working reasonably well, but this pretty simple algorithm has 2 major issues.
private Queue<Task> PlannedTasks = new Queue<Task>();
System.Diagnostics.Stopwatch watch;
private readonly long TimeAllowedPerFrame= 1; // Milliseconds
void Update()
{
watch = System.Diagnostics.Stopwatch.StartNew();
while (watch.ElapsedMilliseconds < TimeAllowedPerFrame && PlannedTasks.Count > 0 )
{
PlannedTasks.Dequeue().Activate();
}
watch.Stop();
}
- It treats all frames as equivalent.
We know this isn’t true. Some frames have animations/particles/physics/etc that make the frame take longer to process, but my code will take the same chunk of time no-matter-what.
I’d really like to be able to vary the Time available per frame for tasks depending on how long the frame is taking to complete.
- It treats all Tasks as equivalent
If a task so big that it’s eating up 10 milliseconds, I have to break it into smaller tasks until the chunks are easily handled. This works on my machine, but someone else may be using a slower device and the breakdown may no longer be good enough.
Also; say task A uses 99.9% of TimeAllowedPerFrame and the next task B is large as well; it has to complete the entire task B even though it takes me well over the time allowed.
I’d like to be able to halt/resume a Task so that I can split tasks depending on the actual situation rather than attempting to predict ahead of time.
I have a few ideas on these problems but nothing I’m entirely happy with; can anyone make some suggestions? Would love to hear from people who’ve tackled this topic before as well.