[SOLVED]How to update enemy's location

Hello! This is my first time creating a topic, so I’ll probably accidently violate 100 rules or something(I did try the read to guide…)
Recently, I decided to try my hand at dueling grounds again. Before, I had code which involved a sword, but now, looking at non-subscriber players, most of them seem to favor hammers. I tried it out, and it seems very clever, so I wanted to create a basic strategy. Essentially, I turn invisible, then I try to build a bear-trap on my enemy, then I rotate between trapping them, spamming decoys, bashing them, and using chain-lightning them. It’s very simple code. The problem is, when my code runs, I want to build a bear-trap on the enemy. However, if the enemy moves, I can’t update the enemy’s location, so my hero moves to the old location and builds a bear-trap there.
Unfortunately, I can only buildXY(bear-trap). I can not use build(bear-trap). I tried using hero.move(enemy.pos) and then building the bear trap once I get there, but for some reason my hero always tries to build at the enemy’s old position. Here is my code

hero.cast("invisibility", hero)

enemies = hero.findEnemies()
def enemyHero():
    
    for enemy in enemies:

        if enemy.id=="Hero Placeholder" or "Hero Placeholder 1":
            hero.move(enemy.pos)
           
            hero.buildXY("bear-trap", enemy.pos.x, enemy.pos.y)
            if hero.isReady("bash"):
                hero.bash(enemy)
def arrowTower():
    if hero.gold>hero.costOf("decoy"):
        hero.buildXY("decoy", hero.pos.x, hero.pos.y)
def punch():
    for enemy in enemies:
        if enemy.type=="Hero Placeholder" or "Hero Placeholder 1":
            if hero.canCast("chain-lightning"):
                hero.cast("chain-lightning", enemy)


    
enemyHero()

while True:
    enemies = hero.findEnemies()
    
    
    enemyHero()
    punch()
    arrowTower()
    
   

It seems like I have to update enemies=hero.findEnemies() by putting it into a while loop, but I’m not quite sure how to do that.

Please tell me how I can update the enemies position so that even if they are moving I can place the bear-trap on them. Thank you! :slight_smile:

Also, please don’t judge my variables… :wink:

1 Like

This is the problem. hero.move() makes lots of teeny tiny movements towards an enemy. If you run hero.move() once, it will make an undiscernable movement. That’s why it’s so good at tracking enemies, because if you loop hero.move() you hero can change direction half way through the movement. But in your code, you’re running move() once, then hero.buildXY() once. BuildXY is an uninteruptable method and won’t stop till your hero gets to the XY location. That whole code will be looped, but move() won’t be looped individually, that means you won’t be able to track the enemy because you’re only making 1 small movement before you lock yourself into a buildXY statement which will take you to the wrong location.
The key is to isolate the hero.move() in a loop.
Another helpful thing to think about: what is the range of building? How close do I need to be?

Extra hint

Use hero.distanceTo() and while.

I hope this helps,
Danny

4 Likes

Okay! It worked! Thank you! I put the hero.move() in a while hero.distanceTo(enemy)>5 loop. Now it adjusts when they are moving :smiley:.

1 Like

Hmm. Maybe remove your program? I saw you are (or were) in top 10, so someone can copy your code…

Well, I think that’s up to @Grand_Sorcerer, it’s his code. I would say the solution rule doesn’t apply considering multiplayer levels have no specific solutions.
Danny