How do I read or write data in a saved game without loading that saved game?
In some situations, you may want to be able to get certain information from a saved game without loading it. For instance, you might want to include information about the saved game on a custom load game screen, you might want to keep track of whether the player has ever beaten your game before, or you might want to store some kind of settings that aren't restricted to one save file. In some of these cases, you may even want to be able to change this information without loading the saved game.
The only type of data you can get out of saved games without loading them is their global variables. If that's the form your data is already in, then it's easy enough to read them--just use import globals to read it into a variable:
# this reads global variable 57 from slot 2 into the variable x x := import globals (2, 57)
Exporting them is slightly more complex, since export globals can only copy your own globals, but still not difficult. You'll probably want to save your version of that global, and restore it afterward:
# this writes the contents of variable x into slot 2, global variable 57 variable (y) y := read global (57) write global (57, x) export globals (2, 57, 57) write global (57, y)
If you're going to be doing this often, you might want a script that will do it for you:
# this is an export globals equivalent to the single-global version of import globals script, save global, slot=0, which=0, value=0, begin variable (temp) temp := read global (which) write global (which, value) export globals (slot, which, which) write global (which, temp) end
Globals are the only things you can read this way. If you want to read some other kind of data, such as the leader's level, you'll have to have the game store it as a global when you save:
# pops up the save menu, first storing the leader's current level in global variable 76 script, save menu with leader level, reallysave=true, begin write global (76, get hero level (find hero (leader))) save menu (reallysave) end # gets the leader's level from a given save slot script, get saved leader level, slot=0, begin return (import globals (slot, 76) #end
If you want to save tags, bitsets, or other kinds of true/false values, you can store several of them in one global and then just import and export that global, so you don't have to waste as many of your globals on duplicating your tags.