How to prevent a method from delaying Update()?

I am about to try to implement a pathfinding mechanic into my project and I feel it is very computationally intensive. Even otherwise, I’d like to know how to allow new frames to load before a method has finished executing. In other words, let’s say my method takes 5 seconds to execute, but I don’t need its results in this very frame. To illustrate, the execution order would look like this:

frame #5000: unit A calls method pathfinder() which takes 2 seconds to find the optimal path

frame #5001: pathfinder() is running…

.

.

.

frame #5119: pathfinder() is running…

frame #5120: pathfinder() has finished, unit now knows where to go

How do I let the game continue while the method does its thing? Doing this via coroutines sounds like it would require manually calling yield at key points in the execution, which seems impractical. What am I missing?

Threading might be the answer you’re looking for. You might be able to get away with using yields at key points in a coroutine. Pre-calculating the paths on start would work if there aren’t any variations or dynamic obstacles in the way. Or maybe calculate shorter paths, as you said you don’t need the entire path right away.