i have an a* path finding,script
problem is it take a fair amount oftime to compute. is there a way to limit the ammoun of time this script runs for.
because atm when it runs the fps drops for a second or two. i want it to run slowly in the back ground or somthing
you can use yield. yield will stop the execution of your function (also called coroutine if it has yield in it) and come back to it the next frame.
for example if you have
function countToMillion () {
for(i = 0; i < 1000000; i++) {
if(i%1000 == 0)
yield;
}
}
this will count to 1000000 over 1000 frames.
So if your algorithm can be easily split you can do it. There might be better ways too, not sure. I know though you cannot have a separate thread for it.
pathfind arraylist (int startpoint,int endpoint)
{
calculate path
{
if done return arraylist of the path
}
}
from what i read coroutine dont have return values
A co-routine can, in fact, have a return value; or you could implement a callback. Either way, there isn’t any mechanism by which you can tell Unity to time-slice your non-coroutine code for you automatically; that’s what co-routines are for. You’ll need to figure out a sensible place (or places) in your A* implementation to insert yield statements (and do any re-factoring required to use them), then call your path search as a co-routine.