Checking the last few letters of an input

Hey, quick question:

How do I check again if the last couple of letters in a player input fit into a certain pattern?

As in if the player enters a word ending in -tite that a certain flavortext/variable is triggered?

Is that eve possible or am I mixing up functions?

I’ve not used it myself but from the advanced ChoiceScript page, there’s this:

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}.
1 Like

That’s the base I’m working off. I vividly remember someone once posting a template for checking the last few letters of an input, but I cannot locate that again ;_;

1 Like

Use the length function to find the index for the last letter, and work backwards from there.

I fumbled about a bit to find the wiki article I needed, but here it is: Extracting letters/numerals from variables.

4 Likes

CSLib has a subroutine called substring that allows extracting parts of strings. It works like this:

*gosub_scene cslib_string substring <string> <starting point (number)> <end point (number)>

For example:

*gosub_scene cslib_string substring "Hello, World!" 1 5

Thw above would yield a string starting with the first character and ending with the fifth, Hello.

In your use case, it would look something like this:

*gosub_scene cslib_string substring my_variable (length(my_variable) - 2) lenght(my_variable) 

The above would yield the last 3 characters of the string. Note that I subtracted 2 instead of 3, because the starting and ending points are inclusive.


4 Likes

Hmmm.

*create return false
*create nword ""

*comment Store word in nword
*input_text nword 

*comment Check if nword ends in "tite":

*gosub check_end "tite" nword

*if return
  *print "Yes, it ends with the suffix!"
*else
  *print "No, it doesn't end with the suffix."
*finish

*label check_end
*params suffix word

*comment Get lengths
*temp word_len length(word)
*temp suffix_len length(suffix)

*comment If the word is too short, return false
*if (word_len < suffix_len)
  *set return false
  *return

*temp i 0
*temp j word_len
*temp end_of_word ""

*comment Extract the last suffix_len characters
*label for_loop
*set end_of_word ((word#(j - i)) & end_of_word)
*set i + 1

*if (i = suffix_len)
  *if (end_of_word = suffix)
    *set return true
    *return
  *else
    *set return false
    *return
*goto for_loop


See what I did?

I created a function that receives whateve sufix you need it to check and the word to be checked

And it returns with true or false…

Here’s a step-by-step explanation of
How check_end function operates:

  1. Check if the word is too short to possibly match the suffix.

  2. Extracts the last chars of the ā€œwordā€ variable one by one, storing them in end_of_word in the correct order.

  3. Compares the extracted portion to suffix and returns true or false accordingly.

So, if the player write things like ā€œpetiteā€ or ā€œgraniteā€ it will return true

And will return false for whatever other nonsense they write.

By the way. Holly momma this is the base of a word parser btw! ::oh::

1 Like

Major thanks <3. Though I am not certain how applicable it is in my case as it’s not just one suffix but several at once spread over several categories. The categories would be a simple if/elseif/else, but the different suffixes i think would be a problem like this?

I’d think you just need to run the subroutine for every suffix you want to check? Unless you mean there would be multiple suffixes in the same word in which case, yeah, it might need some tinkering.

2 Likes

Several sufixes? No problem!

You can do it manually:

*gosub check_end "ite" (nword)
*gosub check_end "as" (nword)
*gosub check_end "ment" (nword)
*gosub check_end "arium" (nword)

Or, if you preffer, automate with a loop:

*temp numb_of_sufixes 4
*temp_array sufixes 4 "ite" "as" "ment" "arium"
*temp i 1

*loop_check_sufixes
*gosub check_end (sufixes[i]) (nword)
*set i +1
*if i > numb_of_sufixes
  *finish
*else
  *goto loop_check_sufixes

Edit: Ninja’d by LiliArch.

1 Like

thank, I’ll play around with it <3

1 Like

Please do so, my friend.

If you find any issues post it so we can try figuring out a way to solve it.

I played around with the example code, and while that works great (major kudos again <3) , I am not too certain if it is applicable to my game in the end. Because there’s a total of 26 suffixes, spread over five possibilities, and I am not trusting myself with that much alteration to the code (as of now)

1 Like

You’re welcome. 26 sufixes over 5 possibilities? I would be lying if I told you I am not curious. That’s a lot of conditionals!

it’s for my dragon game ā€˜ashenmaw’. the suffixes are the nomenclatures of the various flights :3

1 Like

At some point, you have to consider letting the player name their dragon Pookie, if they really want to. 26 suffixes is a lot!

They can? Nothing would be stopping them from it?
The suffixes are just a suggestion and the check would be for flavortext.
What made you think I’d be restricting the name input (aside from npc names)?

Probably because of the string manipulation code. Suggesting lore-compatible names is great. I love it when games do that. I guess I just didn’t really understand why you would need to code that, instead of hard-coding your example names. Something about your example went over my head, I suppose.

1 Like

Did you take a look at cslib_string? Was it too hard to use? Feedback is really welcome.

1 Like

Ah, I see. Sorry if I came across as rude, I was a little baffled.