JavaScript Pathfinding - Split into several "Ticks"

Started by
1 comment, last by Oliver Sch 11 years, 5 months ago
(Sorry, First Post, Please move to Game Programming if needed)

I want to run Astar for my Pathfinding on every Unit.


I would like to reduce possible lag by only using it for X amount of units per game tick.

So that the "For each Unit" loop does not run pathfinder for every unit before it goes on to the next function.

Instead it should only find the Path of a few Units each time the Game Loop is at the Pathfinding Function again.


I am pretty sure I gotta run this with a Start-Stop Variables. But I don't know how that should look.

Here is a Quick Doodle how it might look:

[source lang="jscript"]var Unit = new Array(500);
var arrayStart=0;
var arrayStop=0;

"GameLoop"
{

// Checks If at start of Array
if (arrayStart = 0)
{
arrayStop = 50;
}
// Not the first unit. Set starting point to last Pathfind
else
{
arrayStart = arrayStop;
arrayStop = arrayStop + 50;
}

for (var count = arrayStart; count < arrayStop; count++)
{
"Pathfinder Function" Unit[count];
}
arrayStart = count;

};[/source]

Unless JavaScript if functions are executed in a different manner, I would like to know if this is a viable solution, or if you got better ones.
Advertisement

I would like to reduce possible lag by only using it for X amount of units per game tick.


When you say "possible lag", that would suggest that you haven't yet verified it. I'd say verify that there is unacceptable lag in that loop first.
If you're always stopping the algorithm before it's done, how does that affect the rest of your artificial intelligence?

When you say "possible lag", that would suggest that you haven't yet verified it. I'd say verify that there is unacceptable lag in that loop first.


Unacceptable Lag or not, I would still like to learn how to apply this.


If you're always stopping the algorithm before it's done, how does that affect the rest of your artificial intelligence?


I would only stop the algorythm for a few miliseconds. Instead of stoping the entire script for who knows how long..

I am going to do it the normal way, but I would still like some ideas on how to split it.

This topic is closed to new replies.

Advertisement