I have a class which is not a monobehaviour (lets call it "Generation"), and as such running CoRoutines from within are not an option.
I have a monobehaviour (lets call this "TestScript") which needs to call a function within Generation. One parameter required is a Func>. It then calls that Func multiple times, expecting a result in the form of a double. The problem, is that it takes time to return, it needs to run across many frames, and so it kicks off some Coroutines (as this is in the monobehaviour). I set it to return a Task so that I could use Threading.SpinWait.SpinUntil and only return after the coroutines have exited, but this just hangs the main Unity thread.
Here is the function I am calling:
public void AssessGeneration(Func<NeuralNet, Task<double>> assessment)
{
foreach (NeuralNet neuralNet in neuralNets)
{
neuralNet.Fitness = assessment(neuralNet).Result;
neuralNet.fitnessAssessed = true;
}
}
So, I've tried many other things, such as moving the SpinWait into the Generation class, but still hangs. I've tried calling the function in Generation within a Task on a new thread, but when it attempts to run the coroutines it complains about not being on the main thread.
So - I have a function, in a class not inheriting from MonoBehaviour. It is called from a MonoBehaviour, and must pass in a function which takes a parameter of type NeuralNet, and returns a double. It must return a double after a coroutine has finished, but I can't be blocking the main thread.
AssessGeneration
function (the one holding theTestScript
component, and calling theAssessGeneration
function). \$\endgroup\$