Need more string manipulation

First,
I do believe this language for text-based games need more string manipulation features, such as:

  • get length of a string
  • replace letter(s) in a string
  • take portion of string into another variable

as for now, we can only concatenate from two variables, no other manipulation can be done.

let’s say I have variables: first_name “johnny”, last_name “sins”,
with features above, concatenation will not always yield “johnnysins”, or just adding capital on first letter of variables or adding space between two variables,

but we can do this:

  • get first four letter of first_name (John)
  • get first letter of last_name (s)
  • get second letter of first_name (o)
  • get fourth letter of first_name (n)

in one concatenate command, using more than two conditions to get “Johnson” as result

this will make wording more dynamic, it can change the displayed text without
changing the value of variables used for it, and less static variables has to be made.

Second,
I hope string and integer data type can communicate, so I can store data in a very long weird string.

let’s say I got a variable stats containing “John&Knight&100&75&3&5”, then I split this string into six other strings based on the character “&”, thus yield:

Name: John&
Class: Knight&
HP: 100&
SP: 75&
ATK: 3&
DEF: 5&

erase those last character from every variables and I get values I need. From there, I can use HP, SP, ATK, and DEF which are strings, move their values into integer variables, to do some math for combat and so on.

with this, I can store John’s parameters as string, use *temp for calculations without changing value of the six variables.

Well, for a language dedicated to text-based, CYOA games, I was expecting advanced string manipulation at least, but it’s okay there are room for improvements. I hope I don’t offend anyone, I was just saying that this kind of string manipulation are somewhat staple

  • To capitalise first letter:
    $!{variable}

  • Characters: 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}.
  • Counting characters: 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} letters long, and so its last letter is ${last_letter}.

Concatenation:
To concatenate 3 variables, do a single concatenation twice.

*set concatenated_string "${variable_a}"&"${variable_b}"
*set concatenated_string "${concatenated_string}"&"${variable_c}"

For a space between each variable (or replace the space with & if you wish):

*set concatenated_string "${variable_a}"&" "
*set concatenated_string "${concatenated_string}"&"${variable_b}"
*set concatenated_string "${concatenated_string}"&" "
*set concatenated_string "${concatenated_string}"&"${variable_c}"
8 Likes

I agree with the sentiment for more (intuitive/elegant) string-manipulation. I believe it could add to a more immersive experience. For example, let’s say you’re writing a character’s dialogue. This character wants to vocalize the MC’s name in a long sigh which would take the form of increasing the number of the first instance of a vowel (ex., if your MC’s name is “Sarah”, it would be come “Saaaaaarah.”)

You can do this in most other languages fairly easily. (I’m writing this in pseudocode for ease of readability, but I think this could be easily converted into, let’s say, Python.)

vowels = {a, e, i, o, u}
for i in len(name):
    if name[i] in vowels:
        display_name = name[0:i] + name[i]*7 + name[i:]

print("Sally rolled her eyes. 'Really, ${display_name}?'")

It seems pretty nice, right?

To contrast, this is not the most convenient to implement in Choice Script! I did it anyway, just to see how it would be done. (As a disclaimer, I am DEFINITELY not a choice script buff–I only really picked it up in the last few months, so I’m positive there’s an easier way to do this. I just doubt it is that much easier)

*temp name "Sarah"
*temp display_name ""

*comment we index from 1.......
*temp i 1
*temp vowel "none"

*comment now we begin our loop
*label loop
*temp char name#i

*comment check to see if our ith char is a vowel
*if (char = "a")
    *set vowel char
    *goto loop_end
*elseif char = "e"
    *set vowel char
    *goto loop_end
*elseif char = "i"
    *set vowel char
    *goto loop_end   
*elseif char = "o"
    *set vowel char
    *goto loop_end
*elseif char = "u"
    *set vowel char
    *goto loop_end
*else
    *set i +1
    *temp name_length length(name)
    *if i > name_length
        *goto loop_end
    *else
        *goto loop
 
*label loop_end

*comment display our vowel 
This is our vowel : ${char}

*comment now we have another for loop to adequately index and build the display_name.
*temp j 1
*label loop2
*if i = j
    *set display_name display_name&char
    *set display_name display_name&char
    *set display_name display_name&char
    *set display_name display_name&char
    *set display_name display_name&char
    *set display_name display_name&char
*else
    *temp jth_char name#j
    *set display_name display_name&jth_char
*set j +1
*temp name_length length(name)
*if j > name_length
        *goto loop2_end
*else
        *goto loop2

*label loop2_end

Sally rolled her eyes. "Really, ${display_name}?"

(as a note, if you try to run this code I have ICF turned on)

Of course, there are a lot more differences between the pseudocode and the choice script code than pure string manipulation (we have an array/set structure, for loops that automatically increment, etc) that make this type of operation a lot easier in the pseudocode. However, the heart of this code is a LOT more cumbersome in choicescript- albeit, not impossible! I definitely second the sentiment to have more string manipulation abilities for Choice Script. String manipulation is fun, and right now the type of string manipulation I did in ChoiceScript probably isn’t very accessible to users who aren’t familiar with code, although it is completely possible to accomplish!

Although I do think it’s a good idea to implement more types of string manipulation in ChoiceScript, I don’t know how feasible it would be to implement, or if it’s even on the developers’ plate at this time to reconstruct their language in such a way. JavaScript has string manipulation features, but I don’t fully know how ChoiceScript interacts with JavaScript under the hood. So, I don’t know if we’ll get this kind of functionality in the future, although I can always dream!

1 Like

I don’t think wiki covered those syntax, or did I read a wrong source? where should I look for complete list of syntax?

I’m still learning and got stuck around string manipulation that’s why I made this topic

The documentation of CS is admittedly fairly poor, and so is the wiki.
But I’m one of the admins and I asdfaiuhniaesknlafjk.

5 Likes

I am a poor CS scripter, so everything you do in the Wiki is very much appreciated by me. Even if I don’t tell you or the others that volunteer their help as often as I should… so Thank You!

4 Likes

sorry for bumping an old thread, but since I’m the one started it and my concern is related to logic and manipulation, I think it’s best to continue here.

here’s what I found today when trying to build an inventory system:

1.) I can extract a character from a variable with index (start with #)
from logic below, invt_array has value “00”,
on return, val1 and val2 has value “0” and “0”, successfully extracted
image

2.) the problem rise when I try to do the opposite, overwriting variable with index
image
the code above doesn’t work, I try to set invt_array index 1 and 2, which has value “0” and “0” with new characters, “1” and “2”

3.) the error says:
image

I really hope it can be done two ways, extracting and overwriting with indexes, or maybe I did it wrong way and missed something?

any replies will be appreciated, thank you

No, you can’t write back to a string index. It’s reference only AFAIK.
You’d have to cut the string, change the value and stitch it back together.

1 Like

very well, thank you :frowning:

This topic was automatically closed 24 hours after the last reply. If you want to reopen your WiP, contact the @moderators.