Problems adding to complex variables when writing episodic stories

Twine Version: 2.8.1
Sugarcube 2.36.1

I’m writing a story with a dressing up element, and have my clothing and other objects encoded in a $objects variable as below in a init passage:

<<set $objects = {
“black boots”: { price: 50, type: “shoes”, image: “Images/Shoes/blackboots.jpg”, inv: 0, },
“black fedora”: { price: 50, type: “hat”, image: “Images/Hats/BlackFedora.webp”, inv: 0, },
}>>

All was working well except as I’m updating the story people using an old save can’t access any new items I come up with as (I think) the initialisation passages don’t run again.
I can’t add solely the new items to a new passage when I publish a new “chapter” as that seems to remove the old objects,
and if I write all the objects as a massive list each time I add to the story then that will overwrite the inventory/ object location data (the inv above) again messing with old save files.
Essentially I’ve been using $objects as a spreadsheet, but can’t add new rows to it when I publish a new “chapter”. Any advice appreciated

Thanks
Ohara

Hi there!

StoryInit loads only at the start of a game (playthrough). If you update the StoryInit passage and a player loads an old save, the new elements won’t be added to that old save.

UNLESS: you use some Config API. Namely Config.saves.version and the Save.onLoad.add()
You can see an example made by TME on reddit here

1 Like

Thanks. The reddit example looks like it could do what I want but will probably be quite laborious to code for each thing I’ll need to do it with
What I guess I’m really after was a way of pushing additional line items (like a “pink hat”…) into my $objects variable after it was initialised without overwriting it as I assume I can do that outside of Storyinit and then wont have to mess around with save files.

You can use the <<set>> macro to add additional properties to an existing Object.
eg.

<<set $objects['purple gloves'] to { price: 50, type: “gloves”, image: “Images/Gloves/purplegloves.jpg”, inv: 0 }>>

However, as explained by Manon, if the end-user loads a Save from a point in time before the 'purple gloves' property was added to $objects then that property will cease to exist.

Note: Ideally static data like the Item’s price / type / image should not be stored in a Story variable, because that data doesn’t change over the life-cycle of a specific play-through.

Great thanks. So I’ll shift all my static data into a non story variable (will have to look up into what - only really figured out $ and _ variables) and rework the non static ones that are causing the issue (really just inventory) into something better able to survive the load process. I’m sure there’s plenty of examples out there - will just have to do some research
At least I learned this relatively early in writing this thing.

Read about the special setup variable, that SugarCube creates for purposes like storing static data.