Delete a object in an array

So I have this array of objects and I want to delete a specific one.
I tried .pluck but I got random results when I put…

<<set $inventory.pluck($greenApple)>>

It will pluck any of the objects at random.
And when I use any of

<<set $inventory.deleteAt($greenApple)>>
<<set $inventory.deleteAt($yellowApple)>>
<<set $inventory.deleteAt($rottenApple)>>

it deletes the first item in the array. (I guess they have no value or is == 0)
and when I use…
<<set $inventory.delete($greenApple)>>
Nothing happens.

They way it is set out is
StoryInit…

<<set $inventory = [] >>

<<set $greenApple = {
	"Img" : "[img[img/greenApple.png]]",
	"Name" : "Green apple",
	"Price" : "4 coins",
	"Description" : "A nice green apple.",
}>>

<<set $yellowApple = {
	"Img" : "[img[img/yellowApple.png]]",
	"Name" : "Yellow apple",
	"Price" : "5 coins",
	"Description" : "A wierd yellow apple.",
}>>

<<set $rottenApple = {
	"Img" : "[img[img/greenApple.png]]",
	"Name" : "Rotten apple",
	"Price" : "1 coins",
	"Description" : "Ew... a rotten apple.",
}>>\

Passage…

<<set $inventory.push($greenApple, $yellowApple, $rottenApple)>>\
\
<<for $i = 0; $i < $inventory.length; $i++>>\
<<set $item = $inventory[$i]>>\
<<print $item.Name>> - <<print $item.Price>>\
<</for>>\

Another passage with the examples at the top of the page.

I see two mistakes you’re making.

For one, array.pluck() doesn’t take arguments. So just array.pluck() alone will return a random item and delete it from that array.

And two, as you’ve noticed, array.deleteAt() requires a number to be passed to the function. That number is that item’s position in the array (starting at 0 for the first member). However, array.delete() deletes a specific item based on its value, not its position.

The issue you’re having is that $greenApple will be an object reference, and I believe that reference will break as soon as you navigate away from the passage. It’s a bit difficult to explain without going into unnecessary detail, but to put it simply: deleting it that way won’t work.

The easiest way to do it would be to put something like this in StoryInit:

/% 
        Define all of the game's items here.
        As long as this list is defined in StoryInit, we don't need to save it to the state.
        Because of that, I'll use 'setup', Sugarcube's predefined global object 
%/

<<set setup.gameItems = {
        "greenApple": {
        	"Img" : "[img[img/greenApple.png]]",
	        "Name" : "Green apple",
        	"Price" : "4 coins",
        	"Description" : "A nice green apple."
        },
        "yellowApple": {
        	"Img" : "[img[img/yellowApple.png]]",
        	"Name" : "Yellow apple",
        	"Price" : "5 coins",
        	"Description" : "A wierd yellow apple."
        },
        "rottenApple": {
        	"Img" : "[img[img/greenApple.png]]",
        	"Name" : "Rotten apple",
        	"Price" : "1 coins",
        	"Description" : "Ew... a rotten apple."
        }
}>>

/% These are your items %/
<<set $inventory = []>>

And then later on in your game code somewhere:

/% I'm leaving rottenApple out to show that this list is different the list of game items %/
<<set $inventory.push("greenApple", "yellowApple")>>

<<for $i = 0; $i < $inventory.length; $i++>>\
        /% 
                if $inventory[$i] is equal to "greenApple", 
                then setup.gameItems[$inventory[$i]] is the green apple object 
        %/

        <<set $item = setup.gameItems[$inventory[$i]]>>\
        <<print $item.Name>> - <<print $item.Price>>\
<</for>>

Doing it this way, you can remove the items from your inventory with $inventory.delete("greenApple"), or add them with $inventory.pushUnique("greenApple") to ensure only one instance of that item, or simply $inventory.push("greenApple") to push as many of the same item as you want.

I hope that helps.

3 Likes

That’s perfect. I’m seeing it in my head as one object now, like a backpack with items in it.

1 Like