Level: A Bolt in the Dark


#1

I recently beat the level, “A Bolt in the Dark”, and I ran into an issue where Tharin would kill the first enemy in the first room, and then move into the next room. However, instead of attacking the second enemy, it would attempt to move through the left wall to the first enemy Tharin already killed.

Solution:
var enemy = this.getNearestCombatants(); needed to be declared within the scope of the loop:

loop {
var enemy = this.getNearestEnemy();
this.attack(enemy);
this.attack(enemy);
//etc.
}

declaring the variable (enemy) outside of the loop will cause .getNearestCombatants() to reference whichever object was closest at the time it was called, and we want it to evaluate who is closest each time we loop.

Tharin otherwise might bump into a wall while the loop executes but it didn’t seem to break. There seems to be just enough time to finish the level. Is there any way that variable scope might be mentioned in the level guide?


#2

It’s not really an issue about variable scope, because variable scope doesn’t change inside a loop body like it does in a function body. It’s just that when you initialize enemy with the value of this.getNearestEnemy(), you need to refresh it when you want to get another enemy instead of continuing to attack the same one.

I think what we will do is try to hook up some script triggers to point out that the enemy is the old nearest enemy and needs to be updated, because you’re right, this is a common problem we’re noticing in the new playtesting sessions we have been doing.


#3

So what would I do to get past this level. I’m having the same issue where Tharin is continuing to attack the dead munchkin in Bolt In The Dark. Can’t come up with a solution.


#4

If you have a line of code that says this.attack(enemy), you have to think about what value is stored in enemy. If it’s still the dead munchkin, you might need to store a new value in enemy by writing enemy = this.getNearestEnemy() again once you can see a new enemy, then attacking that enemy instead of the old one.

Does that make sense?