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
  *else
    *set recapitalized &(title#i)
    *goto while
*set title recapitalized
The title is: "${title}"

Enjoy!

17 Likes

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:

3 Likes

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.

2 Likes

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.

2 Likes

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.ā€

7 Likes

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"
  *return
*set workArt "a"
*return

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:

4 Likes

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.

@dfabulich

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.

3 Likes

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.