I'm working on some mod that needs to set and get variables across saves. Here is a working mechanism I found:
Yes it is abusing the
I plan to leverage the
.
Update: The above only works for settings or values within a game. To see how to store settings globally, see comment below: https://forums.civfanatics.com/threads/how-to-persist-values-across-saves.696187/post-16793464.
JavaScript:
GameTutorial.setProperty(key, value);
GameTutorial.getProperty(key);
Yes it is abusing the
GameTutorial
, but it is also used by the game itself in Base\modules\core\ui\utilities\utility-serialize.js
. In the code comment:However, this file suggests us to use the* @description Uses property chunking to persist values into and across saves.
* Abuses the Tutorial read/write store.
* A top level "catalog" object tracks all the objects in the store.
* Each object can have multiple properties read/writen
Catalog
interface in this class, which is also used by Base\modules\base-standard\ui\quest-tracker\quest-tracker.js
for tracking quests. Based on the usage, it seems like it will store many key-value pairs for a single map object. Technically you can also JSON.stringify
a map object and store under a single key, then do JSON.parse
after retrieving it. I'm not sure which option is more performant.I plan to leverage the
Catalog
interface in my mod for now, but let me know if there are better ways 
JavaScript:
const catalog = new Catalog("MyStuff");
const obj = catalog.getObject("foo");
const old = obj.read("someKey");
obj.write("someKey", "New data");
Update: The above only works for settings or values within a game. To see how to store settings globally, see comment below: https://forums.civfanatics.com/threads/how-to-persist-values-across-saves.696187/post-16793464.
Last edited: