How to get a single value from generic object that was picked randomly?

Im creating a simple card game (smth like black jack) using twine 2.3.16, sugarcube 2.36.1.
I have a problem. I need to pick a card randomly from 36 cards. Its not a problem, im using array.pluck for that. But every card has at least 2 values, e.g. type: spades and value: 6.
So, I need to pick card randomly and return just 1 value, and print it to reach my goal.

I tried to do it this way

<<set $s6 to {type: “s”, num: 6}>>
<<set $s7 to {type: “s”, num: 7}>>
<<set $d6 to {type: “d”, num: 6}>>
<<set $d7 to {type: “d”, num: 7}>>
<<set $deck to [$s6, $s7, $d6, $d7]>>
<<print $deck.pluck(function(x){return x.type})>>

I got just [object Object]

Then I tried to pull everything inside, but I dont know how to get random card from this generic object and then return a single value of this card.

<<set $deck to {
s6: {type: “s”, num: 6},
s7: {type: “s”, num: 7},
d6: {type: “d”, num: 6},
d7: {type: “d”, num: 7}

Help me please, i already lost few days on it :smiling_face_with_tear:

1 Like

As a suggestion. You might want to check the documentation for things you’re attempting to use, <Array>.pluck() does not take arguments.

Regardless. You were on the right track with your first attempt.

You received [object Object] because that’s what the array contains, objects with two properties.

Try the following:

<<set $s6 to { type : "s", num : 6 }>>
<<set $s7 to { type : "s", num : 7 }>>
<<set $d6 to { type : “d", num : 6 }>>
<<set $d7 to { type : "d", num : 7 }>>

<<set $deck to [$s6, $s7, $d6, $d7]>>
<<set $card to $deck.pluck()>>

You picked: <<= $card.type>><<= $card.num>>

Once you’ve picked a card, you can simply access the properties you defined on the objects to get the values—as shown above.

NOTE: The <Array>.pluck() method removes the picked member from the array. If you want the picked member to remain, switch it out for the <Array>.random() method.


Brilliant! Thanks a lot =)