Using unordered_multimap as entity and component storage
- by natebot13
The Setup
I've made a few games (more like animations) using the Object Oriented method with base classes for objects that extend them, and objects that extend those, and found I couldn't wrap my head around expanding that system to larger game ideas. So I did some research and discovered the Entity-Component system of designing games. I really like the idea, and thoroughly understood the usefulness of it after reading Byte54's perfect answer here: Role of systems in entity systems architecture.
With that said, I have decided to create my current game idea using the described Entity-Component system.
Having basic knowledge of C++, and SFML, I would like to implement the backbone of this entity component system using an unordered_multimap without classes for the entities themselves.
Here's the idea:
An unordered_mulitmap stores entity IDs as the lookup term, while the value is an inherited Component object. Examlpe:
____________________________
|ID |Component |
----------------------------
|0 |Movable |
|0 |Accelable |
|0 |Renderable |
|1 |Movable |
|1 |Renderable |
|2 |Renderable |
----------------------------
So, according to this map of objects, the entity with ID 0 has three components: Movable, Accelable, and Renderable. These component objects store the entity specific data, such as the location, the acceleration, and render flags. The entity is simply and ID, with the components attached to that ID describing its attributes.
Problem
I want to store the component objects within the map, allowing the map have full ownership of the components. The problem I'm having, is I don't quite understand enough about pointers, shared pointers, and references in order to get that set up. How can I go about initializing these components, with their various member variables, within the unordered_multimap? Can the base component class take on the member variables of its child classes, when defining the map as unordered_multimap<int, component>?
Requirements
I need a system to be able to grab an entity, with all of its' attached components, and access members from the components in order to do the necessary calculations and reassignments for position, velocity, etc.
Need a clarification? Post a comment with your concerns and I will gladly edit or comment back!
Thanks in advance!
natebot13