+1 vote
by (250 points)

Hi all, 

I'm trying to use the print either macro to get it to print one of two buttons but I'm not sure whether that's possible, or if there's another way to do it. Any help would be much appreciated. At the moment, I have: 

 

<<print either( 

<<button "[[What does that mean?]]">>...<</button>>,

<<button "[[You now have to decide what to do next.]]">>...<</button>>)
>>

 

1 Answer

0 votes
by (62.7k points)

While you can use the print macro to evaluate markup, and it will be parsed correctly, it's really not the recommended solution. That is, you're looking for program control, and should probably use one of the program control macros, but combine it with an either() to create the randomization that you want. That might look something like this: 

<<set _sel to either(0, 1)>>\
<<switch _sel>>\
<<case 0>>\
    <<button [[What does that mean?]]>>...<</button>>
<<case 1>>\
    <<button [[You now have to decide what to do next.]]>>...<</button>>
<</switch>>

Note that you don't have to (and shouldn't) quote your double-square bracket markup when it's passed to a macro as an argument. 

by (65.8k points)
edited by

Unless the random value itself is of value there, you don't need a variable there at all.  For example:

<<switch either(0, 1)>>
<<case 0>>\
    <<button [[What does that mean?]]>>...<</button>>\
<<case 1>>\
    <<button [[You now have to decide what to do next.]]>>...<</button>>\
<</switch>>

That said.  I'd simply use the random(…) function there.  For example:

<<switch random(1)>>
<<case 0>>\
    <<button [[What does that mean?]]>>...<</button>>\
<<case 1>>\
    <<button [[You now have to decide what to do next.]]>>...<</button>>\
<</switch>>

The either(…) function and <Array>.random() method are good when you have discrete values you want to select from at random.  When you simply need a random integer number within a certain range, then random() is likely a better choice.

Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...