I am trying to save data about various entities in each Level in my game in a JSON file and load it at Runtime(Like a Scene file in Unity). I can save simple stuff like floats and ints and parse them back to with functions like std::stof. I can even handle stuff like a Vector3(3 floats).
Say, I want to save a User-type like CollisionSystem which resolves Collision between entities with a ColliderComponent. CollisionSystem does not contain any data and only Behaviour. How can I infer the Type from just the string and create an Object of CollisionSystem?
{
"MovementSystem" : {},
"RenderSystem" : {},
"CollisionSystem" : {},
"entities" : [
{
"PositionComponent" : {
"x" : 10,
"y" : 0,
"z" : 10
},
"SpriteComponent" : {
"source" : "\"assets/textures/Goblin.png\""
},
"ColliderComponent" : {}
},
{
"PositionComponent" : {
"x" : 0,
"y" : 0,
"z" : 30
},
"SpriteComponent" : {
"source" : "\"assets/textures/Hero.png\""
},
"ColliderComponent" : {}
}
]
}
The Brute Force method I could think of is a Switch case statement in the Parser like so:
System* GetSystem(string system_type)
{
switch(system_type)
{
case "MovementSystem":
return new MovementSystem();
case "CollisionSystem"
return new CollisionSystem();
.
.
.
default:
return nullptr;
}
}
which can be called like:
System* moveSystem = GetSystem("MovementSystem");
System* collisionSystem = GetSystem("CollisionSystem");
moveSystem->update();
collisionSystem->update();
However, I don't think it's the right way to do this. I am new to C++. So I might be missing some language features that might do this. Is there an elegant way to do this?