This Twine macro lets you use the Wordnik API to get random nouns and adjectives for your story.
- IMPORTANT FIRST STEP: you must have a Wordnik API key to use this! If you don't already have one, go here to register for an API key. If you do already have one, move on to the next step!
- Paste the contents of the WordnikRandomWords.js file below into a new passage. Call the passage whatever you want, and add the tag "script" to it.
- Modify the first line of the passage you just created so that instead of "var API_KEY = 'xxxxxxx'" you replace the x's with your actual API key you got from Wordnik.
There are two parts to using this. First you need to put placeholder HTML in your text. This looks like the following:
Hello reader! I am going to tell you a random noun: <html><span class="myRandomNoun">...</span></html>. Wasn't that neat?
What you're doing is telling the macro where to put the random word. The class name ("myRandomNoun" in this case) doesn't matter -- it could be 'foo' or 'AuntBertha' or '1234' or whatever, as long as you keep it consistent in the next part.
Then, at the end of your passage, put the following:
<<randomWord myRandomNoun noun 1000>>
This is a command that says "Hey randomWord macro! Find "myRandomNoun" and replace it with a noun that has a corpus count of at least 1000." (See below for what 'corpus count' means.)
So the macro takes the form of:
<<randomWord [class] [part of speech] [minCorpusCount] [article]>>
- class: can be any alphanumeric phrase. Is just needs to match the placeholder text in your passage.
- part of speech: For now, only 'noun' and 'adj' are supported. Defaults to 'noun'.
- minCorpusCount: A number from 1 to... a lot. Wordnik is always scraping the web: a 'corpus count of at least 1000' means that the word appears at least 1000 times in their records. The bigger the corpus count, the more common the word. Play around with this until you get a range of words that is to your liking. Defaults to 1000.
- article: If this value is 'article' then it include "a" or "an" at the front of the word, as appropriate. Slightly buggy since I just check for vowels ("an university" will show up, for example).
To see a bunch of these features in action, you can look at the Twine code snippet below, which is what powers the demo.
IMPORTANT NOTE: Due to a bug in Twine 1.3.5, macros do not work on the Start passage. You should put your start passage in a new passage called "ActualStart" and then put the following code in the Start passage:
<<display ActualStart>>
This will warp you directly to ActualStart when your game starts up.