How do I dynamically delete variables in Harlowe?

Twine Version: Harlowe

Hi there! I’m very new to Twine. Apologies in advance if this question has been asked elsewhere (I’m sure it has), but I can’t find an answer that I’ve been able to understand.

In my story, I have the player unpacking a bag with 5-6 objects in it. As the player finds places to store each item, I would like for the placed items to ‘disappear’ out of the description of the bag… and I have no idea how to make it happen.

For example: If the starting state is “Your purse contains a compact, a handkerchief, and a frog.” After you put the compact away, ideally the purse description would then read, “Your purse contains a handkerchief and a frog.”

The only way I can think to accomplish this is by making a ton of conditional statements for each possible iteration of the bag, but that seems wildly inefficient. Is there a simpler way to set this up in Harlowe? (ELI5 answers greatly appreciated.)

Have a lovely day, everyone! :slight_smile:

1 Like

This link will answer your question… Cookbook - Harlowe Arrays

It is on the advanced side of programming, but it is the most flexible and easiest to maintain once it’s in place. There’s a reason why inventory management is used in the array tutorial link.

However, there are more straight forward ways (but more work to maintain) to do what you want to do, but let us know if the link above works for you first.

1 Like

Thanks very much for this! I will give it a shot. Thankfully, the game is pretty small/self-contained, so maintenance should theoretically not be a major issue. I’ll reply back if that doesn’t smooth out the issue.

2 Likes

First, based on the title, I’d like to clarify that, at least in Harlowe 3.3, story-wide variables cannot be deleted. Temporary variables will be removed during a passage transition, but story-side variables remain until the story ends when the tab or window is closed.

For future searches, and those coming to this thread later, the key part of the example from the Cookbook is the following:

(for: each _item, ...$inventory)[\
        _item (unless: $inventory's last is _item)[, ]\
].

In the above code, the (for:) macro performs an action on every entry in an array using the keyword “each”, the spread out operator, “…”, and a story-wide variable containing an existing array. This places each entry into a temporary variable, _item, which can be accessed inside of the associated hook. In the hook, the (unless:) macro is used to place a comma and a space, found within its own hook, after every entry in the array unless the value of _item is the same as the last entry in the array. If this becomes true, the comma and space are not used.

After each line, there is also a slash, “/”. This runs all the lines together, allowing the output to be on one resulting line, despite the code itself across multiple lines. This creates a comma separated listing of things in the array without an extra comma at the end of the list.

Removing from arrays (in Harlowe 3.3)

There are two ways to remove from an array.

The first follows the “only arrays can affect arrays” rule of Harlowe. To remove from an array, you need to create a new array with the value to remove. Then, the hyphen (subtraction) symbol can be used between the two arrays to remove entries with the same value(s) in both:

For example, to remove an entry with the value “A”, it would be the following:

(set: _exampleArray1 to (a: "A", "B", "C") )
(set: _newArray1 to _exampleArray1 - (a: "A") )

The second, and more complex answer, is to use a lambda to create a new array without the same value:

(set: _exampleArray1 to (a: "A", "B", "C") )

(set: _newArray1 to (find: _item where _item is not "A", ..._exampleArray1 ) )

In the second example, the (find:) macro is used to find all entries where some comparison is true, creating a new array in the process. In this case, find every entry where its value is not the value “A” across all entries, again using the spread out operator and a variable containing an existing array.

In either way, the string value “A” can also be replaced with a variable to more dynamically remove entries when needed.

2 Likes

Thank you for this! I’ll see if I can get my story working.

1 Like