I have a scene class for objects, cameras, and lights in my game
class Scene {
private:
std::vector<GameObject> objects;
std::vector<Light> lights;
// ... more containers ...
and I am trying to create a class interface that lets the user add, remove, retrieve, and iterate through these objects without giving them specific access to the underlying container (a vector at this point).
public:
GameObject& get_game_object(const unsigned int id);
void add_game_object(const GameObject& object);
void remove_game_object(const unsigned int id);
Adding, removing, and retrieving objects within the scene works fine.
I encounter a problem though, when attempting to iterate through these game objects.
template<class T>
void for_each_game_object(T t) const {
std::for_each(begin(objects), end(objects), t);
}
This "wrapper" works fine for my underlying class of vectors.
scene.for_each_game_object([] (GameObject& object) {
// do something in the lambda
});
But now I want to swap out my vectors for unordered_maps where the key is an unsigned integer, and the value is the GameObject
(or Camera or Light or etc.).
The for_each_game_object
loop now requires a different lambda, and exposes the key/value pair to the end-user. Is there any way to rewrite the for_each_game_object
loop to only iterate over the values of the map and ignore the ids (which are used internally by the Scene
class)?