The Help file does a decent job at giving you an idea of what you can do with arrays. This will be a lengthy post, because that's how I am. It'll also touch on making a BGM object and how you might go about playing the file if you make scene and window for it (which would be advised).
With Ruby, there's never a single way to do things. It was designed to be fairly flexible which makes it easy to pick up no matter what programming languages you might have used before, if any.
All you need to create an array is a variable name to tie the new object to, and then call the appropriate class' .new method.
@jukebox_data = Array.new
I prefer to write Array.new if I am making an empty array, but you can just do:
@jukebox_data = []
Which does the same thing. I like to be explicit with what I'm doing though, and it helps if you don't understand what '= []' does. I come from a C++ background, so I often carry over my coding style from there.
You can also initialize an array with values:
@jukebox_data = ["data1", "data2"]
The [] will make a new Array object for us (as if we used Array.new) and then add the contents to it and the variable @jukebox_data will be assigned to refer to the new array. You don't have to pass in strings; you can pass in variables if you want to or other such things.
If you use an array that already provides values like the one above, you won't need to worry about using .push, but I'll put it anyway.
object.push("music_filename")
#adds the given argument to the array. You can specify more than one thing by separating them with a comma:
#object.push("file1", "file2", "file3") would add 3 files. For now, I'd stick to just working with one file.
object[0]
#This is the 0th element or value of the object array and assigns it to the variable data. The 0th element is the first element in an array. Thus, object[1] is the 2nd element and object[15] is the 16th element in the array. It's just something you have to get used to is that.
#You can use a variable instead of a number, to change what value we get from the array.
object[i]
#This gets the ith (eye-th) element of the array. Whatever value i is, is the element we are looking at. If i is 5, it's the 6th element. If it is 10, it's the #11th. This is mostly seen when you use a for loop with i as the iteration #value to cycle through a whole array one element at a time.
new_var = object[1]
#We can also assign a variable to refer to the contents of an array at a given element.
Those two (technically four but three are the same idea) things, let you put things into an array and then look at what is in it at a certain location (index or element) within an array object.
In that example, the object only holds file names in a string format. They aren't actually music files, but the name of what the file is. We can use the idea of accessing elements in an array through a for loop to make use another array containing actual music files. I'm going to assume we are only using BGMs first.
@jukebox_musicfiles = Array.new #new empty array
@jukebox_filenames = ["filename1", "filename2", "filename3"]
for i in 0...@jukebox_filenames.size
filename = @jukebox_filenames[i] #get the file name from the array at ith element
music_file = RPG::BGM.new(filename) #create a BGM with the same file name as the value of filename
@jukebox_musicfiles.push(music_file)
end
A quick note on the for loop. These loops will cycle over the code between the for and the corresponding end as many times as specified by start and finish. If we start at 0 and end at 5, we will iterate over the code in the loop
for each value from 0 up to 5. Thus we will go: 0, 1, 2, 3, 4, 5.
The format is:
for variable in start..finish
or
for variable in start...finish
'variable' is what iteration we are on starting from the 'start' value until we hit the 'finish' value. The dots between start and finish are important as they determine whether we include the finish value or not. With 2 dots, between 0 and 5, we will cycle 0, 1, 2, 3, 4, 5. If you we use 3 dots, we will cycle 0, 1, 2, 3, 4. 2 dots means inclusive, 3 dots is exclusive of the finish value.
Back to our code then: we use the value of i (our current iteration) to access to the values in our filenames array. The first iteration, 0, accesses the 0th element. The next iteration, 1, access the 1st element and so on up to the value of @jukebox_data.size. The method '.size' gets the length, or the size, of the array object. We have 3 data values in the array, so size is 3. Thus, our for loop is: ' for i in 0...3 '. We need 3 dots here, because there's isn't an element '3' in the array because it ends at 2 (@jukebox_data[2] is the 3rd and last item in the array).
Now, the variable @jukebox_musicfiles contains 3 BGM objects in an array. We can access these in the same way:
@jukebox_musicfiles[0] #the first music object
@jukebox_musicfiles[1] #the second music object
@jukebox_musicfiles[2] # the third music object
And we can also access them with a variable. If you look in Window_Item you can see this (and this will be helpful if you make a scene for this with a window):
def item
return @data[self.index]
end
Whenever we call @item_window.item, we are getting the item that is held in the @data array at position 'self.index', which is the same value as the item that the cursor is highlighting currently. Like arrays, the first value on the list in the Item window is 0. This lets us access a value held at a particular, varying, element in the array.
We could have a similar set up in our own window that also plays the BGM associated with the data we get:
def play
@jukebox_musicfiles[self.index].play
end
#which could also be written as
def play
bgm = @jukebox_musicfiles[self.index]
bgm.play
end
If this was an item list (which is basically would be), whenever we hit the enter key on an item we have highlighted, it will play the music file associated with it. This is what you want to try to achieve in the end.
There is a better way of doing this, like loading up a BGM object only after the enter key is hit so that we don't take up memory with a lot of music objects, but for the sake of learning how to use things, repeating the same steps is a good way to understand how to use things.
Hopefully that helps a little. I know it's pretty long, and I apologise for that. It makes it look like a lot more work that it actually is. There's quite a few tutorials out there, and Pacman wrote a pretty nice, quick read guide on a few things somewhere here. I'd try to learn what you can from the Help file too. It's is pretty helpful when you need to look up what methods or properties you have access in an object.
I'm definitely starting to think it would be worth writing up a guide of sorts. This only touches on a few of the surface things.
It'd be a long ass read though. I'd have to put pictures in it. Anyway, best stop typing or it'll be too long to post.