Music Puzzle

A little bit ago I talked about a library puzzle, in which I set up a bookcase system where each case would remember what book was stored inside it. Turns out, this is useful for way more than books. Let’s talk about a music puzzle in Vidar.

The core concept is this: on activating the puzzle, 7 flames appear, one for each note in a minor scale. There are 10 torches placed around the room. The player will have to select one of the notes and “load” it into the torch. After all 10 torches are lit, we’ll play them in order. If at any time the notes aren’t correct, we’ll play a buzzer and led the player correct it.

We need some sound assets for this – each individual note, and the music we want the player to match. After that, we can do this pretty easily.

Making the Notes

Like with the bookcases, we need to have a single variable that we use to identify what note-flame we’re interacting with. So each flame’s logic looks like this:

Each music note will have a different SE depending on the note. They should also set variable 80 to a unique number. So B-Bell is 2, C-Bell is 3, etc.

Each music note will have a different SE depending on the note. They should also set variable 80 to a unique number. So B-Bell is 2, C-Bell is 3, etc.

The common event is equally easy, just asking the player if they want to carry that note with them. If they do, take the # of the note-flame we approached and put it in our lantern (our way of carrying this magical musical fire).

You could do this in each event's logic, but I like to abstract wherever I can.

You could do this in each event’s logic, but I like to abstract wherever I can.

Making the Torches

Each torch is going to have 3 pages – unlit, lit, and different colored. We’ll use the last one only as an animation when we’re checking to see if all of the torches are correct.

Like with the note-flames, like with the bookcases we need to see what torch we’re selecting. Here we can’t just do 1-10, because each torch needs its own variable. So torch 1’s variable here is 82. We’ll set our “selected torch” to 82.

While there are three pages, the logic is always the same. I just use this to change the graphic to show the torch burning.

While there are three pages, the logic is always the same. I just use this to change the graphic to show the torch burning. We’ll talk about switching them back and forth next week.

The common event is also a little more complicated. We need yet another variable – the content of the torch. So, just to distinguish what’s happening here:

  • Variables 82-91 are assigned permanently to a torch. Whatever note is in Music Torch 1, that is if Music Torch 1 is currently set to the note “B”, the value of variable 82 will be “2”
  • Variable 92 gets set dynamically as we interact with a torch. So, if we interact with Music Torch 1, it will automatically set Variable 92 to “82” – the variable which represents its contents.
  • Variable 93 gets set by the common event to the content of the variable represented in 92. So since Variable 92 is “82”, Variable 93 looks at Variable 92 and finds “2.” Variable 93 is then 2.
It's a lot of variables, but it's rapidly scale-able. Turning this from 10 torches to 50 takes a matter of *seconds.*

It’s a lot of variables, but it’s rapidly scale-able. Turning this from 10 torches to 50 takes a matter of *seconds.*

Why all these variables? It allows for abstraction. All each torch has to do is set Variable 92, and call the common event. That’s it. That’s bug free. Imagine if we didn’t do these things, and just had each torch responsible for itself. If there was a bug in our logic about storing and retrieving the notes, and we had copy/pasted that logic into each torch, we’d have to fix it 10 times. That’s a mess. Don’t do that.

You're not seeing double: $game_variables[$game_variables[92]] means the value assigned to Variable X, where X is itself the value of Variable 92.

You’re not seeing double: $game_variables[$game_variables[92]] means the value assigned to Variable X, where X is itself the value of Variable 92.

We’ll examine the common event “Play a Note” in a second. The conditional branch asks if there’s anything in your lantern. Skip to the else if there isn’t – we’ll ask you to choose a note first, otherwise there’s nothing for you to really do. If there is something in your lantern, ask if you want to put it in the torch (we’ll ask slightly different questions based on whether there’s something already in the torch, but that’s just flavor text).

If after all that, the player decides to put the note into the torch, we’ll backtrack from variable 92 to figure out what torch we’re talking about. That variable will get assigned whatever is in your torch. And we’ll call play a note, this time with the new note.

Got all that? Let’s play some music

Play The Notes

The play the note common event is actually really simple. Right before we call it any time, we set variable 93 to the note we want to play. Then it’s just a simple matter of conditional branches. A lot of conditional branches.

In strict Ruby, we'd handle this with something called a "case statement." Here, a series of conditional branches is just fine!

In strict Ruby, we’d handle this with something called a “case statement.” Here, a series of conditional branches is just fine!

In our torch common event we call this twice. Once when you interact with  a torch (to play you what note it is currently holding), and once when you place a new note in (to play you what you put in).

We’ve now got a system that allows us to grab music notes from flames and add them as we want to torches. They can be replaced at will. And the torches all remember what note they hold. Next week we’ll talk about how to play and check the solution!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s