Extracting string data from a specific range

Text can also be extracted from a string if you know the specific beginning and ending positions of the data. For example, if we have a string called ~Furcadia that with the text "Let your imagination soar!", how could we pull out only the imagination word from it and nothing else, into another string?

Well, every character within a string counts as a position. So the very first character within the string (the L in this case) will be in position 1, the one following immediately after it (the e) will be in position 2, and so on.

So if we wanted to pull out imagination from the ~Furcadia string, we would go about it like this:

(5:272) set message ~Extract to be the portion of message ~Furcadia from position 10 to position 20.

Position 10 within the string contains the very first i within imagination and position 20 contains the n at the end of it. So with this DS line, we've set the ~Extract string to be imagination, but we haven't removed it from the original ~Furcadia string.

Now that we have some background on what positions within a string represent, let's take a look at an example! We'll make a set of triggers to make ambient music loop the selection of songs indefinitely, playing song after another in randomized order.


Tutorial 6: Randomized, seamless ambient music

We'll start by setting up a string to contain our music numbers, and then make a second string to contain the duration (in seconds) of each song in order for the DS to play the songs seamlessly, one after another:

(0:0) When everything is starting up,

(5:250) set message ~SongList to { 01 05 08 09 17 }.

(5:250) set message ~SongDurations to { 124 071 105 109 096 }.

Since we have songs like m17 in our list, we'll set all the numbers in ~SongList to have 0's in front so that each song is represented by exactly two characters. We'll do something similar for the ~SongDurations list, and make each precisely 3 characters long to accommodate the longer songs (124, 105, and 109) in matching the shorter duration songs (071, and 096) in character length.

Why do it this way? Well, let's take a closer look at the song selection trigger, which additionally needs to be set up so that the music starts for the very first time, too:

(0:9) When someone arrives in the Dream,

(1:194) and timer 200 is not still counting down,

* Or *

(0:50) When countdown timer 200 goes off,

(5:281) set variable %SongSelector.x to the length of message ~SongList in words separated by spaces.

(5:312) set variable %SongSelector.y to the total of rolling 1 dice with %SongSelector.x sides plus 0.

With this, the variable %SongSelector.x will be set to the amount of words (songs) that there currently are within the ~SongList variable, and that is what is then used to determine a random song to play.

Since we know how many songs there currently are within the string that contains all the songs, we can choose a song to be played, at random, from within the list:

(5:312) set variable %SongSelector.y to the total of rolling 1 dice with %SongSelector.x sides plus 0.

However, at this point you might be wondering why the elements within both the list of songs and durations list strings consist of two and three characters each, while the (5:312) line that we used only operates within single digit numbers (1 to 5)..

And this is where extracting data from within a specified range comes in handy! We can use this to extract the number of the randomly chosen song:

(5:306) multiply variable %SongSelector.y by 3.

(5:300) set variable %SongSelector.x to the value %SongSelector.y.

(5:304) take variable %SongSelector.x and subtract 1 from it.

(5:272) set message ~MusicFile to be the portion of message ~SongList from position %SongSelector.x to position %SongSelector.y.

(5:282) set variable %MusicPlayer.x to the first number in ~MusicFile.

So, first we multiply the resulting output of the dice roll by 3, which lets us grab the portions of text within the ~SongList string that contain the music file numbers that we need:

But why use 3, when the elements of the ~SongList string all consist of 2 characters each? Well, the spaces within the string also count as characters with their own position that they occupy in the string, and so they can't be discounted.

In this case, the %SongSelector.y variable will determine the ending position that corresponds to the number of the song. So, for example, if the randomly selected song was third in the list, %SongSelector.y would correspond to the very last number of the third song. This is shown here, at position 9 within the string: { 01 05 08 09 17 }.

Now we can begin to look at how the position number of the beginning of each portion is calculated:

(5:300) set variable %SongSelector.x to the value %SongSelector.y.

(5:304) take variable %SongSelector.x and subtract 1 from it.

In the case of ~SongList's music numbers, they're all only two elements long. So if we know the position of the ending number, we can just subtract 1 from it in order to get the position of the number preceding it. In this case, %SongSelector.x would point at the very first number of the third song, at position 8 within the string: { 01 05 08 09 17 }.

Now that we know what the starting and ending ranges are, we can get the number we need to be able to play the music:

(5:272) set message ~MusicFile to be the portion of message ~SongList from position %SongSelector.x to position %SongSelector.y.

(5:282) set variable %MusicPlayer.x to the first number in ~MusicFile.

A few things are happening here-

  • A new string named ~MusicFile was introduced.
  • It is set to copy a portion of string ~SongList starting from character 8 (which is variable %SongSelector.x) in the string, and ending at character 9 (which is stored in variable %SongSelector.y).
  • String ~SongList now contains {08} as it’s text.
  • Even though this is clearly a number, the DS lines that play sounds and music can only handle numerical variables, so we used line (5:282) to set %MusicPlayer.x to the number the string contained, which gives us %MusicPlayer.x = 8 as an integer.

In other words, to extract this number we use the (5:282) line, as it will ignore any extra 0’s. This results in us getting a whole number without any redundant 0’s.

Now that we know the ID of the song that was randomly selected, we can restore the %SongSelector.y variable to its original value (the value of the dice roll that was performed before to find the random song). This lets us find the corresponding duration of the song within the ~SongDurations string. In other words, we use the %SongSelector.x variable as a garbage dump to get rid of the resulting remainder, because we do not need its current value anymore:

(5:308) divide variable %SongSelector.y by 3 and put the remainder in variable %SongSelector.x.

To continue, now we do the same thing already explained above to find the randomly-selected song’s duration, except with a different amount of characters. This is because the ~SongDurations string uses a different length than those in ~SongList.

(5:306) multiply variable %SongSelector.y by 3.

(5:300) set variable %SongSelector.x to the value %SongSelector.y.

(5:304) take variable %SongSelector.x and subtract 2 from it.

(5:272) set message ~MusicFile to be the portion of message ~SongDurations from position %SongSelector.x to position %SongSelector.y.

(5:282) set variable %MusicPlayer.y to the first number in ~MusicFile.

And now that we finally know the Music File ID of the song that was randomly selected, we can play it to anyone present within the Dream. We follow it up with a timer that matches the duration in seconds:

(5:32) play music file %MusicPlayer.x to any furre present.

(5:50) set countdown timer 200 to go off in %MusicPlayer.y seconds.

Here's the full set of triggers:

(0:0) When everything is starting up,

(5:250) set message ~SongList to {01 05 08 09 17}.

(5:250) set message ~SongDurations to {124 071 105 109 096}.

(0:9) When someone arrives in the Dream,

(1:194) and timer 200 is not still counting down,

* Or, *

(0:50) When countdown timer 200 goes off,

(5:281) set variable %SongSelector.x to the length of message ~SongList in words separated by spaces.

(5:312) set variable %SongSelector.y to the total of rolling 1 dice with %SongSelector.x sides plus 0.

(5:306) multiply variable %SongSelector.y by 2.

(5:300) set variable %SongSelector.x to the value %SongSelector.y.

(5:304) take variable %SongSelector.x and subtract 2 from it.

(5:272) set message ~MusicFile to be the portion of message ~SongList from position %SongSelector.x to position %SongSelector.y.

(5:282) set variable %MusicPlayer.x to the first number in ~MusicFile.

(5:308) divide variable %SongSelector.y by 2 and put the remainder in variable %SongSelector.x.

(5:306) multiply variable %SongSelector.y by 3.

(5:300) set variable %SongSelector.x to the value %SongSelector.y.

(5:304) take variable %SongSelector.x and subtract 2 from it.

(5:272) set message ~MusicFile to be the portion of message ~SongDurations from position %SongSelector.x to position %SongSelector.y.

(5:282) set variable %MusicPlayer.y to the first number in ~MusicFile.

(5:32) play music file %MusicPlayer.x to any furre present.

(5:50) set countdown timer 200 to go off in %MusicPlayer.y seconds.

Account E-Mail

Password