Questions about listboxes

Twine Version: 2.3.5 (Sugarcube 2.31)

Hey folks!

I have 2 questions about listboxes.

1. Can the listbox display box shrink to fit the length of the text currently displaying? I’ve noticed the box borders are meant to fit the longest string in the list, but this has the unfortunate downside of not really working in sentence structure (because a short option will generate a huge amount of whitespace after it).

2. My variables within the listbox options are not showing up as their strings (ie they still have the $ signs on them). Is there any way to fix this?
It seems to be fine on the cycle macro, but not on listboxes. For example, the following code which cycles through an array which contains variables works just fine:

<<cycle "$xmotivation" autoselect>>
	<<optionsfrom $motivation>>

But the listbox equivalent doesn’t show the variables:

<<listbox "$xmotivation" autoselect>>
	<<optionsfrom $motivation>>

Hopefully those questions make sense.

Listboxes simply don’t work that way. It would be really complicated to make them resize based on whether they were expanded or not, and then resize them again based on the current item selected.

If you need that, then I’d suggest using some other structure you’d have to create yourself.

That’s because, unlike the <<cycle>> macro, the values shown in a listbox are not parsed before being displayed.

However, if you want to show their values, then, assuming all members of the $motivation array are strings containing variable names, just do:

<<set _motivation = $ (val) { return State.getVar(val); })>>\
<<listbox "$xmotivation" autoselect>>
	<<optionsfrom _motivation>>

That will create an array with the values of those strings, and then display those values in the listbox. See the .map() method and the State.getVar() method for details.

Note that changing the values of any variables after the listbox is filled will not affect the content of the listbox.

Hope that helps! :slight_smile:

1 Like

Thanks heaps!

No worries. I can live with that.

Thanks for that awesome code. I played around with it, and it works, but I am realising now that I miscommunicated what I’m actually trying to do. The code works great if every member of the array is a single variable, but I’m trying to use variables (that happen to be strings) within longer string members of an array.

Assuming my array contains these variables:
["$he is scared at school", “$he feels lonely”, $he"]
and $he is “she”, the code returns “undefined, undefined, she”.

So definitely one step closer, but not quite what I’m trying to achieve.

OK, if it’s an array of strings which may contain variable names or other SugarCube code, then you could get that like this:

<<set _motivation = $ (val) { return $(document.createElement("div")).wiki(val)[0].innerHTML; })>>\

See my “Invisible Access to SugarCube Output” sample code for an explanation of how that works.

Have fun! :slight_smile:

1 Like

Absolutely perfect. Worked flawlessly. Thanks so much!!!