I'm developing a game using an entity-component system, and had a question about storing, using and linking textures to entities.
I have a GraphicsSystem
that is responsible for storing and registering textures. Each entity has a GraphicsComponent
including a textureID
, which is just an integer used to look up the stored texture in the GraphicsSystem
.
This works well for textures that are only used by one entity, for example player textures. In this case, I can create new GraphicsComponent
s by something like:
GraphicsComponent(graphicsSystem.addTexture("Player1.png"));
graphicsSystem.addTexture()
adds a texture to the graphics system and returns an integer to its position, which is then stored in the component for later use.
My problem is that this approach doesn't work for textures that are registered once and then used across multiple entities. Examples are textures for bullets and world terrain.
My temporary solution to this problem is to register the texture once, and store the texture id centrally. Whenever I need to (for example) create a new bullet, I just need to remember that the bullet is texture id of (say) 1, and then pass this to the GraphicsComponent
constructor for the bullet.
Similarly for my terrains, I currently just need to remember which texture ID is for which terrain type:
World::World() : floorTerrain(1),
wallTerrain(2),
...
I did think about storing these IDs somewhere, for example in the GraphicsSystem
itself, but my world
instance doesn't have direct access to the graphics system.
Anyway, as you can probably tell I am new to ECS game design and would appreciate any pointers!
Thanks, Rich.