I've learned that storing and loading game saves is commonly done by serializing runtime data into binary files and then loading in those binary files de-serialized to reconstruct the runtime state respectively
A Python library pickle allows for serializing and de-serializing entire objects. It makes intuitive sense and ease of use then to compose all relevant world state variables into a single object
WorldState, which will be serialized on saves and reconstructed on loads.
class WorldState(metaclass=Singleton): relationships: dict[Entity, dict[Entity, Literal['friendly','neutral','hostile']] questlines: list[QuestLine] locations: list[Location] items: list[Item] entities: list[Entity] player: Entity time: float
This works, but I'm not sure how to handle the initial game state, before any saves have occurred. I'm left with a chicken/egg scenario, where in order to load game state there must already be serialized binary files to reconstruct, which happens on save, but in order to save game state into serialized binary files there must already be a
WorldState instance constructed to save.
If I were using XML/JSON to store game state I would simply write the initial content manually which would be overwritten on saves to be loaded again later, but since I am serializing them into binary I obviously cannot do that.
The only solution seems to be adding an
if to check whether any game saves already exist, and to manually construct all the initial objects if not, either in the source-code (using dependency injection containers) or loaded from xml/json files that would exist only to represent the initial game state since every other load after the first will be based on serialized binary files, not json.
Am I missing something? Is there a better approach? Should I just represent game state in human-readable formats like json? What techniques are common in industry?