I’m trying to follow along with Rama’s guide to saving to binary. I’ve hit a wall in my understanding though, and could use some help.
I have an increasingly complicated class hierarchy, and I want to be able to save/load them to/from disk, ideally by passing in the parent class object rather than creating separate functions for every single child class.
Suppose I have an hierarchy like this:
class Parent
{
Variable1
Variable2
Variable3
}
class ChildA : public Parent
{
ChildAVariable1
}
class ChildB: public ChildA
{
ChildBVariable1
}
I have a whole library of objects (UObject) which will each be one of the above classes. I want to be able to save them all to disk in my own file format (let’s call it .customfile).
The place my thinking is breaking down is understanding how to deal with all the different subclasses. According to Rama’s tutorial, it seems like each individual variable in the class needs to be put into binary separately (is that correct?), and in the right order. So if I was just saving the Parent class, it would be easy. But do I need to do some conditional stuff inside the saving function to test what class I’m passing in, and save all the child class variables, or is there a better way?
And when loading back, suppose I have a folder of saved files like this:
Object1.customfile
Object2.customfile
Object3.customfile
I don’t initially know whether they are Parent, ChildA, or ChildB classes, so how do I know what to pass into the function to receive the data? Does the class perhaps need to have a variable for what class it’s creating? For example, ChildA would have:
UClass* ObjectClass = ChildA::StaticClass();
If that was the first variable saved/loaded from disk/binary, then it could run the rest of the loading operation conditionally based on that variable. Would that work? Is there a better way? A bit confused and could use some help!