+2 votes
asked by (180 points)

I'm new to Twine, but I've been using Perchance (random generator creator, uses a similar format) for a while now. In Perchance, typing

{a} [variable]

outputs either "a" or "an" depending on the first letter of the variable, so it would print "a dwarf" or "an elf". I'm trying to do this in Twine but can't figure it out. Is there a simple way to do this? I'm using Sugarcube 2.21.0.

2 Answers

+1 vote
answered by (8.5k points)
selected by
 
Best answer

There's a simple Articles library available, among other things, which lets you easily do this (for English). You could try using it as a base, or an idea to adapt to your needs, or just straight-up use the library itself - but then you need to put it in a wrapper for it to work, since it's written for npm/NodeJS only.

Alternatively, I created a minified version for SugarCube (works as a module with requireJS too if you use it), just copy & paste it in your JavaScript, then use ...

<<= setup.Articles.articlize($variable)>>

If you want to know just the article, you can also use ...

<<set _article = setup.Articles.find($variable)>>

... which will set _article to "a" or "an" depending on the contents of $variable.

commented by (180 points)
Thank you! That's exactly what I was looking for.
0 votes
answered by (970 points)

Maybe try an if, else statements? I assume you have a set number of races. So you could have something like:

 

<<if>> $race = elf then set $nounid = “an elf”>>

<<elseif>>  $race = dwarf then set $nounid = “a dwarf”>>

<</if>>

Then put $nounid any where you want that set of words to show up.

Honestly I am a newbie... I am not a 100% sure that format will work. I am just trying to convey the idea or concept.

commented by (16.4k points)
edited by

That's almost valid code.  I think what you meant was this:

<<if $race == "elf">>
	<<set $nounid = "an elf">>
<<elseif $race == "dwarf">>
	<<set $nounid = "a dwarf">>
<</if>>

though a shorter version could simply cover the exceptions:

<<switch $race>>
	<<case "elf" "elder dragon" "ogre">>
		<<set $nounid = "an " + $race>>
	<<case "gelf">>
		<<set $nounid = "a gelfling">>
	<<default>>
		<<set $nounid = "a " + $race>>
<</switch>>

You don't need to include "dwarf" or "human" or the like explicitly, since they will all get covered in the default case.  See the <<switch>> macro for details.

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.
...