Whilst working on examples and upgrades for my C++ Entity System, something randomly caught the attention of my mind, by surprise: duplicate data. What I'm referring to is how an entity system that uses other libraries (e.g. Ogre3D, SFML, etc.), which already store some information about your entities, handles storing information in custom components.
The Problem:
Say you have two define component types, called: Position
component (which holds the location of an Entity) and Velocity
component (which holds how fast an Entity is moving, and the direction it is moving toward). And of course, you have a Movement
system, which moves entities with a Position
and Velocity
component attached to it. Now... here comes the tricky part, how would you store the Position component effectively/efficiently, so that you do not duplicate data? By duplication of data, this is what I mean (this is a simple example): a Sprite object typically has it's own x/y position (take for example, SFML's Sprite class), therefore if I had a Position component with it's own x/y values, then I would be duplicating data, would I not? I thought about using pointers/references* in a Position component, but that doesn't really work out if I want to save entities to files.
*Yes, I do know that sizeof(float) == sizeof(float*)
, but sizeof(vec2/3) != sizeof(vec2/3*)
. This doesn't really seem much of a problem at first: but what if I had bigger objects to store, such as:
A Mesh:
How would I store a Mesh effectively in a component? I was thinking of just storing a string to where the mesh data was located, but then I thought, what if it's not in a file? How do I resolve that? I also thought to store the raw data in the component, but if I use a 3rd party graphics library (such as Ogre3D or Irrlicht), then I require to actually create the Mesh data via another object.
So, the summed up question is: what's the best way to reduce duplicate data, using an Entity System design, in your game?
Would I have to re-make everything from scratch so that it just "fits" in my entity-system? I'd prefer not to do that, especially if I decide to change the design pattern that I use to make games.