# buildXY not working as expected. Quirky behavior my bad, or a bug?

**URL:** <https://discourse.codecombat.com/t/buildxy-not-working-as-expected-quirky-behavior-my-bad-or-a-bug/6789>\
**Category:** Bugs\
**Created:** [February 18, 2016, 4:44am UTC](https://discourse.codecombat.com/t/buildxy-not-working-as-expected-quirky-behavior-my-bad-or-a-bug/6789 "2016-02-18T04:44:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raleigh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/raleigh/32/3742_2.png) [@Raleigh](https://discourse.codecombat.com/u/Raleigh)\
**Post date:** [February 18, 2016, 4:44am UTC](https://discourse.codecombat.com/t/buildxy-not-working-as-expected-quirky-behavior-my-bad-or-a-bug/6789/1 "2016-02-18T04:44:05Z")

</div>

Hello,

I am in the process of Defending the Garrison in the Backwoods Forest.  
What I want to do is build “caltrops”.

Even though I’m equipped with a stone builder’s hammer and the caltrops belt, I’m having trouble building anything but a “fire-trap”.  
During testing, this code will build a fire-trap fine:

```python
loop {
   var enemy = this.findEnemies();
   if (enemy) {
      this.buildXY("fire-trap", 57, 29);
   }
}

```

However, if I replace the string above with “decoy”, “arrow-tower” or “caltrops”, my character just goes to the coordinates and starts chopping his sword - but no item gets placed, and the sword-chopping generates no enemy damage at all. The character seems to get stuck in a sword-chopping loop and never gets out of it (any code after this.buildXY(“arrow-tower”, 57, 29); is not executed as far as I can tell).

I’ve tried building a “fence”, which did work, and “caltrops” (when equipped with the caltrop belt) which does not work. Seems strange.

I just didn’t know whether I was doing something wrong or whether it is perhaps a bug of some sort.  
Any thoughts on why this isn’t working as expected?

---

<div class="post-metadata">

**Author:** ![AdrianCgw](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@AdrianCgw](https://discourse.codecombat.com/u/AdrianCgw)\
**Post date:** [February 18, 2016, 5:10am UTC](https://discourse.codecombat.com/t/buildxy-not-working-as-expected-quirky-behavior-my-bad-or-a-bug/6789/2 "2016-02-18T05:10:28Z")

</div>

All these stuff takes money.  
Your hero will try to build them, fail because not enough money, then wait for the next game frame and try again (in case some money had fallen on him somehow 🙂 )

---

<div class="post-metadata">

**Author:** ![Raleigh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/raleigh/32/3742_2.png) [@Raleigh](https://discourse.codecombat.com/u/Raleigh)\
**Post date:** [February 18, 2016, 8:12pm UTC](https://discourse.codecombat.com/t/buildxy-not-working-as-expected-quirky-behavior-my-bad-or-a-bug/6789/3 "2016-02-18T20:12:19Z")

</div>

Ah, thanks very much for the input!  
However, I’m still kind of confused.

On levels that have coins, a counter shows up in the upper right of the game screen.  
On levels that don’t have coins, there is no counter.

So, does this mean that the only levels that I can build “caltrops”, “arrow-tower” or “decoy” on is on levels that offer coins? I was working on the assumption that I can choose to build (rather than attack) on any level that I want to.

Also, if I didn’t have enough money to build a given item, shouldn’t there be an error message that points to this as the problem? As of now, I just get stuck in a loop and the code never progresses past buildXY.

Furthermore, if building these items take money, is there a list as to what each item costs? This info is not listed on the inventory text and I find no reference to this at all.

It all seems strange and bug-like, and this issue is holding me back on a number of levels.

Would very much appreciate further clarification!

---

<div class="post-metadata">

**Author:** ![ChronistGilver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chronistgilver/32/3778_2.png) [@ChronistGilver](https://discourse.codecombat.com/u/ChronistGilver)\
**Post date:** [February 18, 2016, 11:22pm UTC](https://discourse.codecombat.com/t/buildxy-not-working-as-expected-quirky-behavior-my-bad-or-a-bug/6789/4 "2016-02-18T23:22:42Z")

</div>

Once you get a Boss Star in Cloudrip Mountain, you will begin getting income at various rates. Using this, it is possible to build things on any level. Otherwise, you can only build things if coins are existent.

No, because there is nothing wrong with your code. Money you have/don’t have doesn’t constitute errors. Additionally, just because you can’t build at one time does not mean you can’t build it some other time. Therefore, an error message would not be accurate.

Decoys and caltrops cost 25, and arrow towers cost 75.

---

<div class="post-metadata">

**Author:** ![AdrianCgw](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@AdrianCgw](https://discourse.codecombat.com/u/AdrianCgw)\
**Post date:** [February 19, 2016, 1:14am UTC](https://discourse.codecombat.com/t/buildxy-not-working-as-expected-quirky-behavior-my-bad-or-a-bug/6789/5 "2016-02-19T01:14:58Z")

</div>

The buildXY is not the only function that has this behavior: if you call cleave, it will block until the cleave it is ready.

You must always guard your build calls, just like the cleave calls. `this.gold` lets you know how much gold you have at the moment:

```python
if(this.gold>=this.costOf("decoy"))
    this.buildXY("decoy",30,30);

```

---

<div class="post-metadata">

**Author:** ![Raleigh](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/raleigh/32/3742_2.png) [@Raleigh](https://discourse.codecombat.com/u/Raleigh)\
**Post date:** [February 19, 2016, 2:21am UTC](https://discourse.codecombat.com/t/buildxy-not-working-as-expected-quirky-behavior-my-bad-or-a-bug/6789/6 "2016-02-19T02:21:23Z")

</div>

Awesome,

Thanks so much ChronistGilver and AdrianCgw!  
It all makes sense now - I really appreciate the help.  
Looking forward to that Boss Star in Cloudrip Mountain. 🙂

All the best!
