Tutorial 1: Using Arrays to play a random song from a list
We've learned the theory, now let's test it out with an example! Let's imagine we have a rad jukebox in our RPG Dream that is capable of playing a random song from a list when bumped into. The DS for that should be simple enough but uh-oh! The way our music files are named puts the numbers all over the place! They're named things like m4, m72, m2, m34, and m16.
What are our options, supposing we can't rename the files?
- Use a ton of DS lines to select a random song, or..
- Use arrays to create a list of song numbers that's readable in a sequential format! Bonus: it also uses very little lines of DS to set up, and is therefore more elegant.
This example will show you how to use arrays to accomplish this goal.
First we make a list of the 5 songs we want to be randomly played:
(0:0) When everything is starting up,
(5:311) use variable %Jukebox[5] as an array, and set entry 0 of it to 4.
(5:311) use variable %Jukebox[5] as an array, and set entry 1 of it to 72.
(5:311) use variable %Jukebox[5] as an array, and set entry 2 of it to 2.
(5:311) use variable %Jukebox[5] as an array, and set entry 3 of it to 34.
(5:311) use variable %Jukebox[5] as an array, and set entry 4 of it to 16.
Now the songs are ordered from 0-4! We can even give these songs names as well:
(5:276) use message ~Jukebox[5] as an array, and set entry 0 of it to {Wolf's Ballad}.
(5:276) use message ~Jukebox[5] as an array, and set entry 1 of it to {Rhapsody of the Foxes}.
(5:276) use message ~Jukebox[5] as an array, and set entry 2 of it to {Goblin March}.
(5:276) use message ~Jukebox[5] as an array, and set entry 3 of it to {The Whispering Aye-aye}.
(5:276) use message ~Jukebox[5] as an array, and set entry 4 of it to {Eternal Solitude}.
There we go! We now have a list of songs from 0-4, with names! You might have noticed that the variable and string names contain the number 5 within [square brackets].
Arrays have limited storage, and the first time an array is mentioned in the ds, it has to declare the amount of slots it contains. [5] means that the arrays in our example are being set up to hold 5 values. This is only required the first time an array is declared in the DS, however, if you use it every time, you'll remember exactly how many slots it contains.
You might have also noticed that we started at entry 0! The first entry in an array is always entry zero. It is simply the way the system works. Whatever entry you’re looking for in an array, you have to subtract one from the value. For example, if you’d like the 3rd entry in the array, you’d have to grab entry number 2. In sequence, this is 0 1 2 which is the 3rd entry.
Okay, so we’ve set up our values. Let's use item 183 as the jukebox and play a random song from that list when it's bumped. We will also emit the name of the song being played as an extra measure.
First, we need a way to detect when someone bumps into the jukebox, then we'll pick a random one of our 5 songs to play.
(0:3) When someone moves into item 183,
(5:313) set variable %SongSelection to the total of rolling 1 dice with 5 sides minus 1.
We first set a variable to a random number between 1-5, and then subtracted 1, since our array ranges are actually 0-4. In other words, these five numbers: 0 1 2 3 4.
Okay, now we know where to look in the arrays to extract the music # as well as the name, but how do we actually extract that data? We use the following:
(5:310) use variable %Jukebox as an array, and copy entry %SongSelection of it into variable %MusicNumber.
(5:275) use message ~Jukebox as an array, and copy entry %SongSelection of it into message ~MusicName.
Now we have both the song number to play, and the name of the song to emit to the player that bumped the jukebox. All we have to do now is this:
(5:30) play midi %MusicNumber to the triggering furre.
(5:200) emit message {Now playing <i>~MusicName</i>.} to the triggering furre.
There we have it! An elegant solution utilizing both types of arrays. This is the basic principles of array manipulation, but barely scratches the surface of what can be done with them. Be sure to experiment!
Share this:
-
-
-