Problems with <array>.pluck()

Twine Version: 2.9.0

I greet you.

    <<if $explored.T2N == 100>>
      <div class="index-screens-small-OC"><div><strong>Unimatter Transport</strong>
      <div><<= 3 * $OrbTransport.count("unimatter")>> unimatter for <br><<= ((4*$Oeu) * $OrbTransport.count("unimatter")).toFixed(1)>> Energy </div>
      <<if $energy > $maxenergy>><<set $energy = $maxenergy>><</if>><<= $energy.toFixed(1)>> of maximal <<= $maxenergy>></div>
      <<if $energy >= (4*$Oeu)>><<button "+1 dispatch">><<set $OrbTransport.push("unimatter"), $energy -= (4*$Oeu)>><<goto "Orb Configuration">><</button>><</if>>
      <<if $OrbTransport.count("unimatter") > 0>><<button "-1 dispatch">><<set $OrbTransport.pluck("unimatter"), $energy += (4*$Oeu)>><<goto "Orb Configuration">><</button>><</if>>
    <</if>>
    <<if $explored.T1E == 100>>
      <div class="index-screens-small-OC"><div><strong>Nutrients Transport</strong>
      <div><<= 9 * $OrbTransport.count("nutrients")>> nutrients for <br><<= ((2*$Oeu) * $OrbTransport.count("nutrients")).toFixed(1)>> Energy </div>
      <<if $energy > $maxenergy>><<set $energy = $maxenergy>><</if>><<= $energy.toFixed(1)>> of maximal <<= $maxenergy>></div>
      <<if $energy >= (2*$Oeu)>><<button "+1 dispatch">><<set $OrbTransport.push("nutrients"), $energy -= (2*$Oeu)>><<goto "Orb Configuration">><</button>><</if>>
      <<if $OrbTransport.count("nutrients") > 0>><<button "-1 dispatch">><<set $OrbTransport.pluck("nutrients"), $energy += (2*$Oeu)>><<goto "Orb Configuration">><</button>><</if>>
    <</if>>

What is going on with .pluck?
F.e. The array can be like
$OrbTransport = [unimatter, unimatter, nutrients, unimatter, nutrients].
Thing happens, if I pluck f.e. one unimatter out, it eliminates nutrients from the array. Same happens vice versa.
Can anyone explain?
And do I need to structurize differently or establish a new macro/widget when I wish to .delete only one element/instance not all?

.pluck is the method that removes something at random (documentation). You should be using it like this:

$array.pluck()

What you want to use is .delete (documentation)

<<set $OrbTransport.delete("nutrients")>>

However, you can’t really delete just one instance nutrient (it’s either none or all duplicates - or you’d need some super custom JavaScript*, which I really don’t know how to get).

In this particular case I would suggest using objects instead of arrays:

$OrbTransport {
      nutrients : 5,
      unimatter: 2
}

<<set $OrbTransport.nutrients -=1>>

EDIT: *OK, so I’ve asked and been told that you’d need to find the index of the first instance of that element then .splice the array at that index. Something similar to this

1 Like

@manonamora’s idea to use an object looks good. If for some reason you decide to keep an array, you can use a loop to search for and remove the first member reading “unimatter”.

<<for _i to 0 ; _i < $OrbTransport.length ; _i++>>
<<if $OrbTransport[_i] is "unimatter">><<set $OrbTransport.deleteAt(_i)>><<break>><</if>>
<</for>>
2 Likes

:joy:
that is so much easier to implement than a JavaScript custom code.
I forgot about the loops… you can do so much with those…

@manonamora & @souppilouliouma

I thank you both. Both snippets work.

Although, is there a reason of why there is no like $array.deleteOneOf(“name”)?

I wish you both a nice start to the weekends.

:woman_shrugging:
blame JavaScript (SugarCube is based on JavaScript, all the methods are actually JavaScript ones)