Working String Manipulation (no *script used) (substring, substr, indexOf, contains, replace)

Twiger Functions is back with version 8! (find it here)
(That file/text is a scene, so just drop it in your scenes folder and you’re good)

Besides using arrays (see here) and adding checkpoint-like saving to your game (see here),
You can now make use of javascript-like string functions:

substring
This method extracts the characters in a string between two indexes.

*gosub_scene twiger_functions substring [string] [start] [end] [output variable]

The output variable should be a string of the name of your global variable, ie “name”
Example:

*create name “Mr. Markiplier”
*create nickname “”
*gosub_scene twiger_functions substring name 5 9 “nickname”
${nickname}

This would result in Marki showing in your game.

substr
A lot like substring, but instead of saying where to end, the third parameter says how many letters to include.

*gosub_scene twiger_functions substr [string] [start] [length] [output variable]

Example:

*create name “Mr. Markiplier”
*create nickname “”
*gosub_scene twiger_functions substr name 8 5 “nickname”
${nickname}

This would result in kipli showing in your game.

indexOf
This method tells you the character index of a string within another string, or -1 if it does not exist.

*gosub_scene twiger_functions indexOf [string] [string to find] [case sensitive?] [output variable]

Example:

*create name “Mr. Markiplier”
*create nickname “mark”
*gosub_scene twiger_functions indexOf name nickname true “name”
${name}

Because case sensitive is set as true, this would result in -1 showing in your game. If it had been set to false, 5 would have shown in the game because “mark” starts at the 5th character.

contains
Tells you whether or not (true or false) a string appears within another one.

*gosub_scene twiger_functions contains [string] [string to find] [case sensitive?] [output variable]

Example:

*create findme "plier"
*temp find false
*gosub_scene twiger_functions contains "Mr. Markiplier" findme false "find"
*if (find)
  You found it!

This would result in You found it! showing in your game.

replaceFirst
Replaces the first instance of string B inside string A with string C.

*gosub_scene twiger_functions replaceFirst [string A] [string B] [string C] [case sensitive?] [output variable]

Example:

*create name “Mr. Markiplier”
*create nickname “”
*gosub_scene twiger_functions replaceFirst name "Mr. " “” false “nickname”
{nickname}| *gosub_scene twiger_functions replaceFirst nickname "plier" "moo" false "nickname" {nickname}

This would result in Markiplier|Markimoo showing in your game.

replaceAll
Replaces all instance of string B inside string A with string C.

*gosub_scene twiger_functions replaceAll [string A] [string B] [string C] [case sensitive?] [output variable]

Example:

*create sentence “Mr. Markiplier, please hand me the pliers.”
*create sentence1 “”
*gosub_scene twiger_functions replaceAll sentence “plier” “moo” false “sentence1”
*gosub_scene twiger_functions replaceFirst sentence “plier” “moo” false “sentence”
{sentence1}|{sentence}

This would result in Mr. Markimoo, please hand me the moos.|Mr. Markimoo, please hand me the pliers. showing in your game.



Important note: In order to use *gosub with parameters, the scene has to be included in the scene_list in order to pass quicktest or randomtest (I don’t remember which)

7 Likes

Hey, welcome back. And I see you already made it with additional features to the twiger_functions.

And here I am, still stuck with the same project I’m with since 4yrs ago.

This is amazing, and I will immediately add this to the Algorithm hall of fame - code examples: sorting, shuffling

I am going through your old posts because there is a lot of interesting stuff. I hope you don’t mind questions to very old topics.

Not at all. Feel free to even suggest algorithms I could work on. I’m thinking maybe something that counts how many achievements are completed, but the author might have to list the ‘codename’ of every achievement they add.

1 Like

The naming of variables (s_tring etc) is actually very clever, nice way to avoid conflicts.

Question: What is *takes in line 269? Is it from a different version of CS?

*takes in save_num

Also, I don’t understand where saves… are saved. Where is the value actually stored?

huh… it’s been years since I wrote line 269, and I don’t dare change it if that might break something (though it looks impossible to reach), but I think it was meant to be in a comment.

I believe one of these is a list (^Tom^apple^red) of the values and the other is a list of the variables (^name^fruit^color) (again: it’s been years since I’ve touched ChoiceScript until yesterday)

*create save_1 “”
*create save_1b “”

  1. Yes, I guess the *takes is unreachable, right before the label.

  2. Oh, I see. You can save the status within the game, and reload a checkpoint, right? It’s not persisted across games.

In term of algorithm ideas, I am implementing a general queue and a stack for a game. I think I will use fake CS arrays ( entry_1 entry_2 ) with an upper max (that’s what I have done in all my games so far) to maintain some performance. But I would be curious to see a CS implementation of an unlimited stack or queue.

1 Like

yes although like a lot of things in my twigers_functions, it can easily be made to do other things. I believe authors can print the 2 saved arrays in the game like anything else and then in another game use a text box to import those arrays and load them in. EZ PZ

Also, you should be able to get a random item from t_f arrays EZ (probably doesn’t need explained but you get the array length, random number to that, then get by that index)

1 Like

I use a very similar idea for the level editor here: New puzzle: Mind the gap - with level editor and the race replay here: RacetraCS: Car Racing Game - #7 by choicehacker

… but I was curious to learn how the persisted saves are done. I believe CS must use some Javascript (cookies?) manipulation - I should look into the CS source.