Valhalla Legends Forums Archive | .NET Platform | [C#] arrays of class objects

AuthorMessageTime
Maddox
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?

[code]
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));
}
[/code]
November 6, 2004, 12:04 AM
Myndfyr
Instead of that multi-function return statement, can you try:

[code]
Unit[] itemsFound = new Unit[foundItems.Count];
foundItems.CopyTo(itemsFound);
return itemsFound;[/code]
November 6, 2004, 2:28 AM
Maddox
Hm, I fixed it. Seems like it is something wrong with the SDK I'm using. I've found a work-around.
November 6, 2004, 6:13 AM

Search