Can't use bash and attack in the same code


#1

I’m on the Arcane Ally level and I am able to pass it fine by using basic attacks and my shield. However, I wanted to pass it in a way that left my character with more health at the end by using all of my available attacks. I’m using the “simple katana” and the “steel striker” shield. I wanted to be able to use “power up” from the katana, when available, then move to using “bash”, when available, and when both of those were in cooldown just use my basic attack. I can use “powerup” fine and either use “bash” next or a basic attack with “powerup”. When I use “powerup” and then “bash” and try to use a basic attack last the game gives me the error “Find distance to a target unit”. It doesn’t give that error when I’m just using “powerup” and “bash” only.


#2

post some code please… i’m betting one of your previous attacks killed the enemy and it’s now null… try putting an additional if around the attack call if (enemy)


#3

Here’s what my code looks like. The code seems to run fine up until about 16 sec then my character stops going after the enemies.

// Take down those ogres!

loop {
    var ogre = this.findNearest(this.findEnemies());
if (this.findByType("ogre")) {
    if (this.isReady("power-up")) {
    this.powerUp();
    }
    else if (this.isReady("bash")) {
            this.bash(ogre);
        }
    else {
        this.attack(ogre);
    }
    }
}

#4

you can try something like this. basically just making sure ogre is still valid before attacking it. (note, i moved powerup outside the combat code, as you dont need a target and it doesnt damage anything directly, this way your first attack will be powered up)

loop {
	if (this.isReady("power-up")) {
		this.powerUp();
	}
	var ogre = this.findNearest(this.findEnemies());
	if (this.findByType("ogre")) {
		if (this.isReady("bash")) {
			this.bash(ogre);
		}
		else if (ogre) {
			this.attack(ogre);
		}
	}
}

#5

I tried copying and pasting your code into the level and even writing your code in myself and ultimately it gave the same error. I think it must be something with the level because when I went into the “Dueling Grounds” and wrote in similar code my character did every action as I wanted him to and he came out with quite a bit of his health left over. Here’s the code I put into the “Dueling Grounds”:

// Defeat the enemy hero in a duel!
// Find and attack the enemy inside a loop.
// Use flags and your special moves to win!

this.say("To victory!!! For the good of this land and it's people!!!");

loop {
    if (this.isReady("power-up")) {
        this.powerUp();
    }
    else if (this.isReady("bash")) {
            this.bash("Hero Placeholder 1");
    }
    else {
        this.attack("Hero Placeholder 1");
    }
}

#6

Which level is giving you errors?

Oh. nevermind i see you said arcane ally. i’ll play with it tonight and see if i can see whats going on


#7

Okay cool. I hope you can figure something out. That level has been bugging me for a while.


#8

The problem with your code (I think) is that findByType(‘ogre’) might return true even when there is no enemy, it returns a list of ‘ogre’ type, but I believe that list will always contain something and thus be true. I may be wrong though. Basically you are getting a true for that and ‘bash’ is ready even though the variable ogre doesn’t contain an enemy.

I’d simply change this.findByType('ogre') to ogre

Then it should work hopefully.


#9

interesting. yeah i’ve never used findByType i assumed it was a custom function he created. :slight_smile:


#10

I just tested this and it works. I’m not sure which item gives you that findbytype thing but it’s really not needed. i got rid of it.

loop {
	if (this.isReady("power-up")) {
		this.powerUp();
	}
	var enemy = this.findNearest(this.findEnemies());
	if (enemy && enemy.type == "ogre") {
		if (this.isReady("bash")) {
			this.bash(enemy);
		}
		else if (enemy) {
			this.attack(enemy);
		}
	}
}

On a side note. if you are underpowered this code might help you a little. this is how i beat it initially. when low health i ran away until i got healed. It’s kind of hit or miss, depending on where the ogres spawn and which direction they come from. but try it a few times and you should beat it.

loop {
    var enemy = this.findNearest(this.findEnemies());
    if (enemy && this.health > 40) {
        this.attack(enemy);
    } else {
        var rand = Math.floor(Math.random() * 2);
        if (rand == 1) {
            this.moveXY(42,19);
        } else if (rand == 2) {
            this.moveXY(16,17);
        } else {
            this.moveXY(31,31);
        }
    }
}

#11

findByType is a great function, but you have to have the correct glasses. (currently only the Kithgard Worker glasses though I believe it is supposed to be on the higher level glasses as well.)


#12

Nope. it looks like its only on those kithgar glasses. its not on any of the higher ones i’ve gotten to so far. (the next 5 glasses dont have it). Guess i just dont see the point of it since you can already just access the .type property


#13

I know it isn’t there yet, but was an oversight that is being dealt with. The nice thing is that you don’t have to search through all enemies to find the “shaman” but can go straight to it with this. Not needed but makes code much simpler.


#14

Wow, thanks for the help guys. I didn’t know about the && function. Maybe I should play through all the levels that I can before trying to do complicated lines of code.


#15

No problem.

&& is an operator. it means AND. also you might be interested in || which means OR

you can do stuff like

if (something && (something_else || another_thing)) {

}

Which would mean if something is true AND if either something_else or another_thing is true