#Slow running script with dimension.getEntities() in runInterval

1 messages · Page 1 of 1 (latest)

crimson dirge
#

This runInterval causes a watchdog slow running script warning (about 6.5ms):

system.runInterval(() => {
        const players = world.getPlayers();
        var dimensions = [];
        players.forEach((player) => {
            if (player.isValid()) {
                if (!dimensions.includes(player.dimension)) dimensions.push(player.dimension);
            }
        });
        dimensions.forEach((dimension) => {
            dimension.getEntities({ tags: ["billey_thrown_piranha"] }).forEach((piranha) => {
                if (piranha.target) {
                    if (piranha.target.matches({ location: piranha.location, maxDistance: 2 })) {
                        piranha.teleport(piranha.target.location);
                    }
                    if (piranha.target.matches({ location: piranha.location, maxDistance: 1 })) {
                        piranha.target.applyDamage(1, { cause: "entityAttack", damagingEntity: piranha });
                    }
                }
            });
        });
    });

Also the warning doesn't get spammed it just appears once every time I've thrown like 10 piranhas.
Anyone know a more efficient way to do this?

static zodiac
#

To do what

#

What the problem

#

And how much the ms ?

uncut comet
#

Its more efficent run the code for every dimension

#

Define the dimensions outside of the interval function

#
const dimensions = ['overworld'...].map(world.getDimension);

system.runInterval(() => {
 dimensions.forEach()
});