I'm Coding a Game - Part 4

I’m back with adventures in game coding! Did you miss me? It was so fun to dive back into coding and making art for this project. I can’t wait until I get to create the music for this game. I am thinking that will be more towards the end of this project. Let’s go over what I’ve accomplished since my last blog post about gaming. But first: the changes I’ve noticed from the previous version of GameMaker compared to the new one!
Differences between GameMaker v1.4 and GameMaker v2
I decided to purchase the most recent version of the program GameMaker v2 as I continue forward coding my game. Definitely a huge upgrade! Right off the bat, I noticed that the interface is so much easier to navigate. Many elements such as sprites, objects, and events are streamlined. They are also visually linked to each other. For example, if I click on a certain object, it then connects visually to the events tab, then to the actual code of the event. This makes it so much easier to mentally picture how one connects to the other.

Another new feature is that coding is checked in real time, and the program will let the coder know if a line is faulty. If a variable is created and not used, the coding screen will have an alert by it.

The new program will also let you know if there are syntax errors in real time, such as this one.

The last new feature I noticed was the background and layers window. This new feature allows the coder to place objects at different levels so all the objects appear in the correct order. In the past version, it was rather difficult and time consuming to achieve this task, but in CMS2 layers are arranged in a window:

Now the coder can visually see how it all lays out on the screen and can make adjustments quickly.
That covers all the differences I noticed. I didn’t spend oodles of time trying to spot every single change. I just wanted to get to coding!
Guns, Bullets, and Recoil!
Creating Gun and Bullets
The next step in this game was to give a weapon to our hero and make that weapon able to fire. The first step of the process was to create the artwork. There were three pieces of art I needed to make:

The gun

The bullet

…and the bullet flash. This isn’t the actual artwork. The actual one is just a white circle with a transparent background. When I posted the original, I couldn’t understand why I couldn’t see it. I spent 10 minutes cussing about how stupid the computer was until I realized I was trying to upload a solid piece of white onto a complete white background. Not one of my better moments.

Once they were all drawn up and loaded into the program it was off to write code for these objects and to give them properties.
Begin Step – A New Event
I used a new event I had never used before called, “Begin Step.” When code is written into this event, it tells GMS2 to run this routine before you run any other code for every single frame. This was important when placing the gun onto the character.

As you can see, I assigned the x and y coordinates of the gun to the oPlayer who is the main character. So now wherever the player moves the character the gun will follow and stay in the same place. Next, I wrote the line that makes the gun follow the movement of the mouse.

As I wrote out the above code I thought to myself, This isn’t so challenging. Then I began to code the firing of the gun sequence:
There are some other lines of code on the bullet event and on the gun create event, but this is the majority of the code. I’m not going to go over every line in this picture because I don’t want get the female readers all hot and bothered because of my awesome skills (Sorry ladies, I’m a married man), so I will give a summary. Basically, this code tells the program if the left mouse button is pushed and the “firingdelay” variable is less than 4, the gun will fire bullets. The routine then tells GMS2 to create a “Bullet” instance and project it at whatever angle the gun is at.
Some points of note are the “random_range” feature and writing code for the gun to recoil. The random_range command sets a random number range that the coder sets to the equation. This makes the firing have a bit of sway to it. Very neat little feature. Setting up the recoil required the lengthdir_y, and lengthdir_x commands. To make the gun appear as if was recoiling, I coded the gun to move back five pixels as it was being fired. However, this is a problem as the gun is constantly moving around by the mouse. So, the computer doesn’t know where the gun should move back from. That’s where lengthdir_y, and lengthdir_x come into play. They calculate where the game is pointing and recoil from that.
Tune in for part 5 of the adventures of NickelNDime coding a game. It’s been challenging, awesomely fun, and quite a trip so far. I can’t wait to see where the upcoming parts take me! Stick around to find out!