I am working on an entity system for a game. I have read that cache is very important with the common operation of iterating over entities and their components.
Currently I have a class World that contains the list of entities, and a list of each component.
class World {
vector<Entity> entities;
vector<PositionComponent> position_components;
vector<SpeedComponent> speed_components;
...
};
The idea of this, is to have entities and components contiguous in memory to be cache-friendly.
The problem is that you have to write specific code for each different component, as it is hardcoded in the source code. One solution could be to have a matrix of Component*, where the i-th row of the matrix are all PositionComponents, and so on. The problem with this, is that with the pointers you don't have the memory continuous and break with the cache-friendly.
What would the best way to write generic code and still have cache-friendly data?
template<typename... Args> T * Add(T*(*loader)(K, Args...), K arg, Args... args)
as completely generic Add(), but you still need 1)pass type 2) pass logic the only difference is you write type in <> brackets and you dont call function directly, you just pass pointer. \$\endgroup\$