hyiltiz

Serialize/dump/pretty print game state into file system (save game state to disk)

Recommended Posts

hyiltiz    296

I've been looking into saving game state to file system. Specifically, serialize all the lua tables relevant to the game state into json. This will pose two obvious challenges: a) some game objects may be recursive; b) functions cannot be sensibly serialized in general.

There are quite a few libraries and methods to go about this [1]. And the game already uses a few methods (namely `./scripts/json.lua` and `./scripts/inspect.lua`). The idea is that we can iterate over the common game tables such as `TheWorld`, `ThePlayer`, `TheNet` etc. recursively, and convert each key-value pair into string, indenting appropriately. This is already implemented in `debugtoosl.lua:dumptable()` as well as `table.inspect()` both of which can serialize/convert a `lua_object` into `String`, which can then be printed or saved into a file.

The issue is, when I tried `table.inspect(ThePlayer)`, or `table.inspect(ThePlayer.components)` or even `table.inspect(ThePlayer.components.Builder)`, the game simply ate all my 8G of RAM and got killed by the OS. I wonder if it would be possible to write to a file incrementally instead of creating the whole representation in RAM, holding the string in RAM then writing it to a file.

 Using common tools to inspect (deeply nested) `json` data, it would make it easier for Modders to learn about how this game works in general and would prove to be a very good mental model and guideline for reading and modding the game source code.

In the end, I'm looking to get something like this (after loading the `json` into some `json` inspector; heck even Chrome can inspect `json` files with tree-like inspection support):

_G
├── TheNet
├── ThePlayer
│   └── Components
│       └── Builder
└── TheWorld
   └── SeasonManager

 

p.s. This would also make tools like DSTed [2] very powerful, enabling users to traverse game state that was stored during game play.

[1] http://lua-users.org/wiki/TableSerialization

[2] 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now