KayelGee / token-attacher

MIT License
12 stars 18 forks source link

Issue with Attached Token Returning to Previous Position #22

Closed Nullh closed 3 years ago

Nullh commented 3 years ago

Hey, so I've got a weird problem with this module. I have an actor (let's call it the 'ball') and I want to build a macro to allow a player token to pick up the ball, moving it onto their square on the grid. I'm using the following macro code:

(async () => {
    let targets = Array.from(game.user.targets);
    if(targets.length > 0){
        if(targets.length > 1) return ui.notification.error("Can't carry more than one token!");
        let ball = targets[0];
        let newCoords = {x:ball.x, y:ball.y};
        if(token.x+token.w-ball.w < ball.x) newCoords.x = token.x+token.w-ball.w;
            else if(token.x > ball.x) newCoords.x = token.x;
        if(token.y+token.h-ball.h < ball.y) newCoords.y = token.y+token.h-ball.h;
            else if(token.y > ball.y) newCoords.y = token.y;
        await ball.update({x: newCoords.x, y: newCoords.y});
        ui.chat.processMessage(`I pick up the ${targets[0].name}`);
        await tokenAttacher.attachElementToToken(targets[0], token, true);
        await tokenAttacher.setElementsLockStatus(targets[0], false, true);
    }
})();

This works fine if you're using the interface on the server itself, but if you're using a client browser to connect to the server then there's some odd behaviour. The ball moves onto the same square as the player as expected, but the first time the player token moves the attached ball token moves back to its position before being moved by the macro, then moves an arbitrary number of pixels related to the movement of the player it is attached to. After this initial movement the ball token follows the player token as expected, but I can't find a way to prevent this first erroneous movement. I have tried with and without an async function and with and without awaits, but I still seem to get the same error. Any help appreciated!

KayelGee commented 3 years ago

I could see the error on my end and it occured randomly. This is probably caused by the core foundry update race condition. While the awaits make it work on the client side the updates to the same object on the server side might be overwritten. await ball.update({x: newCoords.x, y: newCoords.y}); and await tokenAttacher.attachElementToToken(targets[0], token, true); both will update the ball which leads to the second update sometimes overwriting the not yet finished first update. This probably won't happen anymore in foundry 0.8.x.