Python Forum

Full Version: Looking to Save Data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm very new to both Python and this forum, so please forgive me if I don't include all the information needed or I've posted in the wrong place.

I am trying to make a game, which I suspect is pretty common. A friend of mine walked me through some of the basics and I was able to create a simple procedural generator that designs planets. It is essentially a mad lib that the program fills in itself with the random module and some lists. I want to use that program as a sample for a jumping off point, though this is now an old project and I'll probably use what I learn from here to make something else.

Anyway, to give an idea of where I am coming from I would have to state that my inspiration was the Legends mode of Dwarf Fortress. I loved how these complex, and at times ridiculous, stories would evolve in that game's generator and I would sometimes create new games just to read these stories. With that said I am trying to take that planet generator, or whatever I end up making that is similar, and allow the program to save each planet it creates and even develop entire systems, using those planets, that the user can then access like a reference library and read the descriptions. I would also like to be able to save all that procedurally created content so the user could close down the program, open it back up, and load all that back up or even another save file if you make multiple saves.

The question is, how would I do that? I've lost contact with my programmer friend a while back, for reasons I'm unsure of, so I can't ask him. I've tried looking it up and have slogged through multiple posts and videos, all of which I'm unsure ever gave me the answer I was seeking.

I'd appreciate any help anyone could give in pointing me in the right direction, at the very least, and I would be glad to provide any information you may need.
How you would do the export (saving) is secondary question and it will depend on the answer of the first question - what (i.e. what kind of data) you want to export and in what format. I am not familiar with the the game you refer to, but quick look at the wiki, suggest that there are two types of information - textual (incl. some configuration options) and visual (images/maps). So I guess your game will have something similar will be in your game, right?

some options (not exhaustive list) - you can export text information as json file or xml file (xml is what the other game uses).
same for config options or you can use "native" config file support in python.
images/maps - as image files (e.g. jpg) or you can convert image to string and save it as text information (see above). Of course you have to implement also the reverse process.
That is if you want to use plain text files. As an alternative you can use database (e.g. SQLite) or some of the other options for object serialization in python like pickle.
I know this is very broad answer but it's also true for your question.
To add to @buran's post. Keep it simple. The game configuration should not be in the same file as the player's config info. For example.
Thanks for the responses so far! I appreciate any help I can get...

In my search I've found someone explaining the pickle module, but he made it seem as though that was for very light data, like serial numbers. Would that work with saving and loading the plugged-in generator text data? If so, I might be able to get to work on that right away!

Don't worry about map image generation. I'm working with text only. Trying to keep it simple for my first project, even though it is still pretty complicated for me, it seems. Haha!