I'm trying to serialize/deserialize a game scene for network sending/receiving and saving/loading from/to disk.
My game engine uses Nodes and Components and as such those are the only objects that need serializing. A scene might look like this:
Root Node
- Node
- SpecializedComponent
- SpecializedComponent
- Node
- Node
- Node
- Node
- Node
- Node
- Node
- SpecializedComponent
- Node
A Node is basically this:
class Node {
map<String, Node> mChildren;
map<String, Component> mComponents;
uuid_t mId;
Node* mParent;
};
A SpecializedComponent is basically this:
class SpecializedComponent : public Component {
uuid_t mId;
Node* mNode;
};
I'd like to either use YAML or JSON for my text representation of this. I have Qt, Boost and any other library I'd like at my disposal so dependencies are not an issue. In fact, nodes are already Q_OBJECTS so I have reflection.
Despite reflection, deserializing this properly back to a C++ tree structure seems to be a problem.
Optimally, I'd like an elegant and efficient solution to serializing/deserializing a structure like this into either binary or text format.
A descent recursive parser usually it's the simpler and sufficiently powerful option to handle the reconstruction part. I could try to setup some code to show in concrete, in pseudocode some thing like this:
Node *parse(stream s) {
content = new Node;
s >> content >> nsons;
for (int c = 0; c < nsons; ++c)
content->addChild(parse(s));
return content;
}
Of course when a component is read we must check the type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With