My buildings won't build in order

Basically, my code isn’t going in order, I am placing buildings in different spots but the code is making some buildings be upgraded.
(also please tell me if this is technically a bug)
(here is my code thing)

while (true) {
    let findNearestEnemy = guard.findNearestMonster();
    if (findNearestEnemy) {
        hero.build("mage", "A");
        hero.build("archer", "B");
        moveRoar();
        hero.build("archer", "C");
        hero.build("archer", "D");
        hero.special("teleport", 36, 51);
        hero.build("archer", "E");
        hero.special("push", 17, 51);
        hero.special("pull", 107, 51);
        hero.build("archer", "F");
        hero.build("archer", "G");
        hero.build("archer", "H");
        hero.build("archer", "I");
        hero.special("burst", 30, 51);
    }
}

Is this inside an event handler that controls one of your guards? If so, you have two guards, so there would be two copies of this code running in parallel. It’s better to do your tower building logic separate from your guard logic.

2 Likes

Okay, I will try this. Thanks for the help.

Wait, would you do separate variables for the guards with the findMyGuards or data.guard to make the variables

i’ve tried

var guard = hero.findMyGuards();
    const guard1 = guard[0]; or [1]
    const guard2 = guard[1]; or [2]


I tried this and my guards are just moving all over the place. here’s my guard code

    var guards = hero.findMyGuards();
    const guard1 = guards[0];
    const guard2 = guards[1];
    const place = data.place;
    var guard1CanUseRoar = guard1.isReady("roar");
    var guard2CanUseRoar = guard2.isReady("roar");
    var typeMonster = hero.getMonsterParameters("ogre", "phoneix"); 
    function moveRoar1() {
        let findNearestEnemy1 = guard1.findNearestMonster();
        if (guard1CanUseRoar) {
        guard1.special("haste");
        guard1.moveTo(30, 57);
        guard1.special("roar");
        if (findNearestEnemy1) {
        guard1.attack(findNearestEnemy1);
        }
        else {
            guard1.moveTo(30, 57);
        }
        }
        else {
            guard1.moveTo(30, 57);
            guard1.attack(findNearestEnemy);
        }
        
    }
    function moveRoar2() {
        let findNearestEnemy2 = guard2.findNearestMonster();
        if (guard2CanUseRoar) {
        guard2.special("haste");
        guard2.moveTo(30, 45);
        guard2.special("roar");
        if (findNearestEnemy2) {
        guard2.attack(findNearestEnemy2);
        }
        else {
            guard2.moveTo(30, 45);
        }
        }
        else {
            guard2.moveTo(30, 45);
            guard2.attack(findNearestEnemy2);
        }
        
    }

Could you please indent your code properly? It’s a bit hard to read this way :confused:

Um, sorry, I’m a bit new what do you mean by “indent properly?”

No idea how to explain it lol

i think what he means is to do this?

if (guard1CanUseRoar) {
        guard1.special("haste");
        guard1.moveTo(30, 57);
        guard1.special("roar");

1 Like

Yes, but a bit off…
Here is an example:

No indenting:

function hi () {
if (e) {
lol();
}
}

With indenting:

function hi () {
    if (e) {
        lol();
    }
}
1 Like
var guards = hero.findMyGuards();
const guard1 = guards[0];
const guard2 = guards[1];
const place = data.place;
var guard1CanUseRoar = guard1.isReady("roar");
var guard2CanUseRoar = guard2.isReady("roar");
var typeMonster = hero.getMonsterParameters("ogre", "phoneix"); 

function moveRoar1() {
  let findNearestEnemy1 = guard1.findNearestMonster();
  if (guard1CanUseRoar) {
    guard1.special("haste");
    guard1.moveTo(30, 57);
    guard1.special("roar");
    if (findNearestEnemy1) {
      guard1.attack(findNearestEnemy1);
    }
    else {
      guard1.moveTo(30, 57);
    }
  }
  else {
    guard1.moveTo(30, 57);
    guard1.attack(findNearestEnemy);
  }
}

function moveRoar2() {
  let findNearestEnemy2 = guard2.findNearestMonster();
  if (guard2CanUseRoar) {
    guard2.special("haste");
    guard2.moveTo(30, 45);
    guard2.special("roar");
    if (findNearestEnemy2) {
      guard2.attack(findNearestEnemy2);
    }
    else {
      guard2.moveTo(30, 45);
    }
    }
    else {
      guard2.moveTo(30, 45);
      guard2.attack(findNearestEnemy2);
   }  
}

I’ve formatted the code, but I’m not sure whether you prefer two spaces or four spaces (I used two because that’s what the majority of the code already uses).

four spaces is better for codecombat

1 Like

Oh okay, thanks guys for the help! I think I got it now.