Howdy, Stranger!

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

Pulling random words from lists?

Hello! I'm new-ish to twine (and have no coding experience to speak of), and using Sugarcube 2 on 1.4.2. I've tried searching the forums and via google, but haven't found anything that quite fits my situation.

What I'm currently doing is allowing player customization of their character, for things like gender, eye color, hair color &etc. Their choices are stored in variables ($gender, $eyeColor, $hairColor &etc....), and in a character description page and elsewhere, these choices are displayed and shown. To keep all descriptions from being really static and same-y every time, I'm having a random word associated with that variable display.

So, the line for gender reads like:
You are <<if $gender is 1>><<print either("male", "a guy", "a dude", "a man")>>.<<elseif $gender is 2>><<print either("female", "a lady", "a girl", "a woman")>>.<<endif>>
...and each time the player views that page, there's a chance of seeing a slightly different description. Same with eye color, hair &etc.

Other uses I'm planning are things like running into an angry animal. But maybe sometimes the animal is ferocious, or ill-tempered, or feral. So the player would see something a little different each time.

The problem I've realized I'll run into, is that every single time I want to have a random description, I'll have to copy-paste the entire string of choices into the passage for each possible variable. If it were just gender, that would be no problem. But I'm planning a large amount of customization, so that's impractical due to the space it would take. And eve more so because, if I decide to add or remove a possible random word, I'll need to change it in every single location that it may appear in the story.

So I'm wondering, is there a way to simplify this down to something like,
You are <<if $gender is 1>><<print RANDOM WORD FROM $gender is 1 LIST>>.<<elseif $gender is 2>><<print RANDOM WORD FROM $gender is 2 LIST>>.<<endif>>

And then just have the list of words stored off in their own passage that I can add and subtract from freely, without worrying about combing through the entire thing every time I want to make a change?


  • Your issues can be broken into two parts.

    1. How to store a list of items: The answer to this is to use an array.

    The following code should be placed within your story's StoryInit special passage, it defines two array variables (lists): $maleTerms and $femaleTerms
    <<set $maleTerms to ["male", "a guy", "a dude", "a man"]>>
    <<set $femaleTerms to ["female", "a lady", "a girl", "a woman"]>>
    ... and you can use those two arrays like so:
    You are <<if $gender is 1>><<print either($maleTerms)>>\
    <<elseif $gender is 2>><<print either($femaleTerms)>>\

    2. How to display the same output multiple times without using cut-n-paste, and there are two basic answers to this:

    2a. Store the content within a passage and <<display>> it where ever it is needed.
    Create a new passage named Gender and place the following in it:
    <<if $gender is 1>><<print either($maleTerms)>>\
    <<elseif $gender is 2>><<print either($femaleTerms)>>\
    ... and you use the Gender passage like so:
    You are <<display "Gender">>.

    2b. Create a widget that outputs the repeating content.
    Create a new passage and assign it the special widget tag, the name of the passage is not important but I generally name mine Widgets. Place the following into the passage:
    <<widget "gender">>\
    <<if $gender is 1>><<print either($maleTerms)>>\
    <<elseif $gender is 2>><<print either($femaleTerms)>>\
    ... and you can use the gender widget like so:
    You are <<gender>>.

    I hope the above helps
  • Thank you so much! I think this is exactly what I'll need. :)
Sign In or Register to comment.