I am trying to create a function that removes an element from an array. Here is the code for the function:
// Removes an element from an array.
// String value: the value to search and remove.
// return: an array with the removed element; false otherwise.
Array.prototype.remove = function(value)
{
var idx = _.indexOf(this,value);
if (idx != -1)
{
return this.splice(idx, 1); // The second parameter is the number of elements to remove.
}
return false;
};
// This code runs once per frame. Build units and command peasants!
// Destroy the ogre base within 180 seconds.
// Run over 4000 statements per call and chooseAction will run less often.
// Check out the green Guide button at the top for more info.
var base = this;
/////// 1. Command peasants to grab coins and gems. ///////
// You can only command peasants, not fighting units.
// You win by gathering gold more efficiently to make a larger army.
// Click on a unit to see its API.
var items = base.getItems();
var peasants = base.getByType('peasant');
for (var peasantIndex = 0; peasantIndex < peasants.length; peasantIndex++)
{
var peasant = peasants[peasantIndex];
var item = peasant.getNearest(items);
if (item)
{
var removed_item = items.remove(item);
//The item is correctly removed (i believe...). If someone checks the content of items it is not there.
base.command(peasant, 'move', item.pos);
var items_new = items;
var item2 = peasant.getNearest(items_new);
//But when i use the getNearest it returns the removed element.
}
}
My problem is that when i use the function it seems to work correct and the element seems to be removed but when i call the getNearest function it returns the removed element. Is it my fault or something else??? Thanks in advance