I'm working on a 2D game engine and I continuosly run into template problems. So, for this one, I've got a templated function like this:
template <class T>
T *S2M_CreateObject(int x, int y) {
return new T(x, y);
}
now, I would like the game to load the level data from a file, and that includes loading and instantiating Object-derived classes, so I made an std::map like this:
map <string, Object *(*)(int x, int y)> o {
{ "warp", &S2M_CreateObject<Warp> }
};
which stores a string that I will be using in the level editor to refer a determined class and maps it to a function pointer that would create an instance of that said class.
I hope you get the idea, this is the approach I like the most but it is not working. However, it works if I delete the Warp template specifier (Warp is a derived class of Object), but that is not the goal. I know I could create a function for every object type I have defined in the game, but since I'm programming a game engine, I can't figure out how many Object-derived classes the user will create and I cannot expect him/her to program each function.
Any other way I can do this?