Ecrofirt

While this doesn't exactly pertain to XNA, I can't find an answer for how to do this correctly and I'm running into this problem making an XNA program.

Basically, I'm re-creating the Windows Starfield screensaver using XNA.

As such I've created a simple class called Star, and I've got a LinkedList of Stars for my program, as linked lists are incerdibly fast for removing items from the middle of the list.

As such, I've got some code like this:

foreach (Star currentStar in stars)
{

currentStar.Update();

}

that works beautifully to update all my stars quickly.

The way I've got my stars set up, though, the Update function returns -1 if the star has passed the screen't border, signifying that the star can be removed from the list.

The problem is, I can't seem to figure out how to go through the list and remove all stars that return -1 wit htheir update function. Trying to do something like this:
foreach (Star currentStar in stars)
{

int ret = currentStar.Update();
if (ret != 0)
stars.Remove(currentStar);

}

Gives me an error telling me:
System.InvalidOperationException was unhandled
Message="Collection was modified after the enumerator was instantiated."

Which makes sense. I'm trying to remove an item from the list, which screws up the enumerator, and then the list can't properly be traversed.

Can anyone suggest what I might be able to do to remove stars without running into this problem It seems I can't use the Remove function while I'm currently traversing the list, which is what's causing this mess.




Re: XNA Game Studio Express Need a little bit of LinkedList help

sideshowdan

You might want to try creating a list of stars to remove in your foreach loop. Then remove each of those from stars.



Re: XNA Game Studio Express Need a little bit of LinkedList help

Derek Nedelman

You can't modify a collection that is being traversed in a foreach loop.

You'll need to use the RemoveAll() method to achieve what you want.

//Change your foreach loop to this
stars.RemoveAll(UpdateStar);

//...somewhere else in your class
static bool UpdateStar(Star star)
{
return star.Update() != 0; //The star will be removed from the list if true is returned
}





Re: XNA Game Studio Express Need a little bit of LinkedList help

Ecrofirt

Hmm...

The LinkedList type doesn't have a RemoveAll method.

How exactly would this work, then

Do you mean for UpdateStar to be a method in my Game1 class And then the RemoveAll function would what, remove any stars that returned true or something




Re: XNA Game Studio Express Need a little bit of LinkedList help

Eli Tayrien - MSFT

as youre going through your stars, save anyone that wants to be removed to a temporary list. once youre finished going through your stars, go through the temporary list and remove each member of the temporary list from the main star list.





Re: XNA Game Studio Express Need a little bit of LinkedList help

Ecrofirt

Thanks.

Such a simple solution, I can't believe I didn't think of it!




Re: XNA Game Studio Express Need a little bit of LinkedList help

Derek Nedelman

Ah, sorry, I should have read more closely. I quickly read your post and thought you were using the List<> type.





Re: XNA Game Studio Express Need a little bit of LinkedList help

Jon Watte

Actually, it's probably simpler to, when you go through the list of stars, save the list of stars you want to KEEP in a second list. Then, after the iteration, replace the list of active stars with this new list.
I'm just sayin' :-)