Variables change mid-execution


#1

Language: Javascript
Board: Gold Rush (still)

Again, this is probably something obvious to those of you who know what you’re talking about, but I’m stumped.

Script goal:

  1. Put items in an array.
  2. Sort array by distance from closest to farthest.
  3. Find the highest valued item w/in the closest 10 based on bountyGold.
  4. Strip out coordinates.
  5. Go there.

What actually happens:
Avatar the coordinates it’s walking to continually change.

var items = this.getItems();  //create array of items
var itemValue = 0;
// Sort by distance
items.sort(function(obj1, obj2) {return obj1.distance - obj2.distance;}

// find best of closest 10
for(var i = 0; i < 10; ++i) {
    var item = items[i];
    if (item) {
      if (item.bountyGold > itemValue) { //if it's better, use it
        itemValue = item.bountyGold;
        var itemPos = item.pos;
        }
    }
}
if (item) {
// Strip coordinates
itemX = itemPos.x;
itemY = itemPos.y;
this.say(itemValue);
}
// Go for it
if (item) {
    this.say(itemPos);
    this.moveXY(itemX, itemY);
}

Shouldn’t it go to the coordinates? If not, how do I tell it to quit interrupting itself?


#2

In Gold Rush, which is one of the old levels, you are writing a chooseAction method instead of a plan method, which means that your code runs every frame, and your actions change constantly. For the chooseAction levels, you’ll want to think about your code as setting the action to do for the next fraction of a second based on what’s going on.

Does this make any sense?


#3

Ahh… That’s what the scenario instructions were trying to say. The instruction drop-down is mostly hidden behind the map. Time to learn something new.

Thanks!