[quote=“Tilea, post:1, topic:43401”]so my config.saves.maxStates
is set to 1
to keep the save file small
[/quote]
I will assume you actually meant the Config.history.maxStates
setting.
Ideally you should only store values that change (dynamic data) during a play-through within the story variable system. Any values that don’t change from the value initially assigned during startup (static data) should be stored elsewhere, like on the special setup
object.
The two main reasons for doing the above are:
1: Reducing the size of the History system, thus the size of the Save slots.
This is important because both are stored with the web-browser’s Local Storage cache and that area has a default maximum size of between 2MB and 10MB depending on the web-browser being used and the operating system/machine/device it’s being used on.
Your changing of the maxStates setting to 1
makes the above limitation less of an issue.
2: Reducing the cost of cloning the current state of all known story variables during the Passage Transition process.
Each time a Passage Transition occurs the current state of the story variable system is cloned, the original copy is added to the History system as part of a new Moment, and the cloned copy is made available to the Passage that is about to be shown. This cloning process breaks the integrity of object references, which can cause issues if you have two or more variables (object properties/array elements) referencing the same object.
Changing the maxStates setting to 1
only controls how many States/Moments are stored within the History system, it doesn’t stop the cloning process.
eg. The following TWEE Notation example demonstrates the above.
:: Story Javascript [script]
Config.history.maxStates = 1;
:: Start
/* Initialise two references to the same object. */
<<set $first to {name: "Aaaa"}>>\
<<set $second to $first>>\
/* Display initial values, both the same! */
name (first): $first.name
name (second): $second.name
/* Alter name for one of the references. */
<<set $first.name to "Bbbb">>\
/* Display values again, both have new value! */
name (first): $first.name
name (second): $second.name
[[Next|After Cloning]]
:: After Cloning
/* Display values, both appear the same! */
name (first): $first.name
name (second): $second.name
/* Alter name for one of the references again. */
<<set $first.name to "Cccc">>\
/* Display values again, now different! */
name (first): $first.name
name (second): $second.name
Personally I would store the (static) Item Definitions on the setup object, which you can do either within your project’s Story JavaScript area.
/* Initialise the items property on the setup object. */
setup.items = {
apple: {id: 'apple', name: 'Apple'},
banana: {id: 'banana', name: 'Banana'}
};
/* Add more items. */
setup.items.pear = {id: 'pear', name: 'Pear'};
…or within your project’s StoryInit
special passage…
/* Initialise the items property on the setup object. */
<<set setup.items to {
apple: {id: 'apple', name: 'Apple'},
banana: {id: 'banana', name: 'Banana'}
}>>
/* Add more items. */
<<set setup.items.pear to {id: 'pear', name: 'Pear'}>>
(obviously I don’t know what properties you have associated with your items, so I will only include an ID and Name in my example.)
You can use a fixed length Array based story variable to track which item (if any) is currently associated with a specific slot.
/* Initialise an Array with 5 slots at startup. */
<<set $slots to Array.from({length: 5})>>
/* Associate an Item with a Slot using the item definition's key. */
<<set $slots[3] to 'banana'>>
/* Display the current contents of the slots
slot count: $slots.length
slot contents:
<<for _i, _slot range $slots>>
_i: <<= _slot ? setup.items[_slot].name : ''>>\
<</for>>
You can use a Generic Object / Map based story variable to track the ID and quantity of any item being carried.
/* Initialise the variable at startup. */
<<set $carring to {}>>
/* Add item to inventory using the Item Definition's key. */
<<set $carring['banana'] to 1>>
/* Increase quantity being carried. */
<<set $carring['banana'] += 5>>
/* Remove item from inventory. */
<<run delete $carring['banana']>>