If I use the following code, any array item from the array returned by the function will be null. It looks like maybe the function is deallocating the droppItems[] memory at the function return. Is there a way to copy the data of the class and add it to the array?
public Unit[] GrabItems()
{
string description = null;
const int range = 8;
Unit[] droppedItems = Unit.Get(UnitType.Item);
ArrayList foundItems = new ArrayList();
foreach(Unit item in droppedItems)
{
if(wantItem.KeepItem(item, ref description))
{
foundItems.Add(item);
Game.Print(Color.BOLD + "Found: " + GetItemColor(item) + item.Name + " (" + description + ")");
Me.ClickMap(ClickType.LeftDown, false, item);
Thread.Sleep(500);
}
else if (item.ItemType == ItemType.Gold && DistanceFromUnit(item) < range)
{
Me.ClickMap(ClickType.LeftDown, false, item);
Thread.Sleep(500);
}
}
return (Unit[]) foundItems.ToArray(typeof(Unit));
}
Instead of that multi-function return statement, can you try:
Unit[] itemsFound = new Unit[foundItems.Count];
foundItems.CopyTo(itemsFound);
return itemsFound;
Hm, I fixed it. Seems like it is something wrong with the SDK I'm using. I've found a work-around.