hero.findEnemies questions


is it possible to make it so it only looks for enemies within a certain range instead of the normal 60m?
in Backwoods Brawl I would like it so that my hero only cleaves when there is 3+ enemies within the range of the cleave… but it looks at all the enemies instead of just the ones I want it to look at.


How to cleave when multiple enemies nearby


sorry… I forgot to mention I’m using Javascript… :neutral_face:


That’s alright, I can convert @sinogermany’s code into javascript if you want.


Thank you @Deadpool198! :slight_smile:


I’ll do it later today. I’m happy to help!

function findInRangeEnemies(enemies) {
    var arrayOfInRangeEnemies = [];
    for (var enemyIndex in enemies) {
        var enemy = enemies[enemyIndex];
        if (enemy && hero.distanceTo(enemy) <= 10) {
    return arrayOfInRangeEnemies;

while(true) {
    var enemies = hero.findEnemies();
    var enemy = hero.findNearest(enemies);
    var inRangeEnemies = findInRangeEnemies(enemies);
    if(inRangeEnemies.length > 3 && hero.isReady("cleave")) {

To change how many enemies you want in range to cleave, change this:
inRangeEnemies.length > 3 e.g to: > 5

and to change how far away you want your enemies to be before you slaughter them with your sharp sword(:dagger: :smiling_imp:) change this:
hero.distanceTo(enemy) <= 10 e.g to: <= 5

Ok, I hope that helps, and sorry if it doesn’t work, I have tried it, but I’m not 100% sure about javascript because I’ve done codeCombat in python so far.

Anyway, I hope it does,
:lion: :lion: :lion:


:+1: @Deadpool198 !
You function is nicer than the original one! Tested in leave it to cleaver.

Put in main to be fully working:

    if (enemy && enemy.health > 0){
        if(inRangeEnemies.length > 3 && hero.isReady("cleave")) 

edit : and if you prefer the original:

function countNearbyEnemies(range_in_metres){
    var enemies = hero.findEnemies();
    for (var i = -1, count = 0, length = enemies.length; ++i < length; ){
        var enemy = enemies[i];
        if (hero.distanceTo(enemy) < range_in_metres)
            count ++;
     return count;

function worthwhileCleaving(minimumNearbyEnemiesCount){
    var range = 5; // you can change it to whatever makes sense in a level
    var count = countNearbyEnemies(range);
    return count >= minimumNearbyEnemiesCount; // true or false
while (true){
    var enemy = hero.findNearest(hero.findEnemies());
    if (enemy && enemy.health>0){
        if (hero.isReady('cleave') && worthwhileCleaving(4))


Thank you, I’m flattered. :blush: :grin:


This helped me too, thank you!


I’m glad, (20 characters)


Found Why is using “for…in” with array iteration a bad idea?
so changed the code:

    var count = 0;
    for (var i in enemies)


for (var i = -1, count = 0, length = enemies.length; ++i < length; )