Capturing output of variable widgets

Twine Version: 2.8.0
SugarCube v2

I use widgets to compose links and generate output I want to put in a variable (so I can display it in another page without side effects).

It works for the alfa link invoking the alfa widget.
I get an Error: <<print>>: bad evaluation: Cannot read properties of undefined (reading '0') error when I want to see the result for the beta link invoking the action widget.

Basically I am trying to create a generic action widget which puts the result of invoking the passed widget name into the result variable.

StoryInit

<<set $choice = 'none'>>

Start

Current choice: <<print $choice>>.

<<link Alfa Result>>
<<set $choice = 'alfa'>>
<<set $result = `<<print '<<'+$choice+'>>' >>`>>
<</link>>

<<link Beta Result>>
<<action beta>>
<</link>>

Actions [widget]

<<nobr>>

<<widget action>>
<<set $choice = _args[0]>>
<<set $result = `<<print '<<'+_args[0]+'>>' >>`>>
<</widget>>

<<widget alfa>>
This is the ALFA text.
<</widget>>

<<widget beta>>
This is the BETA text.
<</widget>>

<</nobr>>

Result

$result

[[Back|Start]]

Ya… the issue is because you’re setting the value of a variable $result with a temporary one _args. And temporary variables disappear when you change passage.

You may want to have : <<print '<<'+$choice+'>>' >> directly in the result passage
OR

<<set $result = `<<print '<<'+$choice+'>>' >>`>>

instead of

<<set $result = `<<print '<<'+_args[0]+'>>' >>`>>

in the widget.

1 Like

@Lancelot
The $result Naked Variable markup reference in the Result passage is the functional equivalent of calling the following…

<<print $result>>

…and as the $result variable contains one of the following two JavaScript Template literals…

`<<print '<<alfa>>' >>`

or

`<<print '<<beta>>' >>`

…then the $result reference in the Result passage is basically the equivalent of…

<<print `<<print '<<alfa>>' >>` >>

or

<<print `<<print '<<beta>>' >>` >>

…and nesting one <<print>> macro call inside another is just silly, and redundant.

Changing the assignment of $result to just…

<<set $result = '<<' + $choice + '>>' >>

or

<<set $result = '<<' +_args[0] + '>>' >>

…should achieve the same outcome, without needing to nest <<print>> macro calls.

1 Like

Thank you for pointing this out. It is my first effort at creating a Twine game. Looks like I still have a lot to learn.