Locating a flag

it says:

line 6: null is not an object (evaluating ‘tmp9[tmp10]’)

heres the code

loop {
var enemy = this.findNearestEnemy();
var flag = this.findFlag();
var flagPosition = flag.flagPosition;
var fy = flagPosition.y;
var fx = flagPosition.x;
this.moveXY(fx, fy);
if(this.isReady(“cleave”)){
this.cleave(enemy);
}
else{
this.attack(enemy);
this.shield();
this.warcry();

}

}

You need to put the code after enemy = line inside an if statement.

loop {
	var enemy = this.findNearestEnemy();
	var flag = this.findFlag();
	if (flag) {
		var flagPosition = flag.pos;
		var fy = flagPosition.y;
		var fx = flagPosition.x;
		this.moveXY(fx, fy);
	}

	if (enemy) {
		if(this.isReady("cleave")){
			this.cleave(enemy);
		} else {
			this.attack(enemy);
			this.shield();
			this.warcry();
		}
	}
}

What is happening is an enemy (and flag) is not always there, so the object is null and trying to perform actions on a null will give you a script error. As a side note, you’re going to want to also add in the command to pickup a flag after you move to it.

Couple of little “gotchas” here assuming Javascript:

  • Before getting a property of “flag” you need to check if you have a flag (that it is not null)
  • To get a position from an item, check “.pos” instead of “flagPosition”

An example would be:

if (flag) {
    var flagPosition = flag.pos;
    this.moveXY(flagPosition.x, flagPosition.y);
}

Good catch on the flagPosition bit, i completely overlooked that thinking his issue was the null objects :slight_smile: