State / Screen management in Entity Component Systems
Posted
by
David Lively
on Game Development
See other posts from Game Development
or by David Lively
Published on 2012-09-12T23:18:17Z
Indexed on
2012/09/13
3:52 UTC
Read the original article
Hit count: 345
entity-system
|screen-management
My entity/component system is happily humming along and, despite some performance concerns I initially had, everything is working fine.
However, I've realized that I missed a crucial point when starting this thing: how do you handle different screens?
At the moment, I have a GameManager class which owns a component manager and entity manager. When I create an entity, the entity manager assigns it an ID and makes sure it's tracked.
When I modify the components that are assigned to an entity. an UpdateEntity
method is called, which alerts each of the systems that they may need to add or remove the entity from their respective entity lists.
A problem with this is that the collection of entities operated on by each system is determined solely by the individual Systems, typically based on a "required component" filter. (An entity has to have a Renderable
component to be rendered, for instance.)
In this situation, I can't just keep collections of entities per screen and only Update/Draw those collections. They'd have to either be added and removed depending on their applicability to the current screen, which would cause their associated components to be removed, or enable/disable entities in a group per screen to hide what's not supposed to be visible.
These approaches seem like really, really crappy kludges.
What's a good way to handle this? A pretty straightforward way that comes to mind is to create a separate GameManager
(which in my implementation owns all of the systems, entities, etc.) per screen, which means that everything outside of the device context would be duplicated. That's bothersome because some things are always visible, or I might want to continue to display the game under a translucent menu window.
Another option would be to add a "layer" key to the GameManager
class, which could be checked against a displayable layer stack held by the game manager. *System.Draw()
would be called for each active layer, in the required order as determined by the stack. When the systems request an iterator for their respective entity collections, it would be pre-filtered to a (cached) set of those entities that participate in the active layer. Those collections could be updated from the same UpdateEntity
event that's already used to maintain each system's entity collections.
Still, kinda feels like a hack. If I've coded myself into a corner, feel free to throw tomatoes as long as they're labeled with a helpful suggestion.
Hooray for learning curves.
© Game Development or respective owner