Background:
In my game engine I have a generic 'script parser' which is used to create game entities by parsing a script file. So in code you would have something like MyEntity* entity = MyScriptParer::Parse("filename.scr");
Any class which is to be scriptable inherits from a generic base class. Internally in the game engine there are some specific classes that use this - particles, fonts etc and this all works nicely in the parser - see extract below
std::string line;
std::getline(ifs, line);
if (line == "[FONT]") {
CFont* f = new CFont();
f->readObject(ifs);
}
else if (line == "[PARTICLE]") {
CParticle* p = new CParticle();
p->readObject(ifs);
}
...
My problem comes with how to handle user defined classes i.e classes in the games that use the game engine. The base class has an abstract method readObject
so anything which inherits must implement this method.
The issue is how would the parser know about the new class? E.g say I have a CVehicle
class the parser would now need to know to recognise "[VEHICLE]" and also be able to create a new CVehicle
Is there any way to store a class type or something in an array/map so maybe I could have a function to register a list of class types with strings to provide a lookup for creating the new instances?
Bit of a long shot and may not be possible so if anyone has other suggestions on how to approach the parsing they will be welcomed