Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: Maddox on November 05, 2004, 06:04 PM

Title: [C#] arrays of class objects
Post by: Maddox on November 05, 2004, 06:04 PM
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));
}
Title: Re: [C#] arrays of class objects
Post by: MyndFyre on November 05, 2004, 08:28 PM
Instead of that multi-function return statement, can you try:


Unit[] itemsFound = new Unit[foundItems.Count];
foundItems.CopyTo(itemsFound);
return itemsFound;
Title: Re: [C#] arrays of class objects
Post by: Maddox on November 06, 2004, 12:13 AM
Hm, I fixed it. Seems like it is something wrong with the SDK I'm using. I've found a work-around.