Coherencia entre listas de C #

I am writing a small game engine for XNA. Right now my entity manager has a list of all entities it can update and render. Lets say, however, that I want to split this list up into 'sub lists'. Maybe some entities do not require rendering, and so we would like to separate the rendering entities from the non-rendering entities, while maintaining another list which shows ALL entities for updating. So thats 3 lists.

Is there a way to maintain consistency between lists easily? To ensure that the exact same objects are being referenced in both lists? (Not a copy of the object?). In C++ we'd just have an array of pointers to entities that render, and another for entities that dont.

The problem isn't really putting entities into these lists, its disposing them thats throwing me off. To destroy an entity, i simply set a 'dispose' bool to true in that entity. The entity manger, when looping through all entities, removes the reference to that entity if 'dispose' is set to true, and the garbage cleaner destroys the entity because there is no more references left to it. Would i have to manually go through each and every list that has something to do with Entities and remove that entity from those lists to keep things consistent?

another example of this 'problem' is with quad trees. I have a 2d game and i generate a quad tree for all the entities. The entities that lie in between 2 grid locations are in 2 branches of the tree. how would i remove it from the tree? search the entire tree for references to that entity? seems a little pointless to have a quad tree if thats the case..!

preguntado el 01 de febrero de 12 a las 03:02

5 Respuestas

Consider instead of 3 lists, have one list and 2 queries of that list.

var list = GetYourMainList(); 
var renderableEntities = list.Where(item => item.IsRenderable); 
var nonrenderableEntities = list.Where(item => !item.IsRenderable); 

// work with the queries
foreach (var item in renderableEntities)
{
    // do whatever
}

Here, you can iterate over each query, the results will stream from the main list, and any update to that list will be reflected in the query results as they are iterated. The caveat is that the list cannot be modified (as in added to or deleted from) while the queries are being iterated.

Respondido 01 Feb 12, 07:02

This would be slow if they need to call this method frequently though, no? - Chris Shain

@Chris, as opposed to maintaining 3 lists simulteneously, keeping them synchronized, resizing, etc? That isn't to say performance isn't a consideration, but I would want to see an unacceptable performance degradation that points to the Linq queries as well as proof that the multiple list approach is indeed faster. - Anthony Pegram

As for your disposing problem, you could implement IDisposable. For your lists, you could make a class that contains the two lists, and if you need the entire list, implement an enumerator that would enumerate over the two lists.

Respondido 01 Feb 12, 07:02

You could put a wrapper around your entity that knows what lists it is in so that when you want to remove it, you can remove it from all places at once relatively easily.

Respondido 01 Feb 12, 07:02

If you have items in multiple lists, then yes, if you want to remove them you must remove them from all lists in which they occur. A simple way to do this is to assign responsibility for removing items to the items themselves: for example, rendering items would remove themselves from the rendering items list when you call RemoveFromLists() on them.

The use of queries as described by Anthony Pegram eliminates this complexity but is inefficient if the lists are large, since they cannot be incrementally updated.

As for quadtrees, a straightforward thing to do is to have each item keep a list of quadtree nodes in which it occurs (or if you don't want to add a field to your items, use a Dictionary). It is then simple to remove them from all nodes in which they occur in constant time.

Respondido 01 Feb 12, 07:02

You could have your entity expose a "Disposing" event that is fired before it is disposed. Then you could have your list's Add method register an event handler with the entity being added that will remove the entity from the list before it is disposed.

Respondido 01 Feb 12, 07:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.