I have some trouble with the findNearest() function called from an item


#1

Hello,

I’m was trying to play the multiplayer game Greed, and somehow I wanted to do this with my peasant:

  • find out the closest gold to my peasant
  • find out if there wasn’t an enemy peon closer than my peasant to the gold

But whenever I try to call findNearest from the item API, I have the following error poping up:

Cannot read property ‘apply’ of undefined

I reduced the code to this, am I doing something wrong here or is this function bugged?

var base = this;
var items = base.getItems();

var item=base.getNearest(items);
if (item) {
    var closestEnemy = item.findNearest(base.getEnemies());
}

#2

You are using one time findNearest() and one time getNearest(). I don’t recall right now which is the correct one, but I’m pretty sure only one of these will work.


#3

base have on its API a getNearest() function, while item have in its API a findNearest() function.



#4

That’s funny, I never noticed this. I actually never had the idea to use an items findNearest-function. I usually try to read the target, or track enemies over several gameticks to guess where they are going. Yours is… so much faster.

Anyway, after I opened an issue about this: Did you try to swap either method-call with the equivalent other?
Try to use findNearest() in both cases, or getNearest(). Maybe the info-popup is wrong.


#5

Even if findNearest() works as well as getNearest() for the base object (I have no idea why there are two functions doing the same thing by the way), none of them works with the item object:

item.findNearest() error is:

Cannot read property ‘apply’ of undefined

item.getNearest() error is:

Undefined is not a function


#6

This issue is now tracked here:

Please post any new information in this issue rather than here, so the problem is handled centralized.
Thanks for your contribution.