1
\$\begingroup\$

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 GraphicsComponents 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.

\$\endgroup\$
2
\$\begingroup\$

You could have a map in graphicsSystem where the key is the name of the texture file and the value is a reference to the loaded texture object.

When adding textures by calling graphicsSystem.addTexture("TextureName.png") you return the texture pointer.

In graphicsSystem.addTexture(string s) you then check if the hash contains a value with the key s and return the texture. if it doesnt contain the key, load the texture and add the key-valuepair to the map.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the reply, I hadn't thought of that solution! However, having a map in GraphicsSystem doesn't solve the part of the problem about how to access that data. world doesn't have access to graphicsSystem for the world constructor chaining (for terrains -- see code above). Maybe I need to rethink the code structure. \$\endgroup\$ – Rich. T. Aug 30 '17 at 11:13
  • \$\begingroup\$ Why would the world need to know about what textures floorTerrain and wallTerrain use? I dont know what type of objects the xxTerrain are but could contain their draw data if you are going for a EC route. Otherwise to be able to do stuff like instanced rendering you could decouple the graphical parts even more and just keep a string for the texture and one for the mesh in each game object. These will then be used by a drawing system that handles loading/drawing of all objects. \$\endgroup\$ – George Hanna Aug 30 '17 at 12:43
  • \$\begingroup\$ In my current setup, world includes a tileMap, which is a 2D array of references to Terrain objects. Each terrain object needs to be constructed with its texture, and that's why I'm having a problem I think. \$\endgroup\$ – Rich. T. Aug 30 '17 at 12:53
  • \$\begingroup\$ Alright, Is Terrain is just an entity with components that handle collision and drawing? If it is then the graphicscomponent in Terrain should be given the texturename and it should in turn handle getting/loading the texture \$\endgroup\$ – George Hanna Aug 30 '17 at 13:07
  • \$\begingroup\$ My Terrain class is just a class containing the texture ID and some information about the terrain, such as a movement cost and whether it's walkable or not, etc. I haven't created terrains as entities in my system because I didn't think they qualified as entities. \$\endgroup\$ – Rich. T. Aug 30 '17 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.