I have written a simple generic implementation of the BFS algorithm, which I intend to use in several places in my Unity game. A simplified version of the class is:
class BFS {
public abstract void OnNewNodeDiscovered(Node node);
public void FindPath(Node start, Node end) {
// Implementation of the algorithm.
// Calls OnNewNodeDiscovered whenever a new node is discovered.
}
}
I added the method OnNewNodeDiscovered so that users of this class can do custom actions, such as logging, changing the colors of things in the scene, etc., during the algorithm.
This works fine, but now I want to create a sub-class which also, in the function OnNewNodeDiscovered, adds a time-delay, so that the progress of the algorithm can be displayed to the player. The problem is that, the only way I know for adding a time delay in Unity is to convert the function to a coroutine, which returns IEnumerator, and then "yield return new WaitForSecods(delay)". But this breaks my design of having a generic OnNewNodeDiscovered function.
My quesion is: is there an alternative way to add a time-delay inside a function that will be called from Unity? Can't I just say something like "sleep(duration)" to sleep for the specified duration and return later to the next statement point, without having to change the return type to IEnumerator?