Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Using Pluck () with a safeguard

(SugarCube 2/Twine 2 Browser version)

Is there a way, when using the SugarCube Pluck function to have a safeguard for a scenario where the list of array items runs out before it is supposed to?

In other words if I am plucking from an array and I have 3 items in there, but then someone clicks a fourth time.... what can I do to gracefully exit?

Can I use an || (or operator) and maybe supply it with an either: list ? Or perhaps I can have a variable check or something?

Thank you in advance.
Sage

Comments

  • Could you simply guard against that fourth click? You obviously know what the length of the array is, so use it within the click if you can. You didn't provide details, so I can't say more than that, but I'd suggest doing that if at all possible.

    Anyway, on the off chance that you cannot do so with whatever you're doing, then you may be able to use the logical OR operator to provide a default value, otherwise you'll have to check the returned value.

    For the following examples, assume this setup:
    /* Set $pies to an empty array for the example. */
    <<set $pies to []>>
    


    Use the logical OR operator to provide a default value
    You may use either the TwineScript or JavaScript operator:
    /* Try to pluck an item from the empty array.  Provide a default via the `or` operator. */
    <<set $pie to $pies.pluck() or "Yo, Dawg.  I heard you like defaults.">>
    
    /* Try to pluck an item from the empty array.  Provide a default via the `||` operator. */
    <<set $pie to $pies.pluck() || "Yo, Dawg.  I heard you like defaults.">>
    
    The logical OR operator isn't always usable, however, since any value which coerces to falsy will return the default, even if it's a valid value from the array (e.g. 0).


    Checking the returned value
    <Array>.pluck() returns undefined if the array is empty. So, unless you're stuffing your array with the undefined value, the only way to get one is to exhaust the array.

    Using the <<if>> macro's def/ndef conditional operators:
    /* Try to pluck an item from the empty array. */
    <<set $pie to $pies.pluck()>>
    
    /* The def operator. */
    <<if def $pie>>
    	/* We have $pie! */
    <<else>>
    	/* $pie is undefined.  Boo! */
    <</if>>
    
    /* The ndef operator. */
    <<if ndef $pie>>
    	/* $pie is undefined.  Boo! */
    <<else>>
    	/* We have $pie! */
    <</if>>
    

    Using the JavaScript typeof operator:
    /* Try to pluck an item from the empty array. */
    <<set $pie to $pies.pluck()>>
    
    /* Equivalent to the def operator. */
    <<if typeof $pie isnot "undefined">>
    	/* We have $pie! */
    <<else>>
    	/* $pie is undefined.  Boo! */
    <</if>>
    
    /* Equivalent to the ndef operator. */
    <<if typeof $pie is "undefined">>
    	/* $pie is undefined.  Boo! */
    <<else>>
    	/* We have $pie! */
    <</if>>
    
  • Wow! This is... this is... well... this is a lot!
    I haven't even tried it yet... I am about to... however... but seriously... THANK YOU
  • So... in case it helps anyone else, I actually was able to use both an OR and also an EITHER within the same setup. I think it's super helpful, really, especially when combined with a widget. Here is the code I used:

    <<set $Usage1List to ["Here is a sentence.", "This happens to be another one.", "Here is the final one."]>>\
    
    <<widget "Sample">>\
    	<<set $Sample to {
    		Usage: $Usage1List.pluck() or either("NewOption1", "NewOption2", "NewOption3"),
    	}>>\
    <</widget>>
    

    Thank you again, @TheMadExile
Sign In or Register to comment.