Now you can extract letters/numerals in ChoiceScript

In the latest version of ChoiceScript up on github, you can extract the characters (letters/numerals) out of a variable, like this:

*temp word "xyzzy"
*temp first_letter word#1
*temp second_letter word#2
The first letter of the word "${word}" is ${first_letter} and the second letter of the word is ${second_letter}.

(Programmers will note that the character index is 1-based, not 0-based.)

You can count the number of characters in a word like this:

*temp word "plough"
*temp word_length length(word)
*temp last_letter word#word_length
The word ${word} is ${word_length}, and so its last letter is ${last_letter}.

You could also write: ${word#length(word)} to show the last letter.

Here’s a sample piece of code demonstrating how to capitalize the first letter of every word in a variable:

*temp title
*input_text title
*temp i 0
*temp prevWhiteSpace true
*temp recapitalized ""
*label while
*set i +1
*if i <= length(title)
  *if (title#i) = " "
    *set prevWhiteSpace true
    *set recapitalized &(title#i)
    *goto while
  *if prevWhiteSpace
    *set recapitalized &"$!{title#i}"
    *set prevWhiteSpace false
    *goto while
    *set recapitalized &(title#i)
    *goto while
*set title recapitalized
The title is: "${title}"



Hmmm, I’m going to have to come up with an excuse to use this.

It is one of those amazing features that I have no use for but still want to use anyway :smile:


Things like this are also why I wish the wiki was being updated my regularly, because in a couple of months I’ll have forgotten this even exists.

1 Like

@Fantom Keeping wikis up to date is an awful lot of hard work. Feel free to volunteer to update the wiki yourself. :slight_smile: The front page has a list of admins, I’m sure you could contact one of them and ask them for wiki edit permissions.

Also if you make the page yourself chances are you’re less likely to forget about it.


I was literally just thinking about that :slight_smile:

1 Like

I’m really struggling to see a practical use for this. I guess that’s up to far smarter and creative minds than myself.

Oh, hell yes. This is awesome. Thanks for adding it! <3

First thing that comes to mind: the beautifully creepy maneuver from Sid Meier’s Alpha Centauri where the protagonist (example, Deirdre) is referred to in lower case (earthdeirdre) when addressed telepathically. This would allow the author to do something similar with custom names entered by the player.


Hollywood Visionary is using it to detect when a word starts with a vowel, and thus needs the article “an” as opposed to “a.”

It also uses the technique to capitalize the name of your film studio, e.g. “Blue Hat Entertainment.”


That’s fantastic. :slight_smile:

Hmm what’s the code for that then? (Hmm suppose I should just stop being lazy and actually look it up myself) Okay I looked and couldn’t find it. :frowning:

1 Like

I’m wondering if you could do something similar for writing characters in the singular they! Right now I’ve been just including an if statement to check for which block of text to jump to, but this could be handy for determining subject verb agreement. :blush:

1 Like

Note that HV is using a slightly older version that was 0-based. The latest version does this in a subroutine:

*label setWorkArticle
*temp fl "$!{workval#1}"
*if (((((fl = "A") or (fl = "E")) or (fl = "I")) or (fl = "O")) or (fl = "U"))
  *set workArt "an"
*set workArt "a"

That code is pretty weird/fancy, so it’s worth some explanation.

  1. workval#1 returns the first letter of the global variable workval.
  2. We don’t know whether it’s upper case or lower case; we standardize on uppercase by wrapping the whole thing in $!{}
  3. You can’t use $!{} directly in a *temp or *set statement, but you can use $!{} in quotes, and you can use quotes in a a *temp or *set statement.

Thus the line: *temp fl "$!{workval#1}"

OK, so now we have an uppercase letter that may or may not be a vowel. We check to see if it’s A, E, I, O, or U, in which case we’d *set workArt "an" and *return; otherwise we’d *setWorkArt "a" and *return that.

So now workArt will be “an” if workval starts with a vowel, or workArt will be “a” otherwise.

Tada! :tada:


This is great - I really dig that you continue to implement additional functionality into CS! :grinning:

1 Like

Great stuff! I’ve been wanting this but I’ve forgotten to ask.


I have a request though, if possible. Namely to use text variables for image links, so you can write something like image ${which_image} left, instead of having to write;

*if (image1)
  image image1.jpg left

That’s just off the top of my head, but if there’s more functionality for images maybe more would use them. Like for instance if one could separate the window into two areas, so maybe the upper part would stay still and the bottom with the next button would be handily frozen in place while the text and the choices scroll inside of a smaller box, like in a visual novel.

I am loving these new programmer features. Just off the top of my head, I’m certain we could program our own text/password save/load features using this functionality.

1 Like

*image ${imgname} was just implemented.


Oh, great! I must have missed that post. It seems there’s a limit on the notifications on unread threads or something.

I’m going to need to find a way to use this, too. I can already imagine something like an NPC saying the MC’s name and, if over a certain letter count, making a comment like ‘that’s a mouthful’. Or if it’s under a certain count, ‘nice and simple, I like that.’ - I’ll have to consider it when I have time for it and get to such a point.

That was exactly what I was thinking. Setting up passwords that would make it easier to handle betas.

That is quite clever, but its really interesting, I wonder how much more we could play with names.