Saving multiple tables to file

My current WIP has an achievements system that’s moderately complicated, and I wanted to make it persistent even if the player used UNDO a lot. That’s a job for write-to-file, right? It took a bit of restructuring to get all the relevant data into permissible formats (everything in tables, indexed text only), but I got the first part working fine. And then everything started going crazy. Inexplicable crazy.

Eventually, I figured out the problem: I’d been saving all my tables to one file. If I split them all out into separate files, everything worked like a charm. If I didn’t, things vanished in unaccountable ways.

Is this how it’s meant to work? This may seem blindingly obvious to people with the right background, but I don’t think that it’s ever actually mentioned anywhere in the docs, and it probably needs to be. (I guess if you go and look in the section on writing text, it’s kind of implied by the presence of an ‘append’ command separate from a ‘write’, but this is pretty non-obvious.)

So you mean that you expectedwrite File of Data from Table of Data; write File of Data from Table of More Data;to store both tables in the same file, rather than overwriting the file each time? While I’ve never read WI 22.12 this way before, I’ll admit that nothing there says otherwise. I think you would be on solid ground filing a documentation bug.

The problem with writing multiple tables to the same file is: how do you plan to read multiple tables from the file, later? Inform provides no way to do that. I believe the way tables get written to a file detaches them from their name, so you can write a table to file and read it back into a different table (as long as they have the same schema; I don’t know what happens if you try to read a file written with a different schema, but it’s probably “unspecified”). If you had multiple tables in the same file, how would Inform know which one to read back in? Just use separate files, is my recommendation.

It doesn’t throw up any errors until you try to use the table.

…by looking at the table name? Evidently I was overestimating the complexity of the process.

Really? That’s pretty entertaining. Still, that’s an acceptable result (by definition) from something that produces “unspecified behavior”. Whenever you’re reading documentation, remember that “unspecified” might mean “causes demons to fly out of your nose”, and you would probably prefer that didn’t happen.

Yeah, the entire “reading/writing tables from/to files” subsystem is about as trivial as it can possibly be and still get the job done most of the time. As far as I can tell, it just dumps the table data to the file upon writing, and to read it just reads things in and prays they’re in the right format. It definitely doesn’t save the table name or anything like that.

This is actually a feature. Imagine a persistent game (like Kerkerkruip) in which, each play, whatever your last completed game did modifies the world in some way, making life different for the new guy. You have a Table of Dark Deeds; every time the game ends, you write the Table of Dark Deeds out to the Deeds file. Upon starting a new game, you read the Deeds file into the Table of Ancestral Legends, so that you know how to set up the world, and use the new game’s actions to populate a new Table of Dark Deeds. This clearly wouldn’t work if a file was bound to the in-game table that wrote it out. (I kind of want to write a Kerkerkruip extension to do this now.)

It does save the table name, but it’s ignored when reading.

I would one day like to write a JSON extension that could do all this (and more).

So I saw, when (in desperation) I started opening up the file in a text editor and directly checking what it was actually up to. (Which was how I ultimately figured out what the problem was.)