Howdy, Stranger!

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

Displaying part of a random passage on click

Twine 1.4 and SugarCube 2.

Let's say I have a simple passage like this:
Some text and <<click "button">><</click>>.

When the button is clicked I want to: get a random passage from a list, then get a random sentence from that passage, and display it below the original text and button--with any links removed/deactivated (so only showing plain text sentence). So the player can keep clicking the button, and new random sentences will keep appearing below it.

I know I can display a random passage with:
<<set $passage to either("Passage 1", "Passage 2", "Passage 3")>>
<<display $passage>>

And I thought I could display a random passage on button click with something like this:
<<click "Show Random">><<set $passage to either("Passage 1", "Passage 2", "Passage 3")>><<display $passage>><</click>>

But that's not working.

As for grabbing a random sentence from the passage (rather than displaying the whole passage), and deactivating the links... I assume this is something I should try to do in prerender, via some kind of black magic vaguely like this?

Comments

  • Do these random sentences need to be from a normal passage or could they be from an array of sentences?
  • Do these random sentences need to be from a normal passage or could they be from an array of sentences?

    That's an excellent question. Ideally, I would like to grab text from normal passages (i.e. actual game passages that the player has been traveling through)... but now that you mention it, I could probably get most of the way to the effect I want by manually creating an array of twenty or thirty sentences taken from various passages... (which I assume would enable a much easier solution, which would probably be worth the trade).
  • In that case….


    If reusing a sentence isn't an issue, then you could simply do something like the following.

    Goes in a widget-tagged passage:
    <<widget "randomSentence">>\
    <<print [
    	"sentence 1",
    	"sentence 2",
    	…
    	"sentence N"
    ].random()>>\
    <</widget>>
    
    Usage:
    Blah, blah, blah.
    
    <<randomSentence>>
    
    Yakity, schmackity.
    
    To add <<click>> to the mix, you'd want something like the following:
    Some text and <<click "show random">>
    <<replace "#random-sentence">><<randomSentence>><</replace>>
    <</click>>.
    <span id="random-sentence"><span>
    


    If you don't want to possibly reuse a sentence, then the following should probably suffice.

    Goes in the StoryInit special passage:
    <<set $sentences to [
    	"sentence 1",
    	"sentence 2",
    	…
    	"sentence N"
    ]>>
    
    Usage:
    Blah, blah, blah.
    
    <<print $sentences.pluck()>>
    
    Yakity, schmackity.
    
    To add <<click>> to the mix, you'd want something like the following:
    Some text and <<click "show random">>
    <<replace "#random-sentence">><<print $sentences.pluck()>><</replace>>
    <</click>>.
    <span id="random-sentence"><span>
    
  • The "black magic" is needed to modify the whole passage. If you just want to grab a sentence from it, you don't need prerender function. yourtext=tale.get(passagename).text will give you the whole text of the passage with passagename at any moment (at least in Sigarcane, didn't test it with Sugarcube). But to get a single line from this text you will need to write your own parser.
    On the other hand, if you prefer to use an array of sentences, you can use them in the corresponding passages to keep synchronization, i.e.
    ::passage N
    ...
    <<$sentences[N]>>
    ...
    instead of actual sentence. So. this will do what you want, only from the other end ;)

    Just curious, why do you need such a tricky thing as a random sentence from a random passage in you story?
  • @TheMadExile, @yun: Thanks very much for the extremely helpful answers!
Sign In or Register to comment.