Code? Code! (Part 2)

Last time I talked about the tools used to build the game. That shouldn’t really say much, though, because any tool can be used to make something great! I think Hotline Miami was made in Game Maker, if that says anything. Regardless, this is the part where I open myself up to getting judged based on code, so here we go!

Timing —

The entire game is based on whether players can perform actions rhythmically or not. Needless to say, having a solution for making sure that the right rhythms were being represented was pretty important. I decided to have a GameObject in control of both the timing and switching between song sections, for no reason other than it making sense in my mind at the time. Attached to this GameObject is a script that keeps track of the time for 16 measures by using the samples from the song (at 44.1 khz) and doing some calculations to turn samples into units of time. I could have simply used measurements of time, but during my furious Googling period, I discovered that Unity is quite average at keeping track of time in small amounts, and I needed something better than average.

Arrays forever–

In the metronome script, the position of the song is compared to a 16 member integer array, which stores the subdivisions in 16th notes for a measure. I also have a variable that stores which 16th note the song is currently on. If the position of the song has passed, say, the 5th member of the array, then the variable will update to say that it is on the 5th sixteenth note of the song. There’s also a variable that tells me whether I’m actually on a sixteenth note or not. This array pattern actually structured a lot of my game, and pretty much any time I need to script something related to when cubes launch, I use a 16 member array that gets compared with the metronome.

Launching stuff and scripting launching and stuff and stuff–

Stuff. Everything in the game that needs the metronome can then listen to the metronome that’s letting me know what measure and beat I’m on, as well as whether I’m actually on a sixteenth note (there are a lot of public variables in this script). Then it’s just a matter of determining which kind of cube I want to launch and scripting it so that it launches on time. I have a script that handles launching of cubes by doing LOTS of comparisons to the metronome using arrays. The unique thing about this script is that it’s a set-it-and-forget-it system, where once a specific pattern for a 16 measure phrase is set, it will repeat over and over until I change it. I did this to avoid conflicts with scripting cube launch patterns and the position in the song — there are way fewer variables to look out for with this structure. There are arrays for all sorts of timing-related things in my game; checking to see when I should remove a cube from play, or making the cube respond to different rhythms, for instance. After this cube launching script, I just have blank GameObjects with scripts for specific songs (or in this case, one song, because I don’t have more than one) that listen to the metronome and adjust the cube launching script as necessary.

Whew! That felt like a lot! Next time I’ll tie it all together and give some sort of explanation of my code based on design and etc. It’s also the post that I’ll enjoy making the most I think!

Advertisements

About Space Town

Space Town is: http://space-town.org
This entry was posted in Uncategorized. Bookmark the permalink.

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