ink icon indicating copy to clipboard operation
ink copied to clipboard

Lists for choices

Open chrysoula opened this issue 7 years ago • 8 comments

Is there a way to generate choices based on the contents of a list? Like, 'choose an available color' and the option list is dynamically generated based on what's in the list at that time?

chrysoula avatar Oct 21 '17 23:10 chrysoula

not in the core language, but you can do a bunch of conditional choices for now

LIST colors = BLUE, RED, YELLOW, WHATEVS

== ChooseColor(list) ==
What color do you want?
* { list ? BLUE } [BLUE] -> blue
* { list ? RED } [RED] -> red
* { list ? YELLOW } [yellow] -> yellow
* { list ? WHATEVS } [whatevs] -> whatevs
+ -> nocolor

clembu avatar Oct 22 '17 00:10 clembu

All right. Doesn't work for a list of indefinite length but that's a convenience. Now, the next terrible way I want to abuse lists is: can I select a random element from one?

chrysoula avatar Oct 22 '17 01:10 chrysoula

Am proud. Tracked down random, cobbled together ~ current_protagonist = characters(RANDOM(1,(LIST_COUNT(characters))))

chrysoula avatar Oct 22 '17 05:10 chrysoula

That's a good random solution - though be warned it'll fail if you use the "give values to list elements" feature.

LIST characters = Maria = 100, Bob = 150

We're intending to add a LIST_RANDOM at some point.

On choices - assuming the result of the choice is a fixed thing, like setting a variable, you can do it using a recursion:

// copy the list you're using to make options ~ temp toprint = InitialList

  • (top) // take an element off the list to print ~ temp element = LIST_MIN(toprint) ~ toprint -= element // thread in the choice for this element <- option(element)

// if there's any left to print, loop to print the next one { toprint: -> top } -> DONE

= option(element)

  • [{element}] -> choose(element)

= choose(element) You picked {element}! -> END

If you need the list values with numbers feature note that you can make a working random function using the same kind of recursion trick. On Sun, 22 Oct 2017 at 6:20 am, chrysoula [email protected] wrote:

Am proud. Tracked down random, cobbled together ~ current_protagonist = characters(RANDOM(1,(LIST_COUNT(characters))))

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/inkle/ink/issues/377#issuecomment-338452447, or mute the thread https://github.com/notifications/unsubscribe-auth/AA40o-ZbtUD05_b-DsIy2ae-26bYMnEWks5sutCAgaJpZM4QBwK6 .

joningold avatar Oct 22 '17 07:10 joningold

Oooh, yes. I'd been trying to wrap my brain around how to pop an element off the list and I KNEW it had to be possible but I just couldn't assemble it. Forward!

chrysoula avatar Oct 22 '17 17:10 chrysoula

@joningold I know this is an old thread, but I think what you wrote above for looping over a list to print choices is exactly what I need. I tried copy/pasting it into Inky and providing a sample set of data, but haven't been able to get it to print any choices. Can you please point out what I'm missing to make this work?

LIST InitialList = one, two, three

test
// copy the list you're using to make options
~ temp toprint = InitialList
- (top)
// take an element off the list to print
~ temp element = LIST_MIN(toprint)
~ toprint -= element
// thread in the choice for this element
<- option(element)

// if there's any left to print, loop to print the next one
{ toprint: -> top }
-> DONE

= option(element)
+ [{element}] -> choose(element)

= choose(element)
You picked {element}!
-> END

2020-06-25 09_51_02-Untitled ink

Skeletoneyes avatar Jun 25 '20 16:06 Skeletoneyes

The list is empty. Try

LIST blah = (one), (Two) , (three)

The brackets “Set” the value within the list

Jazz, in Three-Four Time: A Novel http://www.amazon.co.uk/dp/B004K1EYIE

joningold avatar Jun 25 '20 19:06 joningold

That's what I was missing, thanks!

Skeletoneyes avatar Jun 25 '20 19:06 Skeletoneyes