prettymuchbryce / easystarjs

An asynchronous A* pathfinding API written in Javascript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A novice coder question about using easystarjs.

Dan1400 opened this issue · comments

I need my callback function passed in .findPath() to assign the completed path array as a property to the object which called the .findPath() function, but I can't figure out how to do it since the passed in calback function is out of scope. Any advice would be appreciated.

Hi Dan. Common es5 practice is to maintain a reference to the object as self at the top of the function declaration. If you're using es6 you could use a fat arrow function to retain the scope. If you post a snippet of your code I might be able to provide more guidance.

Thanks for the reply.
I got it working using the self reference solution.
Here's my working test object:
var entity = {
path:[],
x:0,
y:0,
targetX:3,
targetY:0,
findPath:function () {
var self = this;
easystar.findPath(this.x, this.y, this.targetX, this.targetY, function(path){self.path=path});
}
};