Greed: Peon.getItems array is always empty


#1

There are items on the map and close by this peon

var peons = base.getByType(‘peon’);
var peon = peons[0];
if(peon)
var i = peon.getItems();

i variable is always empty


#2

An idea : maybe you want to use : peon.getNearest(items).
It will return the nearest item of the peon.
Because for me the getitems is only applicable to this.
It returns an array of all the items in the level. Like :

var items = base.getItems();

Then you can do :
i = peon.getNearest(items)


#3

@rramon That looks like a bug. Can you check for me if it is actually empty (try to use the result in your code), or is the hover debugger just buggy here?


#4

I would try the base.getItems() instead. The peon may not have privileges to get all the items themselves, and the items returned will be the same either way if it does.


#5

@Ed_Reynolds Thanks! Ill just use that.


#6

@nick

var peon = peons[peonIndex];
if(peon)
var i = peon.getItems();
var item = base.getNearest(i);
if (item)
base.command(peon, ‘move’, item.pos);

Seems to be empty. The peon does not move.


#7

@Vettax Thanks, I have decided to go with that.


#8

When I do var items = base.getItems();items is undefined.
Can someone fix this?


#9

I mean, take a look at this.


#10

Until the first coin spawns, there will be no first item. You need to check items.length >= 0 before attempting to use them.


#11

I think Bellardia is right. Your base’s code has been replaced by an empty function after the first frame, when it tried to access an item on frame 0 when there are none. Now, the hover debugger should probably indicate that your code has stopped running after an error rather than pretend to show variable state from a frame when that code didn’t exist.

I’ll track that issue over here.