Howdy, Stranger!

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

Eliminating options from an either function after selection

I can't find any specific mention of this with a search, but if it has been covered, I'd appreciate a link.

Here's my example and question.

<<set $name = either("Bob","Jane","Larry","Mary")>>

I want to create a set of variables ($metBob $metJane $metLarry $metMary) such that when the corresponding variable is set to "true" that option is eliminated from being a possible selection in the either function.

This could represent the names of people you could meet, and the goal is to make it random, but mark each name off the list as it is used. I'm open to other ways to achieve this same effect. The number of options is going to be relatively short, probably less than ten.

Anyone got a suggestion for how to do this?

Thanks!

Comments

  • You could use an array for your name list and remove each item from it as it's picked.  For example:

    First, add this function to a script tagged passage:

    /*
    * Returns a randomly selected item from the given array, removing said item from the array so that it cannot be selected again.
    * n.b. The `items` parameter must be an array. Also, the removal code assumes that each item will be unique.
    */
    function pick(items) {
    if (arguments.length === 0 || !Array.isArray(items)) {
    throw new TypeError("pick items parameter must be an array");
    }
    if (items.length === 0) {
    return;
    }

    var picked = either(items),
    index = items.indexOf(picked);
    if (index !== -1) {
    items.splice(index, 1);
    }
    return picked;
    }
    Second, make the list an array (preferably from the StoryInit special passage):

    <<set $nameList = ["Bob","Jane","Larry","Mary"]>>
    Basic usage:

    <<set $name = pick($nameList)>>
    Usage:

    <<if $nameList.length is 0>>
    /% All names have been picked! %/
    <<else>>
    <<set $name = pick($nameList)>>
    /% Do something with the newly picked $name. %/
    <<endif>>

    That doesn't create any $met variables for you, which may or may not be an issue.  If your only reason for wanting them is to narrow down the list, then you don't really need them, since the array is reduced on each pick.  On the other hand, if you have additional uses for them, then you could simply set the appropriate $met variable after setting $name.
  • Thanks very much for replying. I'll give that a try tomorrow!

    -B
Sign In or Register to comment.