Method to find out if a unit is targeted by an enemy

I thought there was a method to find out if my hero or a friend is targeted by an enemy unit. (was there not even a level about this?) I can’t remember the method, but would like to use it =(

Welcome to the forum @Christopher_Gmelch !

I do not know nearly enough about CodeCombat coding, so I can’t give you an answer, but I can poke around for questions that might be important.

Important Questions

What coding language are you using?
Have you reviewed the Programmaticon?
How far have you progressed through the game?

ah, sorry. Yes that makes sense:

I try programming in Python and I’m currently on Cloudrip Mountain and tried to write something for Summits Gate. Basically I want my units which are targeted by the catapults to dodge the boulders. And I thought there was a level earlier that kind of did something like this.

Hrmm. check to see if the boulder is categorized as an enemy. Then if the boulder comes within a certain range, you can move away from the boulder.

I use javascript, because I’m a failure of a human being, and it’s closer to C# which I know a bit more of.

Try something along the lines of

enemy = hero.findNearestEnemy()
if enemy.type == 'boulder':
    idk how to do if in a range of the hero, but do that:
        hero.moveXY(boulder.pos.x-5, boulder.pos.y-5)

or you could use find missiles

Ah. Oops. That’ll work.

ok, this kind of works, it’s not what i remeber doing in some other level, but still.

right now Ido this:

def dodge():
    friends = hero.findFriends()
    missiles = hero.findEnemyMissiles()
    if len(missiles) > 0:
        for friend in friends:
            distance1 = friend.distanceTo(missiles[0])
            distance2 = friend.distanceTo(missiles[1])
            if friend.pos.x < 80 and (distance1 < 20 or distance2 < 20):
                hero.command(friend, "move", {"x": friend.pos.x + 10, "y": friend.pos.y})

it’s not perfect, and needs some tweaking, but kind of does what i want.
Thanks folks!

The ideal method is using Vectors to the missiles from the peasants and dodging them, which is covered in Glacier. However if you have not yet reached that level then a simple move command away from the missile will suffice. :slight_smile:

1 Like