Node JS Асинхронность и переменная

Пытаюсь сделать бота который ходит за игроком по координатам. Если игрок остается на пред. позиции ничего не предпренимать. Eсли куда-то отошел, то придти к нему. Но переменная lastpos оказывается равной текущей позиции.

const mineflayer = require('mineflayer');
const navigatePlugin = require('mineflayer-navigate')(mineflayer);
const bot = mineflayer.createBot({ username: 'Player' });
// install the plugin
let timel
lastpos = 1
target = 0
timel = 1000
lol = 1000
username = 'Diman12'
navigatePlugin(bot);
// optional configuration
bot.navigate.blocksToAvoid[132] = true; // avoid tripwire
bot.navigate.blocksToAvoid[59] = false; // ok to trample crops
let i = 0;
let c = 111;
while (i < c) {
    i++
timel = timel+6000
setTimeout(function () {
    const target = bot.players[username].entity;
    if (lastpos === target.position)
    {
    console.log('skiped. position eqal last position')
    setTimeout(function () {
    const target = bot.players[username].entity;
    }, 400)

    console.log(lastpos, target.position)
    }
 else if (lastpos !== target.position)
 {

const target = bot.players[username].entity;
bot.navigate.to(target.position);
console.log('Navigating to', target.position);
lastpos = target.position
}
}, timel)
}

Например, чему равен target.position:

Vec3 { x: -21.765172764173194, y: 70, z: 292.1301529389497 } 

UPD
Как я понял, переменной lastpos присваевается значение не тогда, когда надо (надо после того как бот прошел).


Ответы (0 шт):