I can't look too deeply right now, but here's a slight adjustment. It's a good idea to put all of your code in the on('ready',...) event, or inside a dedicated closure. This prevents polluting the global namespace shared by all scripts and can prevent problems if more than one script declare a variable named "myTimer" or the like. Also, the new 'let' and 'const' variable declarations are much better than 'var', so I'd suggest switching over to using let wherever you would have had var in the past or in examples. Also, I always tell people to use === and !== instead of == and != until you know the difference between them, at which point you'll never want to use the short forms. =D on("ready", () => {
let movementArr = [];
let myTimerVar;
let stepstaken = 0;
let tokenToMove = null;
let holdObj;
on("add:path", function(obj, prev) {
holdObj = obj;
let path = obj.get("_path") ;
let left = obj.get("left");
let top = obj.get("top");
let width = obj.get("width");
let height = obj.get("height");
let id = obj.get("_id")
//this will give up the top left point of the box the path is in.
left = left - (width/2);
top = top - (height/2);
//sendChat("Info:top", "=================");
//sendChat("id:", id);
//sendChat("path", path);
//sendChat("Info:left", left + "--");
//sendChat("Info:width", width + "--");
//sendChat("Info:top", top + "--");
//sendChat("Info:height", height + "--");
let m = [];
let y = path.replace(/,/g,' ');
y = y.replace(/\[/g,'');
y = y.replace(/\]/g,'');
y = y.replace(/\"/g,'');
y = y.replace(/M/g,'');
y = y.replace(/L/g,'');
//sendChat("Info", "y :" + y)
m = y.split(" ");
movementArr = getPath(m,left,top)
//we know that the point click (Left) is going to be the top + path[0][0]
//we know that the point click (Top) is going to be the top + path[0][1]
let lPoint = parseInt(movementArr[0][0])
let TPoint = parseInt(movementArr[0][1])
//sendChat("Info::", "lPoint:" + lPoint + "--TPoint:" + TPoint);
const tokens = findObjs({ subtype: 'token', layer: 'objects'});
tokenToMove = findstartToken(tokens,lPoint ,TPoint);
holdObj.remove();
if(tokenToMove != null){
if(movementArr.length > 0){
stepstaken = 0;
myTimerVar = setInterval(myTimer, 400);
}
}
});
function myTimer() {
if(stepstaken >= movementArr.length) {
myStopFunction();
}else{
//sendChat("Info", "stepstaken :" + stepstaken + " of " + movementArr.length) ;
if(tokenToMove != null){
let t = parseInt(movementArr[stepstaken][1])
let l = parseInt(movementArr[stepstaken][0])
///sendChat("Info", "Top :" + t) ;
//sendChat("Info", "Left :" + l);
tokenToMove.set("top", t); //walk!
tokenToMove.set("left", l); //walk
}
stepstaken++;
}
}
function myStopFunction() {
clearInterval(myTimerVar);
stepstaken = 0;
tokenToMove = null;
movementArr = [];
holdObj = null;
}
function findstartToken(tokens, pleft, ptop){
for(let i = 0; i < tokens.length; i++){
if(tokens[i].get("name") !== '' && tokens[i].get("represents").toString().length === 0){
let top = tokens[i].get("top")-30;
let left = tokens[i].get("left")-30;
let width = tokens[i].get("width")+30;
let height = tokens[i].get("height")+30;
//sendChat("name:", tokens[i].get("name"));
//sendChat("width clicked:", pleft + ":" + left + "--" + (left + width));
//sendChat("height clicked:", ptop + ":" + top + "--" + (top + height));
if(ptop > top && ptop < (top + height)){
if(pleft > left && pleft < (left + width)){
//sendChat("token:", "found");
return tokens[i]
}
}
}
}
return null;
}
function getPath(pathArr, left, top){
//sendChat("Info", "pathArr :" + pathArr.length);
returnArr = [];
let t = 0;
let l = 0;
//sendChat("Info", "length :" + pathArr.length );
for(let i = 0; i < pathArr.length; i++){
l = pathArr[i];
if(l === ''){
i++;
l = pathArr[i];
}
// sendChat("Info", "i1 :" + i + "'" + l + "'");
i++;
t = pathArr[i];
if(t === ''){
i++;
t = pathArr[i];
}
//sendChat("Info", "l :" + l + " t:" + t );
let lmove = parseInt(left) + parseInt(l);
let tmove = parseInt(top) + parseInt(t);
returnArr.push([lmove,tmove]);
//sendChat("Info", "top: " + tmove.toString() + " left :"+ lmove.toString() );
}
return returnArr;
}
});
Here are a few links to API coding discussions that you might find helpful: <a href="https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern" rel="nofollow">https://app.roll20.net/forum/post/6605115/namespaces-novice-seeks-help-exploring-the-revealing-module-pattern</a> <a href="https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1" rel="nofollow">https://app.roll20.net/forum/post/6584105/creating-an-object-that-holds-specific-character-dot-id-and-character-name/?pagenum=1</a> <a href="https://app.roll20.net/forum/post/6237754/slug%7D" rel="nofollow">https://app.roll20.net/forum/post/6237754/slug%7D</a>