0 votes
by (160 points)
(Twine 2.1.3, Harlowe 2.0.1)

Hi!

I'd like to be able to adapt certain parts of my story to the player's name - specifically, what letter it starts with.
I've read about Javascript code that can do this, but I can't seem to get it to work (perhaps because I have no knowledge of Javascript or the proper syntax for it - even if I copy/paste the "if" part, I don't know how to set up the rest of it).

It's not a feature I desperately need so it's no big deal if I never figure it out, but it would be great if i could get it to work. My plan is to create a $FirstLetter variable that can be set to "A", "B", "C", etc. depending on what the first letter of the name is. Now the hard part is extracting that info!

I've already set the player's name to start with an uppercase no matter how they type it in, so I only need to check for uppercase letters.

I'd really appreciate any help!

1 Answer

+1 vote
by (144k points)
edited by
 
Best answer

As explained in the deprecated (substring:) macro's documentation you can access each character/letter within a String/Text value by using the same 's or of syntax as you use to access the items within a collection object (Array or (Data)Map)

(set: $name to "Abcde")
name: $name
first letter: (print: $name's 1st)
(if: $name's 1st is "A")[The first letter of the name is A]

 

by (160 points)
Oh wow, that was much simpler than I expected, and I don't even need to create a new variable since I can just print it at will!
I haven't really learned how arrays work yet so I didn't know you could do stuff like that.

Thank you!!
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.
...