Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pathfinder timeouts when it does a double goal #153

Closed
amoraschi opened this issue May 12, 2021 · 1 comment
Closed

Pathfinder timeouts when it does a double goal #153

amoraschi opened this issue May 12, 2021 · 1 comment
Labels
bug Something isn't working

Comments

@amoraschi
Copy link
Contributor

We have this code:

  bot.on('chat', function(username, message) {
    const pos = bot.entity.position;
    if (username === bot.username) return

    const target = bot.players[username] ? bot.players[username].entity : null
    if (message === 'come') {
        if (!target) {
            bot.chat('I don\'t see you !')
            return
        }
        const p = target.position

        bot.pathfinder.setMovements(defaultMove)
        // This line works
        bot.pathfinder.setGoal(new GoalNear(p.x, p.y, p.z, 1))
        bot.once('goal_reached', async() => {
            console.log('Arrived, returning')
            // Prints "Arrived, returning" correctly when it has arrived
            bot.pathfinder.setGoal(new GoalNear(pos.x, pos.y, pos.z, 1))
            // Doesn't path and pathfinder sends a status of timeout
        });
    } 
  })

async function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms))
}

Slightly modified code from example. But it has a problem, I need to add an await sleep(0), after console.log, because if I don't add it, it will timeout, this doesn't happen when I actually add an await sleep(0), like this:

  bot.on('chat', function(username, message) {
    const pos = bot.entity.position;
    if (username === bot.username) return

    const target = bot.players[username] ? bot.players[username].entity : null
    if (message === 'come') {
        if (!target) {
            bot.chat('I don\'t see you !')
            return
        }
        const p = target.position

        bot.pathfinder.setMovements(defaultMove)
        bot.pathfinder.setGoal(new GoalNear(p.x, p.y, p.z, 1))
        bot.once('goal_reached', async() => {
            console.log('Arrived, returning')
            await sleep(0)
            // Arrives, logs "Arrived, returning"
            bot.pathfinder.setGoal(new GoalNear(pos.x, pos.y, pos.z, 1))
            // And pathfind works
        });
    } 
  })

async function sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms))
}
@IceTank IceTank added the bug Something isn't working label Jul 2, 2022
@amoraschi
Copy link
Contributor Author

Use async/await instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants