Firstly. memorize()
is a function, not a macro. Knowing the proper terminology for things is important.
Secondly. I’m not exactly clear on what you’re confused about:
- If you’re confusing arrays and generic objects, then the values you’re attempting to set are generic objects. If this is the issue, neither would be merged with the previous value anyway.
- If you’re confused about how
memorize()
and friends work upon, then they operate on a key↔︎value store, not an array. None of its official documentation remotely suggests that it works that way. Neither its signature, nor its description, nor its, admittedly few, examples.
Regardless. When you set a key to a value its previous value, if any, is overwritten by the new value. To put it another way, if you set a key to a value, then set the same key to a new value, recalling the value of that key afterwards will yield the new value as-is.
For example, using your example code:
/* 1. Set the key `ends` to the value `{ sorry : true }`, which is a generic object. */
<<run memorize('ends', { sorry : true })>>
/* 2. Reset the key `ends` to the new value `{ lame : true }`, which is a generic object. */
<<run memorize('ends', { lame : true })>>
/* 3. Reset the key `ends` to the new value `{ lame : true }`, which is a generic object. */
<<run memorize('ends', { lame : true })>>
After doing the above, the value returned by the call, recall('ends')
, will be an equivalent duplicate of the exact value set in #3 above, { lame : true }
.
Anyway. If your goal is to keep a record of all reached ends, then I’d suggest using a generic object as you’re doing now, not an array. For example, memorizing a reached ending while keeping all existing ending key↔︎value pairs, if any:
<<run memorize('ends', Object.assign(recall('ends', {}), { sorry : true }))>>
Later, to memorize new endings while keeping all existing ones, if any:
<<run memorize('ends', Object.assign(recall('ends', {}), { lame : true }))>>
To give an explanation of what’s going on, here’s the first example rewritten step-by-step:
/*
Get the current value of the key `ends`, if it exists,
elsewise yield a new empty generic object.
*/
<<set _ends to recall('ends', {})>>
/*
Merge the new ending key↔︎value pair into the existing
endings object.
In this case, equivalent to: <<set _ends.sorry to true>>
*/
<<set _ends to Object.assign(_ends, { sorry : true })>>
/*
Set the value of the key `ends` to the updated endings
object.
*/
<<run memorize('ends', _ends)>>
Checking endings could look something like:
<<set _ends to recall('ends', {})>>
Player reached <<= Object.keys(_ends).length>> endings.
<<if _ends.sorry>>
Player reached the "sorry" ending.
<</if>>
<<if _ends.lame>>
Player reached the "lame" ending.
<</if>>