I'm Coding a Game - Part 10
I know it’s been awhile since my last installment, but I have a good reason. I got tired of writing an article or blog every time I did a minor update or change. Also, I didn’t want the blog to be code/lingo driven. My editor (Yes, I have an editor… who happens to be my wife) would say that it gets really boring. Like a full grown adult I took her criticism in a mature way.
“You know how to really hurt me babe.” – NickelNDime’s response to his wife’s very reasonable suggestions.
In this installment I cover save and continue, parent and children sprites, and sounds. So, lets get to it!
Save and Continue
In my last installment, I added a menu with a continue feature. Full disclosure: the menu was not fully functional. I had to create the actual interface before I added the nuts and bolts of it, so to speak. If someone selected the continue option, it would just start a new game.
The first step that had to be created was the save feature. I wanted the game to save the progress after completing every level. I used what is called a constant to begin with:
What this line means is every time I type, “SAVEFILE” I’m telling the program to write to Save.sav. GameMaker Studio 2 does have a pre-defined location to save files, and I need to find out if I can change that.
Next, I wrote the code to save the progress:
This writes a number to a text file based on the level the player gets to. Each room in GameMaker Studio 2 is represented by a real number. If the character gets to level 2, then these lines of code will open the text file, write “2” in the Save.sav file, then close it. Now that we have the save function down, it was time to work on the load/continue function to make this whole thing work.
Now that I had a save file with information I plugged some lines of code into the routine sequence:
Case 1 now reads that if there is no save file, start the game as normal. If there is a save file, open the file, read the text, plug that number into the variable target, and plug that target into transition.
Now the game has a functioning save and load option. Granted, it’s very basic, but it’s a great starting point.
Child and Parent Objects
This was such a cool feature to learn about. A short explanation of child and parent objects is this: A child object can inherent the properties of a parent object with the ability to have slight differences. Why have this? Well, for example, if I had to code every enemy in the game, I would never be able to finish coding this game. With child parent objects, I can alter a child object slightly and it’s like creating a whole new object.
I created an object and called it, “oEnemyBig”. I assigned the exact sprite that was assigned to “oEnemy”. Under the sprite selection there is an option to select parent. I clicked on that button and selected “oEnemy”:
Now I could change “oEnemyBig’s” variables by clicking on its Variable Definitions:
I changed its size, speed, and hp to give it a slightly different feel, but I saved time by not having to code the entire object. When I run the game, now I get something like this:
This step was remarkably easy. All I had to do was write the sound effects in the code where they needed to be. For example, add a line of code in the gun object to play the gun sound effect every time the gun is fired. As easy as all this was, I still had one problem… I HAD NO SOUND EFFECTS!!! After I took a breath I discovered there are a ton of sound effect generators that are free to use. I used a program called, “Bfxr,” and it did the job just fine.
I created sounds for:
- Gun Shots
- Foot Steps
- Enemy Getting Hit
- Enemy Dying
- Menu Selecting
One last aspect was actually using a coding technique to vary the sound a little further. Take the foot steps, for example. If I use the same sound every foot step, it would sound natural and the player would start to focus on it. To get around this I created four different foot steps, and wrote code to choose one of the foot steps at random.
So, when the character walks, the game chooses one of four different wave files!
Pretty neat, huh? I thought so. Here’s everything I did in video form in case words scare you.
That’s all the updates for this installment! Stick around for the next installment, coming soon!