[Tutor] Best way to save a D&D-like game's state?

Leam Hall leamhall at gmail.com
Fri May 21 21:07:34 EDT 2021


I'll share my own thoughts, though please keep in mind that I'm not the best programmer out there.

Text files for single item things that will go in short lists.
	https://github.com/makhidkarun/perl_tools/blob/master/data/encounter_people.txt

CSV (colon, actually) for datasets that need a little structure.
	https://github.com/makhidkarun/perl_tools/blob/master/data/ships.csv

JSON is my goto for very structured data storage.
	(From an archived repo)
	$ more resources.json
	{
	  "cash": 250000,
	  "cargo": {
	    "textiles": 3,
	    "spice": 5,
	    "weapons": 9,
	    "medicinals": 30
	  },
	  "location": "Birach"
	}

SQLite if the data needs to be searched, and maybe joined.
	(This is just a simple table DB with for a random item from it)
	https://github.com/makhidkarun/Names/blob/master/getName.py
	https://github.com/makhidkarun/perl_tools/blob/master/data/names.db


I also prefer staying with the Standard Library as much as possible. YAML is nice, and I've recovered from looking at XML, so my choices are based on that. If you need to do incremental saves, it depends on how many processes you're running. If you only have one instance running at a time, dumping a dict to a JSON based file is easy. If you will have multiple instances running, you'll need a database to lock the table/row/cell.

For most of my work I use very simple files and build the objects as needed. Thus a JSON file might be loaded into a dict or a line from a CSV split into a list, and then that data passed to the object creation process.

For the record, I tend to program around Traveller. It's actually much easier to code around, though earlier editions of D&D aren't too bad. If you're talking about the later editions, I would recommend a NoSQL solution like MongoDB. SQLite requires structured data before you build the tables, MongoDB/NoSQL doesn't. You can get on-line classes for both, and usually for free.

Don't worry about performance. Not yet, and likely not ever. Python and any of the above will respond faster than you can type the query.

If you're really hyped, figure out the basics and then get a free AWS account. You can use SQL or DynamoDB, Python, and AWS Lambda for lots of things.

Leam



On 5/21/21 8:25 PM, boB Stepp wrote:
> I am contemplating the design of a D&D-like game where I will
> prototype various ideas I have in Python.  Currently I am pondering
> how such games typically have their state saved.  I imagine there will
> be plenty of custom data types, which represent the state of the game
> world when the players save.  I imagine the data involved could reach
> sizable proportions, depending on how complex and rich the game
> becomes.  The first thing that comes to mind is the pickle module,
> which my understanding is that it can save and restore any custom data
> types done in Python.  Next that comes to mind is JSON, but my
> understanding is that it is limited in what it can save and restore
> without additional work by the programmer.  Still staying in the
> standard library I suppose an SQLite database could be made to work.
> 
> Questions:
> 
> 1)  First off, am I thinking along the correct lines of doing this, or
> is there a better way?
> 
> 2)  If it does become true that sizable amounts of data need to be
> stored, which of the above options is most performant?  Thinking about
> this I suppose that in the case of large amounts of world data one
> would want to divide up the data into logical units and only load what
> is currently needed at any point in the game.
> 
> 3)  Algorithmically, how would one perform incremental saves?  I know
> this is done by many programs, so that only the changes to the
> original data are stored, which saves (pun intended) on the amount of
> data that needs to be saved.
> 
> 4)  I am not adverse to considering third party libraries.  If I do
> so, YAML also comes to mind.  I have no idea what other formats might
> be worth considering.  I hope XML is not a serious option as I have
> heard that it can turn into a mess with data files not easily parsable
> by human readers.  Though I must add that some of the programs I work
> with at work use XML and I have had no difficulty reading and editing
> their data files at need in small ways, so perhaps I am unduly
> prejudiced against XML?
> 
> 5)  If this project proceeds to fruition then it is entirely possible
> that portions or all of it will need to be rewritten in a more
> hardware performant language.  If that proves to become the case will
> that alter any of your answers to the above questions?
> 
> TIA!
> boB Stepp
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 

-- 
Site Reliability Engineer  (reuel.net/resume)
Scribe: The Domici War     (domiciwar.net)
General Ne'er-do-well      (github.com/LeamHall)


More information about the Tutor mailing list