getNearest() result can't be used with indexOf()?


#1

Seems like a bug to me. On greed:

var items = this.getItems();
var peons = this.getByType(‘peon’);

if(peons[0]) {
var item = peons[0].getNearest(items);
var index = items.indexOf(item);
this.say(index);
}

indexOf() will return -1.

(edit to produce working code that shows the bug via say(index))


#2

You have a mistake on the variable definition:

var peons = this.getByType(‘peon’);

if(peon[0]){

You define “peons”, but you are calling “peon”

I hope that helps,


Pop() function in python
#3

Fair criticism of the above code, which I threw together to explain the problem. Unfortunately that doesn’t change the actual bug. :slight_smile:

(if you use the above code referencing the peons variable correctly, you will hit the bug. If you reference the peon variable incorrectly, you actually just hit an error because peon is undefined)

Also, it shouldn’t actually matter, because if peon were any variable that correctly dereferenced ‘getNearest()’, then it should also return an item that can be found in the items array…


#4

I’ve reproduced the bug. Tracking it over here. I think it is something to do with the API protection turning the object into not-quite-the-same object. Thanks for the bug report.


#5

Awesome. And good to know about _.indexOf as a workaround. :smiley: