• Welcome to Valhalla Legends Archive.
 

[C#] arrays of class objects

Started by Maddox, November 05, 2004, 06:04 PM

Previous topic - Next topic

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?


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));
}
asdf.

MyndFyre

Instead of that multi-function return statement, can you try:


Unit[] itemsFound = new Unit[foundItems.Count];
foundItems.CopyTo(itemsFound);
return itemsFound;
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Maddox

Hm, I fixed it. Seems like it is something wrong with the SDK I'm using. I've found a work-around.
asdf.