Bad evaluation when removing element from array

Using: Twine 2.3.14, SugarCube 2.34.1
Been using Twine for a month now (with little prior experience in JS).
In my current project I use an array by the name of “$inventory” to keep track of what the player picks up.
The issue stems from needing to remove a single element of the $inventory array to be deleted.
I have looked through countless discussions surrounding this, but none of the solutions have worked for me.
Not sure if I am doing something wrong or if the answers are outdated.

Assuming…
$inventory = [“apple”, “apple”, “bread loaf”]
$fedFood = “apple”

<<set _foodIndex = $inventory.indexOf($fedFood)>>
<<run $inventory.deleteAt(_foodIndex)>>

With this solution the error reads “Error: <>: bad evaluation: State.variables.inventory.deleteAt is not a function”.
Thank you for your time.

Hmm…that code works for me. Maybe try displaying $inventory before you run deleteAt in case something has changed it somehow? You’d get that error if $inventory were a string or a number or an object instead of an array…

Thanks! It looks as though I had added elements to the inventory array by using

<<set $inventory += $variable>>

rather than using

<<set $inventory.push($variable)>>

which was the source of the problem.

1 Like