Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Folia

A minimal framework for narrative games. · By Axile

save/load behavior around function objects

A topic by ironlantern72 created 21 days ago Views: 97 Replies: 9
Viewing posts 1 to 2

today i’ve been messing around with the save/load feature, and found out a few things:

  • any function object nested inside a global (e.g. T_GLOBAL_TABLE = { f = function() return 72 end}) does not survive save-reload
  • closures don’t seem to survive node transition, unless they’re stored in a global
    • storing a closure nested inside a global like in the example above works for the duration of the node, jump()ing to a different node does not immediately set the closure to nil, but makes it silently return nil instead
  • closures never survive save-reload

these seem like LOVE/LuaJIT quirks, so it’s not really a complaint, i just found it worth documenting. the short way to document it is “function objects don’t get saved”.

(+1)

That's correct, yeah, there's not really a straightforward way to persist those types so they just get ignored by the save system. Closures getting trashed by jumps is a little bit awkward, though; I'll see if I can work out a mitigation for that, or at least document it.

i have not yet tried passing closures between nodes as parameters, though i don’t imagine that’d sail any better.

(+1)

Yeah, node parameters need to be saved so we can restore the state correctly when the player loads a save file, so passing in a non-saveable type like a closure is gonna cause problems.

(1 edit)

a related suggestion, i suppose: maybe exclude some globals from being serialized to avoid trashing? from my testing, it seems that story modules get executed on every launch. this makes serializing any globals set in module body pointless (or even harmful/confusing), as they will be overwritten on game start. you could allow the game to set a glob string to filter globals by (flat or recursive). example:

prog = {money = 700, day = 1}
events = {
    { id = "merchant", day = 3, is_available = function() <...> end }
}

--only save children of prog recursively;
-- default glob would be ** for backwards compat
setSaveDataGlob("prog/**")
(1 edit)

or a less unixy form that might be easier on the implementor (not sure)

setSaveDataFilter(
{
    { "prog", "persistent", }, --everything in <global>.prog.persistent down recursively
    { "stats", { "lifetime", "thisRun" } }, --everything in <global>.stats.lifetime and <global>.stats.thisRun
    { "settings", "!someStupidHoldover" }, --everything in <global>.settings minus <global>.settings.someStupidHoldover
-- in short: filter is a list, strings are exact matches, nested lists are forks, ends of lists are wildcards, special syntax for negation

} )


this might be overengineered. a plain list of strings + a special default value for “include everything” is realistically enough

Ahh okay I see what you’re doing here now. Yeah, I’m not sure if I want to add globbing machinery but I understand why you’d want to have some global constants that the save system doesn’t touch.

In this example it looks like you’re storing variable/saveable data in prog, and an unchanging list of storylets in events, so would it be enough to have a command just excludes specific identifiers from the save system? (like dontSave("events") or something)

the example differs from my actual material so far. i have been placing “constants”, the statusLine bypass string, lookup tables related to the “constants”, etc in globals. i’ve packed all persistence data into prog/**. it’s a whitelist kind of situation.

a flat filter could be complemented with a whitelist/blacklist toggle, that should cover me and probably anyone who comes after me.

i’ll also note i’m willing to take a swing at implementing the features i request if given source access. they seem to be piling up rather quickly, it’s only fair i contribute back.

Okay, here’s what I’ve got so far:

  • dontSave() excludes named identifiers from being saved. You can call it with a single string or a table, and you can call it multiple times to add more identifiers to the list.
  • onlySave() does the opposite, excluding everything but the listed identifiers (and has the same signature/usage pattern as dontSave) (also the do-not-save list takes priority if there’s overlap for some reason)

Currently I’d prefer not to make the save filtering more complex than this (for UX/documentation/support reasons, the actual code isn’t a problem) but: I’ve also added a mechanism for extending the engine with an _init.lua file that gets run at startup and has access to all the stuff inside Folia, so if you or anyone else wants to adjust how things work in the core you’ll be free to do that without having to make a custom build or w/e.

i hold onto the potential future where i reimplement the interpreter, so i’d personally stay clear of _init. it would erode the API boundaries and make reimpl harder, and trying to hack folia from inside it would probably be a lot of headbutting walls