0 votes
by (120 points)

Using: Sugarcube2.28.2, Twine 2.2 (I believe)

Now, this is more specific than my question title states; I understand how to delete an item from an array via array.delete("itemhere").

My game has the ability for a player to buy an item more than once. Each time, it places the item in the inventory as a new, separate variable in the array.

As far as I'm aware, using this, will delete all items with the same name. In example...

<<set $items to ["apple", "apple", "orange"]>>

<<run $items.delete("apple")>>

Will delete all items with the name "apple" from the array.

What I want to do is just delete ONE of the "apple"s. But, I won't always know the location of the apple in the array.

Is there a way to get it to remove a singular apple, so one apple still stays in the array?

Thank you for any assistance! I've been looking for an answer for a while now (weeks even, oof), with no luck.

1 Answer

0 votes
by (61.5k points)

You can delete a single item by finding the index of the item you want to delete and deleting at the index instead:

<<run $items.deleteAt($items.indexOf("apple"))>>

 

by (37.1k points)

FYI, the Mozilla Developer Network (MDN) site's JavaScript documentation, such as its Array documentation, may help you find solutions like this in the future.

Hope that helps!  :-)

Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...