Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Bypass "...but it's dead!" hero freezing while using bash() in Javascript

I would like to use the bash function to deal more damage when I’m fighting, but if I cast “chain-lightning” using the dragon gloves before I go to bash an enemy who is a little distance away and it gets killed, my hero gets stuck on the hero.bash(enemy); function. This is very troublesome because it meant my hero would stop doing anything altogether. Any ideas to solve this? Like tell my hero to forcefully stop the bash() function if it runs for more than a set amount of time?

P.S. Not a dupe of https://discourse.codecombat.com/t/but-its-dead-and-my-people-freeze/13824 because that is Python.

Post your code, please.

This is the typical code I use in brawls:

while (true) {
    var enemy = hero.findNearestEnemy();
    var item = hero.findNearestItem();
    var friends = hero.findFriends();
    var flag = hero.findFlag("green");
    for (var i = 0; i < friends.length; i++) {
        hero.command(friends[i], "attack", friends[i].findNearestEnemy());
    }
    if (hero.gold >= hero.costOf("soldier")) {
        hero.summon("soldier");
    } else if (flag) {
        hero.pickUpFlag(flag);
    } else if (enemy && hero.distanceTo(enemy) < 10) {
        if (enemy && hero.canCast("chain-lightning", enemy)) {
            hero.cast("chain-lightning", enemy);
        } else if (enemy && hero.isReady("bash")) {
            hero.bash(enemy);
        } else if (enemy) {
            hero.attack(enemy);
        }
    } else if (item) {
        hero.move(item.pos);
    }
}

Maybe try using bash first, then do chain lightning, because chain lightning might also kill people you didn’t target. So your hero is running towards someone, to bash them who’ll subsequently be killed by your own chain lightning, causing an error.
:lion: :lion: :lion:
P.S. this happens quite a lot to me as well, I normally wait 0.1 and then check if they’re there again. If so, then I attack.

var attackingRange = 30;  // you are using 10m but the throwers attack you from 25 m
// I will put all my hero attack code under
    if ( enemy && enemy.health > 0 ){ 
       if (hero.distanceTo(enemy) < attackingRange) {
        //  attack code here 
        }
    }

Try first without summoning soldiers ( your target is maybe killed by your soldiers) and using flags, then increase the code complexity. ( I never use flags even in ‘flag’ levels, I’m too slow…) . Tell us the result of your tests.

Why your hero is freezing:
0 meaning is there is no item, enemy or flag:

 item | enemy| flag |     in code     | executed 
-------------------------------------------------
   0  |   0  |   0  |                 |
   0  |   0  |   1  | else if (flag)  | first 
   0  |   1  |   0  | else if (enemy) | second 
   0  |   1  |   1  |                 | 
   1  |   0  |   0  | else if (item)  | third
   1  |   0  |   1  |                 |
   1  |   1  |   0  |                 | 
   1  |   1  |   1  |                 | 

``
You are telling imperatively the order of execution but do not handle 5 remaining 
situation. You must at least have an else  ( your hero to stay at the same position 
or go somewhere). 
The hero freezes at item = 0 | enemy = 0 | flag = 0
1 Like

I believe you have misinterpreted my code, because if there is nothing then it just cycles through the while() indefinitely until it finds something it reacts to. My hero freezes only if the enemy he’s trying to bash dies before he bashes it. Maybe you thought that after it runs an else if it will run the else if after it as well.

Reality:

 item | enemy| flag |     in code     | executed 
-------------------------------------------------
   0  |   0  |   0  | while(true)     | N/A
   0  |   0  |   1  | if (flag)       | first 
   0  |   1  |   0  | else if (enemy) | second 
   0  |   1  |   1  | else if (enemy) | second
   1  |   0  |   0  | else if (item)  | third
   1  |   0  |   1  | if (flag)       | first
   1  |   1  |   0  | if (flag)       | first
   1  |   1  |   1  | if (flag)       | first

Also, after some extensive testing I think I have found a nice solution:

if (hero.isReady("bash")) {
            while (enemy && hero.distanceTo(enemy) > enemy.size) {
                hero.move(enemy.pos);
            }
            if (enemy) {
                hero.bash(enemy);
            }

This exploits the fact that the .size attribute of almost any interactive entity is the radius of it’s hitbox, so my hero can bash the enemy as he is right on the hitbox so he never misses.

Thanks for the help anyways.

BTW Is it just me, or are ogres actually roblox noobs in disguise? (“oof” death sound)

:thinking: intensifies

I will reply in detail later but you can check one of my first posts here :slight_smile:
see the BrokenSyntax post '...but it's dead!' and my people freeze!