Learn To Code by Making Games! No Experience Required | Aaron Craig | Skillshare

Learn To Code by Making Games! No Experience Required

Aaron Craig, Helping Everyone Learn the Best They Can

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
58 Lessons (7h 12m)
    • 1. 1.1 - Course Introduction

      1:36
    • 2. 2.2 Download & Install GMS

      3:46
    • 3. 2.2 - GMS Overview

      5:17
    • 4. Difference Between 2 and 2

      7:12
    • 5. 2.3 - Running YoYo Game Demos

      2:41
    • 6. 3.1 Creating Sprites

      7:56
    • 7. 3.2 Animating Sprites

      5:04
    • 8. 3.3 Importing Sprites

      2:14
    • 9. 3.4 Sprite Sheets

      9:15
    • 10. 4.1 Objects Overview

      5:10
    • 11. 4.2 - Object Events

      4:01
    • 12. 4.3 Object Properties

      3:20
    • 13. 5.1 Rooms Overview

      5:27
    • 14. 6.1 Getting Ready To Code

      4:05
    • 15. 6.1 Your First Code

      8:06
    • 16. 6.3 Variables

      9:40
    • 17. 6.4 If Logic

      8:10
    • 18. 6.5 Comments

      4:09
    • 19. 6.6 Reading Errors

      7:32
    • 20. 6.7 Functions

      8:35
    • 21. 6.8 Functions - Part 2

      6:11
    • 22. 7.1 Creating Your First Game

      14:58
    • 23. 7.2 - Planning Our Next Game

      10:06
    • 24. 8.1 - Import Player Ship

      3:30
    • 25. 8.2 - Player Movement

      4:37
    • 26. 8.3 - Restricting Player Movement

      7:31
    • 27. 8.4 Key Mapping

      4:07
    • 28. 8.5 - Player Lazers

      10:42
    • 29. 8.6 - Lazer Sounds

      3:36
    • 30. 8.7 - Thrusters

      11:50
    • 31. 9.1 - Room Settings

      4:14
    • 32. 9.2 Background Music

      4:44
    • 33. 10.1 Enemy Ship Movement

      6:57
    • 34. 10.2 - Enemy Lazers

      8:52
    • 35. 10.3 - Collisions

      7:22
    • 36. 10.4 - Destroying The Enemy

      6:26
    • 37. 10.5 - Player Death

      5:26
    • 38. 11.1 - Dynamic Enemies

      8:19
    • 39. 12.1 - The Debugger

      5:23
    • 40. 12.2 - Breakpoints

      3:35
    • 41. 13.1 Visualizing Player HP & Lives

      15:13
    • 42. 13.2 Player Score

      11:41
    • 43. 14.1 - Setting Up The Boss

      3:55
    • 44. 14.2 - Bringing In The Boss

      6:54
    • 45. 14.3 - Moving The Boss Around

      11:47
    • 46. 14.4 Screen Shake Script

      15:26
    • 47. 14.5 Boss Lazer

      10:29
    • 48. 14.6 - Boss Collision & Health

      8:03
    • 49. 14.7 - Power Attack

      5:48
    • 50. 14.8 - GUI & Level End

      6:59
    • 51. 15.1 - Setting Up The Menu Screen

      11:42
    • 52. 15.2 - Changing Rooms

      4:50
    • 53. 15.3 - Credits Room

      6:59
    • 54. 15.4 - Endless Mode + Levels

      9:33
    • 55. 16.1 - Arrays and Choosing A ship

      24:06
    • 56. 17.1 - Controller Support - Part 1

      12:20
    • 57. 17.2 - Controller Support - Part 2

      13:37
    • 58. 18.1 - Now What?

      1:06

About This Class

If you've ever wanted to make a game, or start programming, but have been intimidated by the process, this is the course for you! We're going to make a game from scratch using GameMaker Studio 2 Trial - $0, and 0 experience required.

This course will walk you through the basics of game development, game design, programming fundamentals, and more. GameMaker Studio 2 is an awesome engine for creating 2D and 3D games with ease. Throughout this course, you'll learn to use Sprites, create Objects, change Rooms, and create your own Scripts. We'll go step by step, explaining everything along the way.

The best part is that, by the end, you'll be ready to create your own games, or branch into software development in general. You'll learn real coding, terminology, logic, and more that will help you be a great programmer and game designer.

Transcripts

1. 1.1 - Course Introduction: Greetings and welcome. My name is Aaron Craig with Let's Learn This together dot com Now I am super excited. The to enroll in my beginner game development course with gamemaker studio to what's really cool is that you don't need to spend any more money on the software because gamemaker studio, too, has a trial version that lasts for a month so you can download that and get started on this course working just 20 to 30 minutes a day. You can complete it in that time frame and be able to make the awesome games that we're gonna create in this course. This course is specifically designed for people who have never coated before, aren't sure how to begin, but really want to make games. I remember how difficult it was to start programming and start game development when I didn't have the right resource is or the right teacher. So this course will help you get through that step by step along the way we're going to go through, and I'm gonna show you exactly what you need to know. Explain everything as we go so that by the end you will be a full game developer ready to start working on your dream game. And what's also really cool is this course is part of a trilogy of my courses, so you are going to be doing the beginner course and on my website. Let's learn this together dot com. You'll be able to find the intermediate course where we make a tower defense game and an advanced course where we make a classic turn based J. R P G. And if you send me proof that you purchased this course on here, then I will give you a discount on the bundle for those other two courses because I don't believe you should have to buy them in more than one place. So that all being said, I am super excited. I hope you are, too, and let's just jump into it. 2. 2.2 Download & Install GMS: Alright in this video, we're going to be finding and downloading gamemaker studio to. So if you already have it installed on your system, go ahead, Skip on to the next video for everyone else. Go ahead and navigate to google dot com. We're gonna start from right here. So the first thing we're gonna do is search for game maker studio too. The first couple of results are going to take you to the publishers company, which is yo, yo Gamestop comes to go ahead and click on the very 1st 1 and it is going to bring up a page like this. The splash art may be different, but what you want is in the top. Right corner here is called Get gamemaker. We're gonna go ahead and click on that. It's gonna load up this page right here. You'll be able to look at all of the pricing and the system requirements to make sure that your system can run it. It doesn't require much, so you're probably OK now, if you're interested in purchasing one of these, you can. But we're going to be using the trial version for this entire course. You do not need to spend any money to do everything that we're going to do to make your first game. If you do want to buy one right off the bat, I recommend going with desktop or if you just have all the money in the world, go for ultimate. So we're gonna go and click on download right here. It's gonna take us to this page. Now you will need to make an account. If you already have one, you can log in right here. If not, you'll need to register Gamemaker Studio to is linked to your account because when you purchase it or when you download a trial, you get a license and that license is linked to your email in the password that you give it . So if you need an account to go ahead and register right here, otherwise you can log in right here. I'm gonna go ahead and log in with my email that I have set up for this. And when I come in, it's going to then tell me what I have and again give me the option to purchase. But right up here allows you to download gamemaker studio tubes. I'm gonna click on that, and then it will download through Google Chrome, which is what I'm using. And it may take a few minutes, as it is 175 megabytes, depending on your Internet connection. It may take a little bit longer. I'm gonna go ahead and fast forward this video to when it is done downloading. But if you need to just pause and wait for it to finish on your machine, all right, now that it's done down there with a click on it once gonna come up and it's gonna ask me if it's OK to run on my machine going. Click. Yes, because we want to install it. Now all you need to do is pretty much just click. I agree. You'll want all of these things on here unless you know exactly that. You don't go and click on next and then choose where you want to installed. You'll probably be OK in the default location, so go ahead and click on install, and that is going to go through this process and again, this might take a little bit of time, or it might take longer, depending on your machine. It's not that large of a program, so hopefully it won't take too long to get up and running. I'm going to go ahead and fast forward to when this is done. Well, it's done installing, doing click on next and then finish, and now it is installed on our machine. If you're on a Windows machine, you can click of the Windows key and type gamemaker studio to, and it will pop up. If you're on a Mac, I think you use the finder. I'm not familiar with it. And now the first time it loads up, it's going to take just a little bit longer than usual. It's gonna go through getting it initialized on your system after the first load up. It won't take nearly this long, though, and you can see here is loading up. And then it will come up with the splash screen that you'll need to start making a new project. And that's what we're going to do in the next video. So now you have gamemakers to do to up on running. You are ready to start making games 3. 2.2 - GMS Overview: Now it's time to make our first project. We're not going to start coding just yet, but I want to get you familiar with the game makers to your two environment and show you were a couple of the settings are that you might want to tweak. So to start, we're going to click on new, and we're gonna be doing a gamemaker language project. If you click on Dragon dropped by accident, it may look the same at first, but when we begin coding, it's gonna look very different and you'll be quite lost. So make sure you do gamemaker language. Click on that. And when you do that, it's going to bring up a dial of window for you to save your project in a specific spot and give it a name by default. It is in a Documents folder in gamemaker Studio to you could move that around if you want, though I'm just going to call mine learning GMs and G. M L, which transferred gamemaker studio and gamemaker language and then click safe. When I do that, it's going to compile the project and bring up something that should look very similar to this now don't be intimidated. This is not to intensive. There are some game engines that when you load up, they just bombarded you with images and text game maker studios pretty good. It's clean and it's simple, but let's go over what they do. So on the right hand side here, you have your resource tree, which is everything is gonna be in tabs and everything. You can move around, so if you want something in a different location, you can drag it and you can move it into that new location. This one doesn't make any sense, but you can do that so you can also adjust the size over here on the bottom right hand corner. You could make the larger or smaller, or just click on this larger box to make them a good size that will fit your layout down here. You have a lot of different tabs, and we'll get into some of these later on. The most important one is gonna be this output. It's going to tell you if you run into any errors or how your game is compiling and how it's doing, then we have up here under file and preferences so This is where all of the settings were gamemaker studio to our I just want to go over a couple, the first ones. They're gonna be here in general, settings under background. You could change this background image right here, and I'm going to go ahead and do that because I have one that I'd like to dio, and it's actually gonna look like this. So if I press apply, you can see here that now my background has changed. Looks kind of cool. Now, when we get decoding, you'll be able to change all of the default colors as well. It will get into those as we do that so that you can see the effect they have. Another important thing is this. Options down here under main. Now this affects the game itself. Up here you have the game frames per second. If you're familiar with frames or FPs, you'll know that most games these days run at 60 FPs, which is what I'm going to change this to. You can leave it at 30 if you want, or if you're running on a lower and system, that may be better for you. But most games run at 60 FPs later on. A lot of these settings will be useful, but you can also look at your project. Use time if you ever want to know how long you've spent inside of a specific project, so the last really important thing is going to be under help. Open manual Now sometimes it doesn't open up. It just leaves a white screen, but most of the time, and it works. If it doesn't give your project a restart, and you should be good now. A little helpful key is also F 12. Or if you click on this little icon right here, you can get rid of all the docks or you can bring them back. So I press F 12 and this is the manual. Now I know reading manuals is usually really boring, but when you are coating, you need to know the documentation behind what you're writing. Because coding is not something that you can just figure out. It's nothing that you can into it because it's not intuitive by default. Gamemaker Studio to use is a great language that is very readable. So if I show you if I show you some code, you'll understand probably what it does, but that doesn't mean you'll understand how to use it before you start coating. So this is a fantastic place, and it has a search bar here that you can look for specific code that it will tell you exactly what you what you need. So don't forget this because it is really useful, and I'll show you how to access it quickly and how to read it as we get further on. But that's gamemaker studio to 40. You have this resource tree, which we're gonna be working a lot in, but it's not too complicated. It's not overly in your face. There's not too much going on, and it's really simple to get up and started. Which is good, because in the next video we're gonna be looking at sprites, adding our own and even creating one ourself. So let's jump into that next 4. Difference Between 2 and 2: Gamemaker 2.3 is a huge update that brings a lot of new features. But for the most part, everything should still work the same. If you import your project into 2.3, it should convert it without any errors in your game should run just the same. What I'm gonna do is show you what is now different and a couple of things that might be confusing if you don't know what it did. So the first thing is you have a new asset browser over here. Now it kind of looks the same. It might be just a different look, but it's actually a completely new way to view all of your assets and they are now not forced into any groups. So you can see here that I've got a folder called objects, rooms, scripts, and sprites with something inside of each one. Well, no longer do I actually have to put objects inside of objects. I could move this object into my room folder. And that's because all of these are, are just folders kinda like on your machine. You can put anything inside of a folder. The same thing goes for this now. So it opens up a whole new way to organize and to move your objects and your sprites and your resources around inside of here. So if you have a player, you can put all of the players sounds, sprites, objects, all of that in one group and make it easier to find. You can also color coordinate them as much as you want with specific objects or even folder. So it makes it very easy to find certain things, but that's about all the differences for the asset browser. Next is gonna be the sprites. Now, for the most part, they're exactly the same except the UI when you open it up, looks a little different if you have multiple frames there now down here. And you can see that if I add a few and come back there now here, and if I play, it looks like that. So for the most part, it functions exactly the same. It just looks a little bit different. And the origin now is this little arrow or this little diamond. It looks a little different, but same origin. You can just choose where it goes right up there. The big change is going to be the scripts. Now for the functionality of the core functionality, scripts are still going to be the same thing, except now they are going to be called functions. You name them and treat them like this. And a script over here in your resources can have many functions inside of it. And mine are white because that is a preference that I've changed, not because of GameMaker 2.3 or anything like that. Now the other thing is that you can actually add arguments inside of your script. So here I can pass in two variables, the exposition and the y position. And then I can use them inside of my script just as if I had done this. So now this is argument 0 and this is argument one without having to come in and actually specifically say that with that in mind, that's about the only difference when it comes to scripts. The way I'd recommend using these is to group certain things together. So if you have lots of camera functions or scripts, you can now put all of those together. Things like the get Camera X or get camera why or the middle camera or get active camera. You could have them all inside of one script that just says camera. And all the functions are right there. You still call them exactly the same. So if I add a creative vent inside of here, I could call it get Camera acts. And they call and function in work exactly the same as you would expect inside of a previous versions of game maker. They are now just functions. You can have many of them inside of one script and you can pass in arguments there as well. And the other big thing which is not going to be covered in these courses is the sequences and animation curves. These are both really great and powerful tools. So I'll just show you really quickly, but I'm not actually going to get into how to use it. A sequence is a way to animate things, anything inside of your game with full keyframe control. So if I drag a sprite inside of here, you can see that I now have a sprite and I have a whole Animation panel down here. And I can actually control things like its position, rotation, multiply, image index, image speed, origin, and scale. And that can control that with keyframes. So if I move up a few frames where I find it because the sprite is actually animating. And I move this right here. Now, it's actually going to move over the course of those frames. So it's making keyframes for me. And I can add more and I can adjust everything. And then you can create sequences inside of your game to play animations. You can have sequences that have objects in them that you can replace with the instances in your game. You can send out an notifications like broadcast messages and moments so that you can see when a sequence is playing or what's happening inside of it, and then have your game actually react to that. You can now your sequence have events that do specific things during those events of the sequence running. Really, a sequence is an extremely powerful tool that you can use for almost anything in your game if you so desire my plans to use them for robust cutscenes and really anything that I want to animate inside of my game. But you can use them however you so desire. And an animation curve is just how the keyframes down here work. So if I right-click down here, I can convert this to an embedded animation curve. You can see that I can move these around and I can actually change how it works. And I could take this animation curve that I can play around with. And I could change it to linear or smooth. And I can move these and add dots and change us all around. And then I could take this into an external animation curve and actually save it as a resource over here that I could then apply to other things inside of my sequence. Then just the position for this one sprite that I've got dragged inside of there. And that's really all of the differences. In 2.3. There are a few code differences, but for everything that you're gonna do inside of this course, it does not matter. Everything that we are going to talk about is still valid and functioning and working just the way you would expect it. The biggest difference and the one that I think will confuse most people are the scripts. So they are functions. You name them, and you can pass in arguments, just like we would do when we are getting argument 0 through whatever number of arguments that we have inside of there. Hopefully that helps if you have any questions, don't hesitate to ask and if you want to see any more examples, then feel free to also ask for that, but that's all I've got for you. So go ahead and jump right on into the course. 5. 2.3 - Running YoYo Game Demos: all right, Now that you've got game major studio install on your machine, I want to show you how to create a project from a demo and run that games that you can get the experience of compiling and then playing a game that's built in gamemaker studio to on your machine. So to do that open up gamemaker studio, if you don't already have it open and then click on demos here, you'll see that there are some official demos down here and I've got this one appear because I've already downloaded it before to down a demo. You just need to click on the little download icon. It'll only take a few seconds, depending on your Internet, probably. And then you can cook on the play buttons. I have the platformer light, and all you have to do is you name it and then you save it. So I'm gonna save it as this is a copy of that, Uh, not in there. And then it's going to open up the I. D. E. Itself, the integrated development environment. So we have objects and rooms and there's lots of different things that we can look it over here don't worry about them. Right now, all you need to do is click on this little triangle or press F five to run your game. Now I take a few seconds to compile and to build, but once it's done, you'll see that you've got a game that you can play here now. It's not a fancy game or anything. You just use the arrow keys to move around and you use the space bar to jump and you're collecting stars and trying to avoid the ghosts. It's really simple, and it comes from Euro games. But it gives you this feel that this is kind of the things that you can make anything that you can envision that is two D and even some three D things. You could make it side of gamemaker. Anything that you could envision like this you can create with this software, and by the end of this series, you will be able to do that. So here we could move around. We see it's in a window that we have here. We have controls, we have a player. We have physics we have in Iraq, interactions with different objects. We can take damage things like that. This is a game that I might not find to fun. But it's a game that works and that you can play so you can take a look at the other demos . If you're interested in those, they also have some tutorials from yo yo games officially on there you could look at, but you don't need to. That's why you're taking this course right? So hopefully that helps holding that gets you excited because you are going to be making an awesome and much more fun game than this one in this course. So with that being said, let's get into an overview of gamemakers so you understand how to create Resource is, and then after that, we're gonna dive into your very first game. 6. 3.1 Creating Sprites: All right, So let's get down to making our first Sprite. So a spray is a two D image which we are going to draw inside of gamemaker studio to, and I'll show you how to do that, the tools that are available and talk about some naming conventions along the way. So we're gonna open up our resource tree right here and right click to create a sprite. Now, as a side note, if this ever disappears like you click on this X or for some reason, it's just vanished. You can always come up here toe windows and you can click on whichever window you're looking for. In this case, resource is, you click on that and it appears, And then you can dock it in whichever side you wanted to go on. Resize it. So that's the right size and you're all set. So I'm gonna go and right click create Sprite. You can also come up here to Resource is and you can create any of the resource is that are available, including a sprite, which is right there. And you can see the shortcut for this is salt s. So this brings up our sprite editor window first we want to do is give our sprite a name so you can rename it from over here in the resource is or you can rename it from over here under name. I'm gonna name SPR Smiley Face. We're gonna do something very basic. Now, notice the way I have spelled this here because it is going to be important. All right, so I'm using S p r, which is the prefix for Sprite, and then I'm naming it with a capital s and a capital F. I'm capitalizing the first letter of each word inside of here. Now, I'd recommend you use this naming convention, at least for this course. That way you're typing out the same things I am. And you can understand why I'll get later on into the importance of following a specific naming convention. But if you want, you can also do something like underscores or dashes. It's not a big deal. Just make sure you pick one naming convention one style and stick with it. All right, so now we want to go in and we want to actually create our sprite. We'll do that by clicking on edit image or you can double click on one of the sprites. This one's empty, but if I double click on it, it will bring up our image editor. And you can see here this is a whole new window. Whole new workspace, actually. Right up here is your workspaces. So you can click on this one, and it will take you back to here because this is its own workspace. And if you ever wanted to take this, put another monitor. You can drag that and drag it over to the other monitor, and then you could be working in multiple workspaces at the exact same time. And you can also then come and dock fees so they're back where they go. So inside of our image editor, there's quite a bit going on here. If you've ever drawn inside a photo shop illustrator or really anything like paint or Gib, you might be familiar with this. If you're an artist, you could barely pick up with this pretty quickly. But if you're not, I'm gonna go over this. So over here we have brushes which changed the size of what we're actually drawing, and then we have colors. Now, there may not look to be many colors right here. But if you click on this box, it brings up a huge slew of colors which you can also input hex colors, which could be very useful because you can go online and search for, like, a color scheme for a sprite or a layout. And then you can type in those hex values and you can save those right here under custom colors, and you can choose every color under the rainbow, and you could have two colors here. So if I left click, I'll draw white. If I right click, it draws black, and you can assign those by clicking on these arrows right here and choosing your colors. All right, so then we have our toolbox. Now, the toolbox has a lot of different tools. We're not going to get into how to use all of them. But I'm gonna show you a cool little trick right here, Which is these three tools. You have a rectangle, the lips and try and polygon tool. And you do an outline like this. Uh, yes, from a polygon tool. Uh, but you can also do a fill on either one of these. So this is asking me to commit because I cooked out of it. I'll say yes. And now that's there. So you can do a fill. Or you could do an outline on any of those, which is very nice. Someone Go ahead, impress Control Z to get rid of that which is also under edit. Undo, redo. In case you're not familiar with that trick and the last and pretty important thing is actually the layers right here. So if you've never used layers, you can imagine putting transparent pieces of paper on top of one of another. Then at any time you can take out one of those pieces of paper and it will fundamentally change the image. Look at this. We're gonna do a fill ellipse. We're gonna do a nice blue, and I am going to hold control and zoom out with my middle mouse wheel that I'm gonna click with my middle mouse wheel and move this around. That way I can see the whole image. Now, if you also used to drawing with grids, you can click on this right here and toggle grid, and you can change that grit size, and you can also set it to snap to the grid. If you want, like this right here, you can see. But I'm not going to do that. I don't want it to snap. Instead, I'm going to start at the top left and go to the bottom right? And you can see here that this is now showing what we have inside our sprite. But if I add another layer, it comes up on top, which is important. I can go to our pencil tool and I can draw some eyes and a smiley face. No, these layers are different. If I click this view button, I can make one of them disappear. And if I put them in a different order, it will also change because now the blue is on top of the actual face. So the layer here is pretty important, and then upheaval. You have image effects, which could be things like cropping, resizing, even changing the image weaken mirror and flip it. So if we were to flip selected flame frame there you go press control Zito under that, then you have a few effects like blurry and gray scale, and they'll add more effects as well, but this is a pretty powerful tool for drawing your sprites. If this was animated, we could view the animation right here. But that's gonna be in our next video. But if we go ahead and click this X, we can see now that we have our sprint. And if we look in our resource tree gives us a preview over here as well. The last important thing is this origin right here. The reason for its importance is going to be described later on when we actually get objects into our game and I'll show you how that works. But for now, almost every single time you're gonna want to click on this drop down and do middle center that will keep everything working smoothly and making sense later on. If you have a sprite, our character moving around and he seems to be jumping all over the place while you're moving him, it's probably because his origin is not directly in the center, but that gives us a sprite. Ah, very beautiful smiley face. Or maybe you troops something much better. But that's a spray inside of our game, which is a good first step, because remember Every game has graphics. And now our game has something which is awesome. In the next video, we're gonna learn how to animate it and do a little more inside of our image Editor. 7. 3.2 Animating Sprites: so we have a Sprite. But now how do we animate? How do we make it look like it's alive and moving in doing something? Well, it's actually kind of a trick. It's an illusion. So our Sprite is going to be animated by putting a lot of images that are very similar back to back and playing them at a high speed. Now this is very similar to a flip book. If you ever made one of those stick figure flip books that move when you spin them really fast, kind of like this old Mickey Mouse and Donald flip book, right? So each page is going to be a Sprite, and when you put them back to back, it looks like they're animals like they're moving. It's an illusion, but it could be a very good illusion if your sprint has a lot of sprites in it, or a lot of sub images taken look like it's very much alive, and it can also look very real and animated. So it's do that. We're gonna go ahead and come in here. I'm going to go to editor image. I'm going to right click on the image, and I'm going to copy it That over here, I'm going to right click and paste. So this gives us to But if we press play, you can see this moves. But nothing is happening because they're the same. So I'm gonna click on the second Sprite here and now with our second frame, what we're gonna do, because we're gonna turn his smiley face into a frowny face. So make sure you've got Layer one selected. We're gonna do this a little bit of the time so that it looks kind of natural. Now, reminder. I'm not an artist and I'm not an animator. So if you can do better than go right ahead, this is just gonna teach you the principles behind animating a sprite. So I'm gonna go ahead and start deleting some of this with my eraser and because I'm on layer one, it's on Lee deleting his actual smiley face, not the blue behind it. Now I'm gonna click this brush and make sure you've got the pink selected, which is this one right here. And I'm gonna go ahead and kind of make it so that it looks like he's not smiling as much. I'm going to the same thing. I'm gonna copy this, Gonna paste it and again on the new one. We're gonna come up to a race which is E for short. We're gonna race a little more, and then we're gonna go back to the paint brush tool with his d. Keep going like this, and same process again and again. Animators are very patient, and they work is sometimes very, very tedious. This might give you a very small example of that right here. So you can see that he is no longer smiling as much. He's starting to look a little cross, maybe a little sick. I'm not quite sure paced. One more can erase this part. And then we're almost done. So now he is not smiling. He is quite sad. We'll go and copy this one more time. Paste it and well, I'm gonna do is make a very deep friend. Okay? The group's edited the wrong one. We can move these back and forth. So you see that now? If I press play, you can see that it is very awkward. That's because this speed is too fast. We only have six frames right here. So what we do is turn the speed down. Let's say 25 and press play and you can see that that's a little more natural. I probably should have moved this part, but that's okay. So I'm gonna pause that exit out of the image editor and you can see that all of them are now inside of here. The speed you have two choices for you can do frames per second or frames per game frame. I'd recommend sticking with frames per second and for lower animated sprites for things that don't have as many Sprite images inside of them, I'd go with the lower speeds. Let's try something like four, and that looks OK. Hopefully yours looks better than mine, and you can see that this speed you can change. And so, as you change that, speed it, please. Much slower or much faster. And this speed is important because this property is actually going to be part of this sprite. It is going to dictate how fast the Sprite plays inside of your game, so make sure you choose a good speed. It's something that you can change later on through code, and you can come back here and change it. But you want to make it look as smooth as you can the first time around. And then in the next video, we're gonna be looking how to put these animated sprites into our rooms. 8. 3.3 Importing Sprites: so we know how to create our own sprites, and we also know how to animate our own sprites. But what if we want to use sprites that we don't have created inside of gamemaker studio to what do we do when we have sprites or art that we want to bring into our game? That is not our own. Well, it's actually really simple. There are two ways of doing the first way is navigating to that folder in a separate window and you can actually go into whatever you want and just drag and drop that file onto sprites and you can see here that it comes up. It is named exactly what it was inside of the folder on. We can rename that to whatever we want, and it is inside of here. Now, the other way is if we right click would create a new sprite, and we're gonna leave that name spray one, and we just go to import. When you do that, you bring up whichever folder you were in last. But then you can just navigate to wherever you were, and you could bring in whatever kind of art file you want. That is the two ways of bringing in our assets that are outside of your game. Now, what if we wanted to bring in something that was animated? If we open up our folder again, we can navigate to something that is animated like this folder or this file right here. This running shadow is a gift, and if I drag this inside of here, it will actually automatically animate it for me. You can see here if I play. This looks like he's running. This is a gift file. So if you have any gifts that are animated, you can just bring those in either from a separate folder or by importing them. It's actually very simple to do. But what about Sprite sheets? Maybe you've heard about them. Maybe you haven't. They are a very common way of storing large amounts of sprites of different kinds. Inside of one file. In the next video, we're gonna look at how gamemaker studio allows us to bring those in and make them animated in fairly easy ways. As long as the spreadsheet is well done, 9. 3.4 Sprite Sheets: Once you start working on two D games, you're bound to run into Sprite sheets all the time. They are very useful and very prominent in the art community. A lot of people make them and distribute them. Oftentimes, if you buy art assets, that's how you will actually receive it in several different formats, like an illustrator or Photoshopped file. But they will be in a spreadsheet. So what is a spreadsheet? Well, first off, it looks something like this. This is a character spreadsheet, and if we zoom in, we can look at this and see that this is a character that is animated from left to right in these different rose So we can see here. This is the walking pose you have walking up, and you can look at her feet moving, walking to the left, walking down and walking to the right. This is a specific character sheet. You might also see things called tile sets, and those are spreadsheets that arm or environmental based either for building an over world or maybe some kind of house, something like that. So a spreadsheet can contain really any kind of image, but it'll have this kind of format where it might be animated from left to right, or maybe from top to down. There's no set precedence. That's usually up to the artist who compiled the spreadsheet. Now this one isn't especially good spreadsheet as we will look at when we actually import this. We're actually gonna use this specific spreadsheet which is available for you to download and test yourself. There are bad spreadsheets out there that do not have all of these sprites a uniform distance apart, and you have to manually stitch them together. There are tile sets that are put together very poorly in different pixel sizes for each asset inside of it. Which ones you get working on, it will drive you bonkers. So the moral of the tale is get high quality assets where you can and pay for high quality assets when you need them, because otherwise you'll spend a lot of time and frustration. Okay, so we're going to right click, and we're gonna create a sprite. Now, this image right, this right here, this creates sprite from images, is tempting, but that actually is not what we're looking for. It's going to stitch whatever sprites you click on together and not allow us to create a one sprite from a sprite cheat. So we're gonna create a spray. I would have named this SPR. Um, Sara walk. Look up, I think is the 1st 1 that was there. So I'm naming this because the character that we were just looking at is actually Sarah from the website. Open game art dot org's. And if you are looking for free art assets, that's when I have a recommend. Now we're gonna import it just like we normally dio. So bring it in. We're gonna open it initially a couple different ways. This first way is bringing it in as one sheet, and you can see here that it's all together, and that's not really what we need. So we're gonna open it up. What we're gonna do is go to image and we're going to convert to frames underneath image. We're gonna convert this to frames, and this this menu can be a little bit confusing. I'm gonna control and zoom in with the middle mouse wheel and then middle click to move around, and I'm gonna find right here, which is where we want, but I'm going to zoom out just a little bit so that we can see the top left as well. Okay, so this this window here is basically going to allow us to take these individual images and pull them out and stitch them together and keep them in one sprite. So it looks animated The way it does that is by these options right here. So we need to tell it how Maney frames. So how maney individual images do we want? So let's count that up. We've got 123456789 So we need to put a nine in this box. If we were pulling anywhere else, it would be one less to less three less Down here, you get the idea. And since we put a night in here, you can see there's now nine boxes, but they're going up, down, up. That's not really what we need. So we need to say per row, there's gonna be nine in there. So we say nine again, and now they're going from left to right. But they are not spaced properly, and that's because they need to be of a different size. So the actual width and height. We need to figure out. So the width, I believe, is still 32. But the height is gonna be 48. What I'm gonna do is actually left, click and move this. I'm gonna put this right over her and I'm going to zoom in so they get the first box just right so you can move this around right here and it looks like she's a little bit larger than 32 pixels. So I'm going to say 34 and then bring it down and we're close. It's trying 36. I'm going frame with Keep that 32. We're gonna go to 50 here and now you can see here that this fits perfectly. It's exactly her size, except for this one right here. So let's add 52 because we can trim that as we need to sell. Now we have this. Now what we need to do is actually set a separation, and it's going to be a horizontal separation because we don't want this empty box. There's nothing there. So instead, we need to say there's gonna be 32 pixels of separation between each sprite, which is in this horizontal separation box. As soon as we put that in and zoom out. Now you can see it's got a box around this entire row and I'm gonna go ahead and click Convert. So it's going to give me a confirm option that I want to say yes, and then this gives me the sprite. And if I press play, it looks like she is walking. That's the first way of doing it. The next way is very similar once you get to the convert two frames, but to get there is a little bit different. So we say SPR, Sarah lock left. We go into here, go into the sprite itself. There were going to say image and we're going to say import strip image. We're gonna click on what we want, and then it's gonna bring this up immediately. So it's a little bit quicker. Maybe if you're used to that. Otherwise it's the exact same process. Once you get into here, it's the exact same thing. You're going to find out where you want. Put the box around that. Adjust the number of frames, frames Perot's. With the height, the separation and the horizontal pixel offset over here is when you're actually moving this box. If you need to move it just like one pixel over, you can adjust that right here. All right, so that is using a spreadsheet. You can see that if the spreadsheet was not spaced perfectly, if there was, like 16 of pixels between the first image and 25 between the next, then what you would have to do is manually stitch all of these images together. You would need to come in. You would have to import the spreadsheet just as one image. And then inside of here, you would be using the rectangle select tool. And then you would be choosing each frame along the way at a new one. Pieced her in there, select again, cut paste, so on and so forth. And then when you're done, you delete the original come up to image, and there's ah, auto trim all frames right here, which sometimes works pretty well and sometimes doesn't. So you would need to then move her so that she is in the same spot as the other one, some mover down towards the bottom and then this one to the bottom, right, as well right there and then we would want to trim one more time and they go, so you need to do that individually for all of them, which would be very tedious. Having a good spreadsheet is immeasurably helpful and saves a lot of frustration in a lot of time. Up next, we'll be looking at levels or rooms as gamemaker studio calls them, and what they are, some of the properties in them and why you need to use them. 10. 4.1 Objects Overview: in this video, we're gonna be doing a brief overview of what objects are inside of gamemaker studio. So objects over here on the right in the resource tree, they are one of the many options and many things you can create. An object in a game is basically anything that is going to be active or doing something that requires code to run. So you can think of an object as a player, character, enemy, maybe a house. Even it could be just an invisible object that is storing the time played the experience of the player. The score that they've currently got objects are where you put most of your code, and they are what is actually interacting inside of your virtual world. So to make a new object, you right, click and you can create it. Now you can name it over here, over, over here, in the actual object window that pops up. So I'm gonna do a prefix of O b J. And I'm gonna call this test now. Right underneath is the spread option. And right now, in this new project, I don't have any sprites. So if I click this button right here I can make a new sprite. I'll double click. I'm just going to fill this up with a nice color, very bright. And I'm gonna close that. And you can see now that this has a sprite assigned to it over in the resource tree and right here. And if you want to change that, you simply click on this and you can choose from all of the sprites that are available. These options are for editing your sprite, which will bring up the Sprite editor window and then also editing the specific image that is on your spray right now. Underneath here is a collision mask. For the most part, you want to leave the collision mask the same as the sprite. Where that actually is adjusted is in this Sprite editor window. So it's over here in the bottom left corner, and I haven't talked about them yet because we aren't were up to that point yet. When we actually start coding and want objects to collide, this will become much more important and will touch on that. Underneath that are four options which are fairly important to understand. Visible is the 1st 1 that is the easiest should this object be visible or seen inside of your game? If it is checked false, it will not be seen unless you go in through code and tell it to be visible. If it's checked, then it will be visible over over. Next is solid now. Solid can be a little confusing when you actually start programming, because you'll think that Oh, I've marked these things that solid. They shouldn't run into each other. They should be solid and know that they collide. And that's not exactly the case. Depending on how you set up movement, it will work or it won't work. Solid simply means that if the collision is being checked and I'm inside of that check, mark me a solid and don't let that thing run into me. You don't have to use those specific collision checks, though, so it could be a little confusing. Over next is persistent now. Persistent means that should this object move from room to room or from level to level, and you can think of this as if you are on the menu screen. Do you want your game object, which is gonna be holding all of the data for your game like how long you been playing your score, The experience, things like that. Do you want that to stay in the screen level? You don't. You want to continue on so you'd mark that as persistent. You probably also barked the player as persistent because you'd want them to move on to the next level once you go there and uses, physics is tied in with this physics option down here. So if you want to do a physics based games like angry birds or silly one like goat simulator, this is the way to do it. You can set every object toe, have its own values, things like density, collision group, friction, all of these things you can set and then you can tell it This world is going to be a physics world Now. The thing to note is that if you do physics, it will be different than most of the code for movement that we do inside of this course. Physics require a different kind of movement, whereas normally were going to say move five pixels to the right. When you press right on the keyboard, physics says begin movement this direction, and it takes into account the friction and the surface you're on and how heavy something is in all of those different factors. So you have to code it a little bit differently. Next up, we're gonna look at events because they are extremely important and it is the key to putting your code in the right place. 11. 4.2 - Object Events: Now let's look at events to start off. Let's ask ourselves, what is an event in game maker? Studio actually has a very helpful section here in their manual about events. So if we click on this, it talks about how they are the building blocks of gamemaker studio. They are the things that are going to run based off of the code inside of them, and they will trigger at the appropriate time. Now, what is the appropriate time? That depends on the specific event. There are a lot of them. So if we look at the events, we have things that create destroy, step alarm down to other and a synchronous. All of these events happen at different times during the game. Some of them are pretty obvious. If you have a create event, you can see here that it is a place to put your code and it will trigger. It will activate when the object is created, so it's only gonna occur one time. Now you might have an event like destroy. It's the opposite of create. This will only occur once when the object is destroyed. Then you have some events that are a little more confusing. Like the step event step in gamemaker studio is synonymous with frames, so every frame or step of your game the code inside of here will run. That means that if your game is set to 60 frames per second, this is going to be checked 60 times a second. You have things like drawing, so you can manually draw things on the screen if you want to. You have events that make a little more sense that are related to peripherals like mouse. So if you left click down on the mouse, the code inside of here will run these air. All events Gamemaker Studio is event driven inside of your game. When a specific event happens, it will trigger the code you've got in that event. So there's an event for basically everything that can happen. And if there's not a specific event for it than inside of the step, event is where you would write the code to check for that. Like if your player reaches a certain circle inside of your game and maybe that circle is invisible. But it's the cue to trigger a cut scene or to bring out a boss or to start a fight or whatever. You can set up your own events inside of the step event here, and they could be checked 60 times a second. So all of these events are useful. You may not use them all, but it is a good idea to know that they exist. For instance, if you want to check for a collision, that's right here, if you're making a mobile game, they have gestured based events. If you want to get the keyboard, if you want to get the mouse. If you want to set an alarm, which is very important, an alarm is a way of counting down. And when the count reaches zero, the alarm activates. Alarms are very useful for a lot of different things, and in here you can see we can have 12 of them. So these events are where you put your code and how the game knows when to do certain things. They are the building blocks the backbone, and every single object has its own events. If we make a new object, this object is completely unrelated to O. B J test. Over here, it doesn't have its sprite. It doesn't have any events, every single object can have its own events and its own code to do whatever it needs to be done up. Next. We're gonna look at just a few of the built in properties, toe objects that will be very useful to us as we begin programming our games. 12. 4.3 Object Properties: so every object in gamemaker studio has a lot of properties that are built into it. They are very useful if you know how to access them, and then you could pull them out and tweak them while the game is running and you can see some of them over here already, everything over here is accessible through code, so setting things too visible in solid and persistent. Those are details about this object that gamemaker studio to records and makes freely available for us to check and alter when we need to. Inside of the manual. It has a list of everything that you would need to know about your objects. So some of this is going to be unnecessary for where we're at right now. So I'm just going to cover the things that you might find useful and interesting. The 1st 1 is going to be these ones here, so the i d. Is the specific object you've got right here. It knows every single object that is in your game that you have the visible, solid and persistent. Those are things that you can access and they are they look just like this and code you have alarms, those air, the alarms that we talked about. Every single object has them and you can access them here. Now sprites themselves also have properties. So if we were to open the spread up, you can see that it has a width and a height. It has an origin. It has a collision mask which we just briefly touched on. All of those things you can look at and some of them you can alter through these built in properties inside of your game, you have things like the direction it's moving. You have the place it started in your level. You have the current X and Y position inside of your game world. All of these things and more gamemaker Studio two saves for you and it works on its own that when you need it, you can access them. But otherwise they kind of just stay out of the way. If you don't need to know the gravity direction of the current object, or of the world that you're in, you don't have to deal with it. Gamemaker studio keeps that updated all of the time for when you finally need it. So there are a lot of built in properties. And this is true about every single thing that you're going to see inside of gamemaker studio about timelines and funds and she aiders and scripts and rooms there are built in properties, toe all of them, and you just have to know how to find them and then how to access them. We're not gonna worry about that now, but we are gonna be using a lot of built in properties for a lot of different. Resource is throughout our game, both sprites and objects and rooms Using built in properties is a great way of generalizing your game code. And we're gonna talk about that at a later date as well. Really? What I want you to know is these air here? If you need them, don't forget them. So with that being said, with this video being very vague, I know let's jump into what rooms are, how they work in gamemaker studio to and why you need to use them 13. 5.1 Rooms Overview: studio rooms are synonymous with levels over in the resource tree. Here, you can see we have rooms and we have room zero, which is put in here by default. So if we double click on it, it'll actually open up a new workspace specifically for the room editor. And then we can look around our room. Right now it's super boring. Okay, there's nothing there. It is just a black background, and there's nothing inside of it. But what we can do is actually adding our sprite. Now we're gonna put in a sprite in a room, and we're gonna run it. But there's gonna be nothing we can do with that Sprite. There's no code. There's no logic behind it beyond play in a loop. So we're not gonna be able to move it to click on it to do anything interesting. That's gonna come in the next video. But we are going to see how toe add different layers even inside of our room, just like there's layers when you're editing the sprite. So over here we have actually called layers. So we have instances background, which you can actually change the color right here. So if we give this kind of ah, nice to go a nice green color who? It's not so nice, is it? Let's go with, like, an orange color. Okay, So again, you can press control to zoom out with the mouse wheel, you can move it around. You can also click on this center right here to fit it in completely. Now you can add more layers down here. Some of the layers air pretty obvious. There's a background layer. So if you wanted to have multiple backgrounds, you could. If you wanted to have another instance layer, you could. Now instances will get into it a little bit. But that's kind of where you put your objects and everything that's actually gonna be playable in your game. There is a tile layer, which will get into later a path slayer and in asset Layer, which we're going to click on right here. So this asset layer actually allows us to put sprites directly onto the screen. Now, as a note, if you're clicking on background or instances and you try to put this in here, it's gonna come up with this error. It's going to say that it can't do it because it's not the appropriate layer. Then it will actually tell you that it's an object layer and you're trying to put an asset . So you click. OK, To get rid of that, just click on assets and to be able to put it in there properly so you can see we can move these around. If I hold Ault, I can place a lot of them inside of there. Now, this is a really good way to create kind of a interface for your room for your level. Now again, remember, we're not able to click on these. There's no code behind them. If we double click, it's gonna open this up. The only option we have is how fast it plays, which it starts on. So if we were to change this to five, uh, you can see right there that he is now unhappy because if we open up our Sprite editor, the 5th 1 is right here now counting and computers could be confusing. Take my word for it. They started 0012345 So instead of our room, we said it to start on frame number five, so it starts there now to run our game to actually see this in action. We can press F five or this run right here, okay? Or you come up to build and you can run it. There's debug is well, which will get into later. But for now, let's go ahead and run this. We're gonna see it pop up and this is our game. You can see that they are animating. And this one right here is a little bit off as well. So that gives us an idea of how they look and what's going on with it again. Click. I can't do anything interesting. But instead of a room, there are some options that are important to know as we get to making our game. So we have our layers. These are very important. But then you also have some options down here. So I'm gonna go to click on that, and I'm gonna make this just a little bit larger so we can look at these. So we have room settings you have persistent. Which basically means does the room stay changed after it is left by the player? This is important because we're gonna look at that we're not going to use in the game we're gonna make. But if you want to make a persistent game like an RPG where the towns people are moving around our maybe a farming simulator where you're cutting down grass and you wanted to stay cut down, persistent is going to be important. Then you also have the width and the height. Now, these air directly related to the actual width and height of the room. So if I change this to 600 by 600 you can see that changes right there. You'll have creation code, instance, creation order, which are important. Then you also have things for view ports and cameras, and you have room physics. We're gonna get into a lot of these options for rooms and a lot of these settings. But before we can start doing anything very interesting, we need to get in tow objects we need to get to making those. And that is what we're gonna do in our next video 14. 6.1 Getting Ready To Code: on screen right now is a portion of the game that we are going to coat together throughout the rest of this Siri's. Now it looks intimidating. If you've never seen any code before, if you don't know how to program at all, I understand that it can be very intimidating to think about that, to think about everything you have to learn because I'm sure you've heard that. It's complicated and vast and it's difficult. It's like learning a new language, and all of those things can be true. But they can also not be true. It all depends on the teacher and your mindset, and my goal is your teacher is to introduce this to you in a way that is natural and easy and will allow you to experiment every step along the way that you will. By the end of this course, know what all of this is. No, what every single line of code is inside of our game. Be able to tweak it to your settings and be able to make a game on your own without any help. No, it's a big goal and we're going to start small. We're going to start with just one line of code inside of our next video. It's gonna be simple. It's gonna be easy. But you're going to see that you can start doing things without having to know everything all at once. And I am gonna be here. I'm gonna be teaching you as we go, and I'm gonna be helping you along the way. If you run into any questions, any problems, feel free to reach out. Ask questions here on you. To me. Others will probably have encountered the exact same problem you have. And we can help each other out in this area. And I talked about in a previous video. But the manual the manual is a huge help. It is a little bit intimidating to look at its just walls and walls of text. But if you run into a very specific problem with a line of code, you can search for that line of code or you can come in here and you can middle click on specific line of code, and it will take you directly to what that is supposed to do and how it is supposed to work . I'm gonna teach you how to read this How to use this? That way, when you want to do something in your game, you can go in and say, Okay, I want to make an object. Appear over here with this much health, and you'll have a good idea of how to do that based on the manual that you will know how to read. So I hope that is exciting. I hope that learning to program you will be excited to do that will make it a lot easier. And the last tip I have for you a mental mind hack is to know that you can do this. Coding can be difficult and it ISS. When I first started, it was a difficult process for me. But if you believe that you can accomplish this, if you believe that in a couple of videos you'll be able to start coding information like this to start getting your objects to do things, it will increase your confidence. But it will also change the way you view it. If you think it's impossible while for you, it just might be, you might create a mental block that you cannot get past, but if you believe you can do it that you can learn it than that mental block will disappear. And little by little, you will become a programmer, games coder, someone who can create this all on your own. So that's my encouragement. That's my tips for you. In the next video, we're going to write our first line of code, and hopefully you'll see that it is not that hard. And especially inside of gamemaker studio. It is actually very easy and very readable, so with that, let's dive into it. 15. 6.1 Your First Code: Alright, it's time to begin coding Now we're going to start out nice and easy where it's gonna do one line of code for this entire video. But where we put that line of code is going to be extremely powerful. So let's dive into that now. So we're gonna open up our O B J smiley face and you remember the events. The events are very important because they tell the object when to trigger its code. So to start out, what we're gonna do is we're gonna put some code inside of our step event. So open up, step and do step. There's begin and end and just step. We want just the regular step. Now I'm throwing around the word step because that's what's actually here. But you may not be familiar with it. Instead, think about it in terms of frame. So this is going to run every frame of our game. And if you remember back in the beginning, we actually set our frames to be 60 frames per second, so that's going to be 60 steps per second. So this line of code is going to run that many times, and you may be thinking, What do we need to be checking 60 times a second? Well, there's actually a lot of different things, such as collisions and movement and health. The other things that when they reach the right circumstances, they need to be acted upon immediately. Otherwise it's gonna look weird to the player. Otherwise, there might be a glitch in the code if it's not acted upon as quickly as possible. Now let's go back to our room really quick. Let's set it to be the correct size of 10 24 and 7 68 That's just the default size. We're gonna put this object at the bottom middle of our room. We're gonna come back into our object. I'm going to minimize the room editor in our step of it. This is what we're gonna right? We're gonna right? Why equals why? Plus five semi colon. It's grand. Just copy along with that and just right exactly what I've got here. And I'll explain it as we go along. So why you can see it turned green. Now if I write hello, you can see that does not turn green. It actually turns blue eventually, but everything that is green means that it is a built in property of the object. So there are a lot of things that gamemaker itself stores for you, things that you don't have to keep record of things like it's X and y position. And that refers to the room position. Because inside of our room, you can think of this as a grid. You can see these numbers over here. This is the X and the Y. It starts at 00 in the top left corner and goes X increases as we go across. And then why increases as we go down? So our guy has a specific X and Y position within this room, and what we want to do is actually move him a little bit every single frame of the game. Now, what we've got right now is we're actually telling it, Take its Y position. We're gonna set its Y position to equal. It's why. Position plus five. Now, when we do this, what's actually gonna happen is this guy. His wife position is gonna increase by five every frame of the game, and if we increase it, he's actually gonna go right down off the screen. So let's go ahead and move him up here and I'm gonna press F five and run this and you should run it to to see what happens. He's gonna move, and he's gonna disappear off the screen with that one line of code. We've now told him to move every frame. And the way this works is you are giving on the left hand side. You're saying take this information and a sign it over here. That's what this equal sign does, right? Just like in math. Now, we're not gonna be doing a lot of math. This is pretty much the most advanced math we're gonna do with Maybe a few multiplication is here and there. But you do not need to know math in order to program. That is a very common myth to do our entire game. You will need to do about this level of math. That's it. What I want to do next is take this line of code press control X to cut it. We're gonna add another event. We're gonna add a key down. We're gonna actually press down on that. I'm gonna paste this in. We're going to do is I'm gonna run this. This is going to show you that events are very important. Okay, so now we have our smiley face, but he's not moving because it's not in the step event. It's only gonna trigger when I press down on the arrow key. So if I hold down, he moves my present. Once he moves a little bit, hold it down. He goes longer and you can begin to see hope. How you gonna do movement, right. You are adjusting their Why coordinates when you're pressing down. Now, if we added mawr key downs like left and right and up and all we have to do is adjust their why and their exposition based on the event of a key press, that code will then run. This is one line of code and it's already moving our character across the room. We already are about 1/4 of the way done with our movement. This is awesome. This is one line of code. It's not fancy, and hopefully it makes sense If it doesn't, if you ever get confused, what we're going along give me a shout out. Ask questions here on you to me. I guarantee, if you are confused. Someone else taking this course, someone else learning to program along with you is just as confused. So shout out, asked a question, and I will do my best to respond, and others who have figured it out will help you as well. So that is the one line of code. That is all we're going to write right now. I do want to show you one thing, though, So if we take this, I'm going to copy it this time and put it inside of the step event. If we go into our options and we actually change this to, like, five frames per second press, okay, save the game and then we run it again. We're going to see that this moves so much slower because we've changed the frames per second. Now I'm not pressing in the keys. This is moving down, but this is going at five frames per cent. So you can kind of get the idea that having more frames per second is good right now and probably for the foreseeable future. 60 frames per second is where mainstream games are. Some were still 30. Some very popular games are still 30. But for the games that we make in here, you can probably put it at 60 and run them quite well on your machine, even if it's not a beefy computer, even if it's not top model brand new. So that is your first line of code. I hope that makes sense, if not, ask questions. All right, We're going to keep going. But I'm gonna go at a slow and easy pace, and we're just gonna learn how to code. We're gonna learn how to read code and write it so that you will be able to write and understand your entire game. By the time you're done with this course in the next video, we're gonna talk about variables, a great way to store data and actually change how quickly things happen based on that variable 16. 6.3 Variables: in this video, we're gonna look at variables. That's a big word. And it's one that gets thrown around a lot in code, but I want to break it down for you. Variable is simply a way of storing a piece of information in a name that makes sense to us . Now, most of the time variables get created inside of the create event. They can be made or adjusted anywhere in code, but a lot of times they need to be a big fancy word initialized when an object is created. So we're gonna do that inside of the create events to go ahead and add that Now we're gonna do this really easily at first, and then we're gonna start using variables a lot. They are super important. So if you don't get it, make sure you ask questions so variable can be any name you want. For the most part, there are a few names that it can't be, and I'm guessing you can figure out at least one of those. It would be why anything that turns green, you're not gonna be able to name of variable because it already is reserved. It's something that gamemaker studio says I only have a value for this name. You can't use it. And those are reserved words. Those are things that you can use but you can't change their value inherently. Like I can't go in and change. Why? To equal my name. Because why is inherently a number on a grid system? So let me show you how this works. If we right player speed, you can see here that it's gonna turn blue because blue is a variable. And if you remember, all of these colors can be changed inside of file preferences if you want to. So player speed is a thing that I just wrote, and right now it's nothing. It's an error. It actually says unnecessary expression players speed used as a statement. Okay, Error messages are notoriously difficult to understand. Basically, what that means is that we haven't done anything with this yet. So what we're gonna do is we're gonna use the equal sign. Remember what this does is it says it takes the information on the right hand side and puts it in whatever is on the left hand side. So we don't say player speed equals five. So now If we were to look at the value of player speed, we would literally see that it is five. Any place that we put this, we're going to get five. And where we want to put it is, actually, we're gonna go ahead and delete this step event. That's what I want to do is focused on this key down. We're gonna replace this fires with player speed. So we have this and this. This is where it is initialized, and this is where we're using it now. This is important because when you start using variables, you can start getting some weird errors when your game runs. So let me show you first. What happens if it works properly? So when I pressed down, our players gonna move exactly like he did before. Okay, Player speed right now is basically just a five anywhere we put it. Now, if I try to access this when it's spelled differently at all, it will still turn blue because it's not smart enough to know that it doesn't have this variable anywhere because it could in a roundabout sort of way. There's a lot of different ways that you can initialize variables in a lot of different places. So gamemaker studio says Okay, you want to use this, go ahead. But when the game runs in compiles and you attempt to use this, it's going to come up with an air. So right now there's no air because I haven't pressed down. But as soon as I pressed down, we're gonna get this code error, fatal error, and it's gonna come up and say the game has crashed. But we'll look at how to read these and more in depth. But the thing I want you to know is that variables and pretty much everything encoding is case sensitive. So make sure you use a naming convention that you are familiar with again. I use Camel case and we don't capitalize the first letter. But we capitalize. All of the words after that and gamemaker studio has built in, uh, code suggestion. So if I start typing P l. A. You can see here that it says variable and inserts it for me, which is super nice. Now you might be thinking, Why do I need to do this? What is the point of doing variables when I can just put five in there. Well, think about it this way. A variable construe ore data. And then you can change that data later on. Think about the players experience or their health, right? You can't just put a number 10 inside of there for player health because you need to be changing that it needs to be growing and shrinking as the player fights and levels up. And if you have variable, you can say player health equals this much, and then you can increase it, and then you can decrease that variable as it goes along. Variables are super important. They're used all the time. So once again, this right here is used to initialize it. This creates the variable for the very first time because it's actually in the create event . You are setting it here. You're saying take the value on the right and place it in the left and the value on the right is a five. So now this works just like five in another super important part of it is that you can do this and put this variable in all sorts of places, and then when you need to make a change, you just make one change instead of 10 of them. So why don't we start creating movements? I'm going to go in here good. Aqui down. I'm gonna press. Oh, let's go ahead and cop actually write this code in here. We're gonna do why equals on this is gonna be lying minus player speed. So now we have key down and keep up and you can see both of them are using player speed. So let's run this. And now our player could go up and can go down, which is great. And now we have this in two spots. So if any time we wanted to give him super speed, we just changed that player speed in one area and all of a sudden he could move super fast . But that's variables in a nutshell. They are simply a way to store data inside of a very easy to read name and then put that in place to then be used for the data inside of it. And then you can change that value later on at any time. No naming variables for one. You should use a good naming convention. Yes, but I would also highly recommend using descriptive names because they don't have to be right. I could say cows are great. And that is a variable, right? A copy and paste this in here and here and run it and it's gonna work just fine. Variable names are on Lee for us humans there for the coders who are writing it doesn't. The computer does not care what it's named. It doesn't care how you put it. If you put in lots of dashes and, you know, whatever. Okay, maybe not dashes. But that is a very willing but you're never gonna remember that. You're not gonna know what this is for your not gonna remember exactly how to spell it if it's like this, which is why I highly recommend you naming it something that makes sense. Make it something that is human, readable. Even if it's longer, you could do something like this, you know, stand for, like, player speed. But I would highly recommend taking the time to type out exactly what it is. That way, when you look at your code later on or someone else comes and look at your code, they know what that variable is supposed to be doing and configure out if it's in the right or the wrong spot in the next video, we're gonna be looking mawr in coding, But we're gonna be looking at making our code more interesting by using if logic 17. 6.4 If Logic: in this video we're gonna be looking at if logic Now I have some very crude drawings of clouds and a hamburger. Hopefully, this will make sense to you in a much larger picture sense. So if logic when we're coating it is just like decisions that we make every day, if it's raining, we should grab an umbrella. If it's not raining, we should leave the umbrella. That kind of decision making is how our game is going to work. Its gonna look at a value. So here the value would be raining, right? So in our game, we might have a weather system that says during this time of the day, start raining or start snowing and then our code would just say, if raining is true, if it is, our players should grab an umbrella. Over here, we have something that makes sense, right? If from hungry, we're gonna eat food Now, this whole thing of eat food well, there's a lot to that, isn't there. There is seeking out the food. There is checking to see if you have money. There's checking what kind of food you want. If you have variety at hand, this doesn't have to be simple. This could be a very complex thing to do if this resolves to true. But if this is false, if you're not hungry, maybe you're going to take a nap. Maybe you're going to go play a game. Maybe you're just gonna, you know, chill, not eat any more food. That kind of logic is what we're gonna be looking at inside of our code. It's going to be logic that is very similar and actually looks just like that, and it's gonna be very easy. So let's get started on that. We're gonna add a step event inside of the step event I'm actually gonna right, If you can see here that if is a keyword, that's what it says. It means that you can't name a variable if because it's already reserved, No good coating practice says to put open and close parentheses in here, and then you put open enclosed curly braces. So to do these, you do shift 90 And to do these, you do the keyboard keys to the right of the letter P. If you don't you shift. You get square brackets. If you use shift to get open and closed curly brackets. Now, inside of if we want to put a value and we're going to do something, like if X is, let's go with greater than 500. So this sign right here says if this value turns out to be larger than the value on my right, this code is going to trigger now inside of our room, we have our smiley face, and what I'm gonna do is I'm gonna put our smiley face on the left hand side, where his exposition is about 32 and then I'm going to see if exposition is greater than 500 players. Speed is going to equal zero. And because this is a step event right down here, I'm gonna say X equals X plus player speed. So remember, step event runs every single frame of her game. So now our object is going to move from here thus far over and as fast as he can. But as soon as he reaches the halfway point, he's going to stop moving because his exposition will then be greater than 500. Let's watch this inactions press F five and he stops right are if logic is now true. Before he reached this point, it was false. So he kept on moving. And now he's trying to move. But players speed is equal to zero, so it doesn't take him anywhere. This variable is zero because this if condition is true, this opens up a lot of power and a lot of freedom. Now, for us to start doing interesting things, you can imagine that you can put anything you want inside of here. You could say if player health is less than zero. So if the player is dead, do something, restart the game, tell them that they've died or if Boss Health was less than zero, then hand out loot. Give the player experience move on to the next level. These are all things that you check through an if statement press control Z to take us back to here. Now there's one more part of an if statement that's important. And just like in our example, in the beginning, you could have multiple checks, right? So I'm gonna see if else if and in here, I'm gonna write this just the way it should be. So it So if this is true, then do this But if this isn't true, what should it do? We can actually tell it exactly what it should be doing. It can say else if this is true, If X is greater than 250 Well, why don't we set player speed equal to to? So we started slowing down the player as it gets closer. Let's press f five and run that. What happens? Well, as soon as he reaches this point, he's going to slow down and then he'll stop Onley. One of these will ever trigger at a time. If you want them to trigger more than one of the time, then you could actually add just another if statement right here, then both of these would be checked. We'll work on that later. But this allows you to do if this is true, then trigger this code. If it isn't what we want to do this. But what happens if both of them are false? What if we want to have a case that catches everything? Well, there is something for that. It's called else if everything before this is false, if it doesn't return true, then this will happen. So what do we want to put in here? Well, what we put player speed equals 20. What will happen? Let's run it, Find out that's a pretty good way Too quick. Quickly. No, on your screen. Maybe you saw that. We run that one more time and look really closely. You zooms forward, slows down and then stops right, Because this isn't true. In the beginning, his ex is 32. This isn't true. In the beginning, his ex is 32 which means that this triggers and that means he moves very, very quickly. So that is, if else if and else powerful tools to start doing interesting things in our game. Hopefully that makes sense. If it doesn't ask questions, you are probably not the only one confused. In fact, I guarantee you are not the only one confused. So ask questions. Let's help each other out. This is super powerful, and we will be using this a lot 18. 6.5 Comments: before we get into more advanced programming techniques. I want to take a second to talk about the green English sentences that at the top of each code event that you have created, and they are called comments now the compiler, the engine that actually puts your game together is not going to care about these at all. These are just for you and anyone else that will be looking at your code to understand what's going on now. Specifically, these three slashes and description lets you alter what is over here so you can see that if we come over here and we say, Set up player and I press control us to save now this actually says Set up player. And that's what this keyword at description is for. Those are Jason comments, which were not really going to get into. But you can change that, and this is immensely helpful if you describe what every single event actually does over here and then two slashes produces a single line comment. Now I normally delete these ones because I don't find them that important. But when you're coating, it's a very good idea to be using those comments you can put them at the end of a line of code to explain what it does. So, uh, this one's pretty obvious. Right are variables called player speed, so it may not need to be explained. But if we went down into here, See, we have a comment like this. Once the player reaches 500 they'll stop. This is a comment that explains this line of code, and you might be thinking, Why do we need to explain what's going on? I coated this. I know what it does. But think about it for a second. When you work in a game, it gets larger and larger. There's gonna be hundreds and then thousands of lines of code. And even if you coated every single line, eventually you're not gonna remember what every single line does. It gets too vast, too complicated. But if you add comments in it, you'll know exactly what it does. And even when you worked on it so that you can go back and if it ever needs to be changed, it can be. And this is assuming you're working on the project completely alone. If you ever start coating with someone else, you guys will want to use comments all the time. That way you're on the same page about what this does. You can look at a section and just be like, OK, this comment tells me exactly what it does. I don't have to try to figure it out by looking at the code itself, because sometimes code could be a little confusing when it gets larger and more complex. But comments will help simplify that. The last important comment is the multi line comment, which looks like this. You have an asterisk and a slash You want to do a slash asterisks to start and then an asterisk slash to end. And this is a multi line common. So everything between is going to be commented out. This is a great way of leaving a much larger description. You can also leave comments in line. If you really wanted to like this, it would be kind of silly. It would look odd, but game make extra. You doesn't care. It's going to completely ignore this and just go on its merry way. So comments are a fantastic way of leaving notes for yourself and for anyone else is gonna look at your code because when you begin making the game, it can get large, and it can get difficult to remember what everything does, and comments will help you. As we're going along in programming, I will be leaving comments and I encourage you to leave them there yourself, especially if you're doing anything different than me. If you start adding in different at art assets or anything else, leave comments to know what you've changed. That way, if you run into any problems, you'll know where they might be at up next. We're gonna be looking at common errors and bugs you might run into now that we're doing a little more advanced programming. 19. 6.6 Reading Errors: while you're making your game, you are going to run into errors. You're going to have problems that arise because that's just how coding works. You don't get it right the first time every time. So it's important to understand how to read air messages and then how to find the solution on your own. Going into the Internet and asking people works. And it's not a bad idea when you get stuck. But if you know how to read air messages yourselves, you become much more self sufficient and can fix those problems on your own. So I'm gonna go over the most common errors that you're going to run into while you're making your game and following along in this course. So the 1st 1 is going to be a variable, not set before using it. And this air can actually come up a couple of different ways. So the 1st 1 is gonna be if you don't actually have that valuable initialized at all. So if we were to come in here and delete this, then we no longer have player speed as a variable, given a value. So if we try to access that anywhere else it's going to throw that error. So if we come into the steppe event, we can see here that we are accessing this player speed. Now this looks like it's actually gonna work because we're actually setting players speed to equal something. But if we take this part and we put this at the top, then this is going to give us an error because we're trained to access player speed before it actually has a value. So it's pressed F five and run that, and you see here that it gives us this code error. It tells us that something is wrong in the game, has crashed, But this cold air is very descriptive and useful. So if we look at this, it tells us that step event for objects. Smiley face. This is our object. This is our step event, and it also comes in here and tells us the line number. So Line three is causing the air. It does us the actual code that's on that line, and then this line is also very important. You have the variable objects smiley face dot player speed, and then it says, not set before reading it. These numbers over here, they aren't very important. They are gamemaker studio numbers for referencing objects and things like that. So don't worry about it too much. This right here is the important part. It's not set that can happen when you have not initialized variable. So if we know that we can come in here and we can say player speed is equal to five, then if we run the game, it should work now, except when it doesn't. Right here is another code air and this one's actually the same thing again. It's not set before reading it. We're trying to access player speed, but it's not right. And why isn't that right? Well, maybe you noticed it is not the exact same thing. Copy and paste this in here, you can see the difference. One is Capital s one is not gamemaker studio, and pretty much every single programming language you'll ever use is going to be case sensitive. So this is an entirely different variable that the capital s version. If we put in that capital s, then it's going to run just fine. Okay. Great. Another common error is going to be accessing data when that object doesn't exist in your room. So right now we only have one object. But if we were to add a second and we were too at a great event and give this player health is equal to 10. Now we have a second object and we have a data that we can access. So inside of the crate event, why don't we try to access that data now to access data inside of another object you have to use on object excess? Er, To do that, you actually have to type in the object name you can see. Here it comes up with object one and even auto fill will help you out here. So if we type in object is a little too to normal, let's change this name really quick. So let's say O b j test. Then if we type in o b j test, it comes up. So Otto Otto feels very useful. Three to seo BJ test dot And now we have access to everything that that object has. These were all the built in variables. But if I start typing player health now, it brings up both of these and you have to be careful player health is actually part of O B J test player. Speed is not. But if we say we want to increase the health of this player by 10 that's great, right? That's a nice thing to do, given more health. But we're gonna come up with an air because that object is not in our room yet. It doesn't exist. So this says, unable to find any instance for Object Index one name O B J Test. Now the object index number. That part isn't as important. It's gonna change as you have more objects in your game. But this air message unable to find any instance for it. That means that it doesn't exist. It's not in your game, and objects can come into your game and they can leave your game all the time. When we begin making our space shooter, we're gonna be destroying ships and creating ships left and right. So if we're trying to access a variable in a ship that no longer exists, you're gonna get an error like that and to fix that this way, it's really easy. We just put that object in, and now if we run it, it should work right? Not quite. There's one more air that you might run into that is really confusing at first. But let's go over so that you understand it and can fix it if you ever run into it. So it looks like everything is the same, and it actually is inside of our room, though this is where it can get kind of confusing. So if we go to instance creation order right over here, you can see that O. B. J Smiley faces made first and then oh, BJ test is created. Now that's a problem, because over J. Smiley Face is actually trying to access O B J test and give it another value, But it can't because it doesn't exist yet. So if we move over J test up, it will be created first, and then O. B J Smiley face can access its data and increase its health. That's a little more of an advanced air, but it comes with the same idea. In this instance, creation order could be a very important thing to know. If you ever have a game object, it needs to be created. The very top of this instance, creation order so that its data is in the right place at the right time. Hopefully, that's helpful. If you run into air messages that you just don't understand, or you get some really strange ones, give a shout out. Everyone who follows along with this, I can almost guarantee, is going to come across some sort of error message as they are working on it. That's just the nature of programming. Things don't always work the way you want the first time. But if we work together, if we help each other out and we are very careful in what we type, then you will create wonderful things. So in the next video, we're gonna be looking at functions, and they are the backbone of doing more advanced things in our game. 20. 6.7 Functions: We're gonna look at functions now, and I'm gonna be splitting this up into two parts. We're gonna look at functions that do something by themselves, and they're gonna look at functions that return of value that we can use. So to get started, let's talk about what a function is and why we would want to use it. You can think of a function as a chunk of code that has been broken apart and is being used for one specific purpose such as this right here if we wanted to create a function that we could call and this is all terminology that you don't need to know, But it's what I'll be using. So try to get used to it. If we wanted to create a function that we could call to set player speed, we could do that. It would look something like this set player speed. We pass it of value, and then it would automatically when this is called, set the player speed equal to five. Now, the code inside of this function is located elsewhere. It's not gonna be right here. It's gonna be somewhere else, and we don't need to know how it works necessarily. Unless we're the ones writing the function, we just need to know what it's going to do and then use that value. So a very common function that we're gonna be using looks like this show message. And the functions when you write them are going to turn this gold yellow color and they're all gonna look like this. They're gonna have an underscore with no capitals anywhere. And if you just type, show underscore, you'll start getting all of these functions that you can use down here as well. There are a lot of built in functions inside of gamemaker studio to in fact, that is what a game engine is when you get down to it. It is just a collection of functions that have been used and created for anyone to make a game with the value of an engine really lies in how useful those functions are and what you can get out of them. So this function right here is fairly self explanatory. It's going to show us a message now the message and it's actually going to show is the message that we tell it to show and the way we tell it to do that is by passing in an argument. So if you don't know what a function is supposed to do, you can actually middle click on it and it will bring up this help message. It will tell us exactly what it is you can see here has a description. It tells us what it does. It has Syntex now syntax just means how you should actually write it inside of gamemaker studio too. It has a list of arguments and that is a fancy word. Meaning what information? What data are you passing into toothy function that it needs to know And that is a string here str stands for string and then what? It returns and then an example of how to use that very useful information. So we need to pass them a string here. And you can also look right down here because this says show message and then str so it tells us we need to pass on the string. Well, what is the string? It is a sentence or a word. It's a collection of characters between quotes. So let's start it off with a quote and let's just say hello world. And the quote, This is now a string. It is not computer code. When we look at this when the compiler looks at, it's not going to say, Oh, is there variable called Hello World Somewhere I should be looking No, no, It's going to say this is a string literal. It's just these collections of letters and I should put them together just the way I was told. So if we pressed F five, we're going to see this message Pop up. Hello World. Okay, and this is something that specifically you should never use in a finished game. But you can use it here to see lots of different things. It's very useful and debugging. So this is a function. It's a very simple function, and it just does something for us. But what are some more useful functions that you might actually want in your game, while a very useful one is actually going to be instance, create an instance destroy, so you don't need to note in instances right now? Just know that if you call this function so right now we are on key down. All right, let's call instance destroy and we don't need to pass in anything to this we can. This is kind of confusing at first. This asterisk next to these arguments means that you can if you want. They are optional, but if we don't pass anything in, this is actually going to just destroy our player. So I have this on the key down. So let's see what happens if I press key down. Boom, He's gone. He's been destroyed. Now let's go to keep up and let's say instance, Create. Now we have two options here. We have create depth and create layer. We're gonna go with create layer and inside of here. We need to pass in a lot of different arguments, but they may not all make sense. Why don't we middle click on this and we can actually look at what those are. So this function is going to create a new instance at any point within the room and on the layer we specified. Okay, the syntax looks like this and those are described right here. So the X and the y, where that it will actually be created. Okay, that's that's simple enough. Let's give it a position of 32 by 32 and inside of here. You need to use commas to move between them and you can actually see right down here if my my cursor is right here, it's at 32. It's an X. If I move, it passed this comma. It says I'm not why? And then over here, I'm at layer I d or name. Okay, so what is layer I d? So that is where we're actually going to create the instance, and that could be found inside of our room. So if we go in here, we actually have layers, and we haven't instances layer and a background layer an inside of instances where we have all of our objects. So we need to create it here and this you can actually change If you ever wanted to weaken name. It objects a little bit easier to spell a little bit easier to remember. So it's gonna work space and we have to type in objects, but it needs to be in quotes. Will say objects is where we want to make it. And then o b j stands for object. What are we actually making? Well, let's make another smiley face and Then we put a semicolon at the end, and now it is a correct function with arguments that were passing in. Let's run this and take a look at it. Remember, it's on key up now. Be careful because key up is going to create a new object quite fast. It's going to make a lot of new objects, and then this objects up here these smiley faces. If you press key up, they have the exact same code. So we will actually make a lot of new smiley faces over and over and over. And if you hold that down, your game will probably start to crash or freeze because it's using so much memory to create all of these new objects. So just be careful with that. I'm going to close out before that happens. But this is a function that is very useful works going to use this all the time. Same with instance. Destroy. We need to bring objects into our game, and then we need to take them out, and this is how we're going to do it. These are functions that we can pass information and just like this, or we could just call them just the way they are, and they will do something that is, in essence, what a function is. And it provides us with so much more power to start doing things. And the next year we're gonna look at functions that give us information back, and with that, we'll be able to start putting it all together and start working on our game. 21. 6.8 Functions - Part 2: in this video, we're gonna be looking at functions that return data to us that then we can use inside of our game. In the last video, we looked at functions that didn't need to have a return value, so we had instance, destroy an instance create and as a side note instance create actually does return something, but we didn't need it. It does something all on its own. It makes a new object for us. We didn't need to catch its return value, But there are some that if we call the function and we don't catch its return value, it's basically wasted. It doesn't do anything if we don't save its value. So a really good example of this and a function that will actually be using a lot is going to be the random math functions. So if we type out random, we can see here that there are a lot of different random functions and one of them were actually going to use quite frequently. Is this I random range, but we'll start out with random. This is a function that just takes a number, and then it will return from zero to that number a random choice and it could be a decimal . It could be a whole number. It doesn't really matter. So if we passed in 100 right here, this is going to return to us a number between zero and 100. If we run the game, though, nothing is gonna happen. It doesn't do anything in and of itself because we need to catch the value you see here that it returns a reald number. And that just means that it's a number that no, that's a number. Riel is a type of number inside of gamemaker studio. And so it returns riel, but we have to catch it. So I want you to just imagine that this number right here, this function is actually returning just a number. And we can look at that through another function we looked at. So we say show message. We wrap this function inside of another, and I pressed F five. This is going to give us just a random number from 0 to 100. This is 80.37 So you can see here that we saved it. We caught that number. It returned something to us. And now we can do something with that. This is very useful for creating randomness in your game for just choosing a number out of the blue. And now we can do something with that. A very useful one that will be using quite often is gonna be I random range. So here, where is gonna pass in two numbers. So we're gonna say zero and 10 and this function is going to start at zero, and it's gonna choose a number between zero and 10. And the I part stands for integer, which means it's going to be a whole number. So it's gonna be 0123 all the way to 10. There'll be no decimals, which is useful because we don't want to have any of those floating point numbers any of those decimals hanging on when we're doing certain kinds of maths. But again, this is a function that returns something to us. If we don't catch it, it is completely wasted. All you have to do to catch it is put inside of a very and again, if we come down here, we were to show random number. It's going to give us never between zero and 10 which is six this time. Hopefully, this is making sense. Hopefully, this is starting to click. Functions can give us values back and it's not always numbers. I'm just showing you that because when we start working on our game will be using this. A lot will be using these I random ranges all over the place. But the functions can return to us a string. They can return to us a sentence. They can return to us an object they can return to us a sprite or a path or ah font. There are There is no limit on what a function can return to you. But if it is returning something, you need to catch it. If you don't save it somewhere, you might as well have not called in the first place. It is going to waste your computer cycles. It's going to be a complete waste of energy, and you'll get nothing from it. But remember, not all functions need to be caught. The way you know is you can click on it and the returns. If it returns in a it doesn't return anything. You don't have to worry about it. If it returns something chances are you'll want to catch it, not every single time in this specific function. We don't have to catch it because it's still doing something. But if we call Iran and range and we don't assign it to a variable or we don't save it somewhere, it's totally wasted. So functions can do something in and of themselves, and then they can return something or they can not. Knowing if they return is important, so make sure you look at that manual every time we use a function. I'll be telling you if we need to catch it or if we could just let it hang there and just do it, something this is important. We're gonna be using these all the time, and you don't need to know what all the functions do. There are thousands of functions inside of gamemaker studio to all you need to know is that if you want to do something, go to help and search for find what's there, look for the function or just start typing. If you need a random number, type random, see what comes up. Click on it middle Click on that function and see what it does. That is the best way to learn in my opinion, so functions are great. Now we have that. Let's put it all together in the next video to create something a little more useful and interesting. 22. 7.1 Creating Your First Game: alright, It's time to put everything we've learned into one project we're gonna make your first game is gonna be simple, but it could be fun and they get a little challenging. So we're gonna go to new project gamemaker language. I'm gonna call this your first game click save. And now we have a brand new project. And the reason I'm doing this is because I want to go over the things that you'll need to check every time you make a new project. So let's make this resource tree a little bit larger so we can see it a little bit easier. I'm gonna go into options under Main. We're gonna set game frame 60 frames per second, because by default, it sets it to 30 apply. And okay, now we're gonna make a new Sprite, right click on that created new Sprite. I'm gonna call this SPR circle. I mean, like, double click on this and then we're gonna make a circle. Now, if you want, you can make any sprite here. The game that we're gonna create is putting this sprite in the room, and it's going to change positions very quickly and we have to click on it. And every time we click on it, we get a point. And when we get our maximum points, we win. So I'm just gonna draw a circle because it's nice and simple, and I am not an artist. So we have that. Now this part's important. Make sure you go to middle center, set the origin properly. Okay, We have our sprite. Let's make a new object going to call this O B. J Circle. A sign it are Sprite. That way it actually shows up. And then before we forget, let's go ahead and put our object inside of our room so it doesn't really matter where it starts, but I'm just gonna put it right there, and we're gonna leave all of this stuff exactly the same. The room with height, all of these layers. We don't touch any of that right now. Let's go back into our workspaces and we're only gonna use a couple of events. The first thing we're gonna do is create, so increase the size so it's easier to see. And we need to do a couple of variables inside of here. So I'm gonna put in move speed. We're gonna put this equal to 120 and the move speed here is gonna be how long it stays in one spot. So it's gonna move around the screen, and right now it's going to stay in one spot for 120 frames or two seconds. Then we're also gonna put in a player score, which will start at zero. Now, we're gonna introduce one new thing here, and that is going to be alarms. So what we gonna add? Event. You go down to alarm, You can see here there are 12 options. We're gonna choose the 1st 1 and they're all blink. They all start as a blank here, and I'm gonna rename this as move the circle, delete this line from there and save it. Now, an alarm is basically a timer. You have, I'm sure used alarms in your everyday life, and you can think of them as exactly the same. Inside of Gamemaker studio. It is going to be a chunk of code that runs after the alarm reaches a certain number, which is going to be zero. So down here, we're gonna access that alarm in kind of a strange looking way, and we're gonna get into this later on. But for now, just copy exactly what we've got here. I'm gonna say alarm bracket, square bracket zero square bracket equals move. Speed. And this is the way that you access the alarm. If we were using alarm to, we would say alarm too. But we're using alarm zero. And just so that maybe it piques your curiosity. Alarms are actually stored as an array, and you access a raise through these square brackets. Now, you need to know the rays are usually to know that this is how you access the alarm. Now, inside of the alarm, what do we want to do? We actually want to move the circle to a new spot in the room. So we want a random number. So I'm gonna say X equals I random range. Now, we wanted to be from zero comma and we wanted to go all the way to the other side of the room. But if we open up our room, where is that? We have zero on the left, and the width of the room is 1000 24. Okay, so if we come back in here we could put 1024 and that would create the outset. The X to be 0 to 1 24 and that can work. There's nothing inherently wrong with that. But what if we change the room size? What if we made it 2000? Well, now all of a sudden, it's only going to appear in half of the room, so it's gonna be pretty easy. What if we made the room a lot smaller? Well, now it's not gonna appear in the room at all. It's gonna peer way out here, and we'll never be able to click on it. Press control Z inside of the box here, set it back to the way it waas. So what is the way that we can fix that? Now you may remember that game maker studios stores built in variables about everything about sprites, about objects and about rooms. Everything you see here can be altered through code, which gives you the programmer, an immense amount of power and a really nice built in variable that they have is actually called room with. This is going to give us the max width of whichever room we're in and it will always be correct because it is built into the room. So if we put this right here and then we go in and change our room sized later on to be larger or smaller, it won't matter. It will always pick the correct Max with, and then we're gonna do the same thing. For what? And you could imagine that we're gonna have a built in variable for this as well. We're gonna say room height. Remember, it goes X from left to right. And why, from top to down. And that's why we have That's where we have room with for X and room height for why and then inside of our alarm after it triggers, we need to set it to trigger again. So we're gonna set alarm zero equal to move speed. We're going to save it because we've actually got most of what we need done. The next thing we need to do is make it so we can click on our circle, and that is actually another event under mouse left down. Now you could put this under right down middle down, left press left released. Left down just means that as soon as that left mouse's goes down, it's going to trigger and it's going to work. So we're gonna put our code inside of here. I'm gonna delete this second line, and I'm going to say description would be Move will say we're gonna increase score and decrease the timer. So our code for this is gonna be very simple, but you'll see here how it works. We're gonna say move. Speed equals move speed minus 15. Now this number and even this number here you can certainly change any time you want. I played around with this a little bit, and it's actually going to get a little bit difficult to be able to win, but not too hard and that it becomes impossible. If you make this a 20 it's going to be very, very difficult to win. But if you like that kind of challenge, that might just be what you need. They were going to say players, score equals. Players score plus one. Now, typing that out gets a little annoying. I don't know if you've noticed, but we're repeating ourselves, and as a programmer we value laziness, and I know that sounds really bad, but the idea behind it is if we don't have to do something, we don't do it. And if we can do something more efficiently than we should do it that way. So let me show you a little trick here that might save you a lot of time. Later on, we can actually take this part out. We go and delete that and I'm actually going to put in plus equals now. This is shorthand for exactly what this is we're saying. Take the value on the left and add the value on the right and put it back inside of here. So whatever player score is, it's going to add one to it and then assigned that new value to players. Score if you like typing it out like this because it makes more sense to you. Certainly keep doing that. If you want to use this shorthand, do that, then a swell. This is the shorthand I'm going to use because it's going to save us a lot of time. And once you understand what it does, it becomes very readable as well. Okay, so now we have changed our move speed to be quicker. We have increased our players score. The last thing we have to do is make it so. The alarm triggers immediately as soon as we left click on the circle, and the way to do that has to go to an alarm zero and set it equal to one. This means it's gonna shoot off one frame after this fires, and as soon as the alarm reach zero, the alarm triggers. So if we said it toe one, it's going to trigger immediately after this line of code runs. So let's go ahead and press F five and let's play our little game. If you get an error, try to figure it out yourself. If you don't know how to do that, then shout out because I'm sure someone else's encountering it as well. But on our screen here we have our little circle. It's moving around every two seconds. So now let's try clicking on and see what happens. So left click, and it moves right away, which is great, and now you can't really tell if it's going faster. So let's left click on it again. Still can't really tell because we haven't changed much. Let's click on it one more time, and now it appears to be moving faster. Let's try again. Okay, Now is definitely going quicker, So I'm gonna click on it again. Okay? All right. Now it z going fast. There we go. Now. It is really, really fast. So we actually clicked on it about six times to get it to be about this speed. And for me, at least, this speed is pretty much unclip Kable. Unless you get lucky, you're not gonna be able to click on that cause it's going so fast. So a game needs a way to end it. There needs to be eight objective inside of that game. So we need to put something in there that's at a step event. So call this trick, players score, and we're gonna see if players score is greater than six. This is kind of an arbitrary number, but when I was playing around with it, I noticed, After six clicks, it becomes so fast, it's almost impossible. So at six clicks, I would say you've won, so we're gonna say show message. You win. Okay, there we go. Now, if we run this again, that will pop up when our players score reaches that certain number, so I'm gonna click on this as fast as I can. Oh, it's moving now. There we go. I have one and it's gonna keep going because inside of the step event that runs every single frame, so that may not be the best place to put it. Unless, of course, we follow this with the new function called Game Restart and you can imagine what this does . It resets your game back to how it was in the beginning. So this right here, these four events, these dozen lines of code, this produces a fully playable game. We have a fun little game where we have to follow the circle around and click on it, and you can make this as hard or as easy as you want. And as soon as you reach that Max score, it resets, and then you can play it again. This is a full game. It has a beginning. It has a difficulty level. It has an objective. It has a way that you can win. It has no way that you can lose, but I think that's OK. If you want to make it harder, you can by adjusting these values right here. And that's your first game If this is the first game you've ever made Congratulations. This is This is awesome. This is taking everything we've looked at and a little more with alarms and putting it all together to make something fun. If you have any problems, if you run into something that you just can't figure out, shout out you are not the only one running into problems. I guarantee it. So with all of that being said, with all of this information now, inside of your head, you've got variables. You've got functions you've got. If logic all of that running through your head, we can now go on to the main project of this course and that is creating a top down arcade space shooter. It's going to have a lot more code. It's gonna have a lot more sprites and objects. It's gonna have multiple rooms. We're gonna be learning more about programming and how to put that together. But it's gonna be done a little bit of a time building on what you know, if you were able to make this game that you are gonna be able to make our next game as well . So with that being said, let's move on to the Final course project 23. 7.2 - Planning Our Next Game: No, - that is the game that we're going to be making next. It's gonna be a big project, much larger than what we just did. You can see here. There's quite a few objects. We have sprites sounds. We're gonna be using scripts and fonts. We're gonna be doing all of those things. But there's not gonna be anything in here that is new to what you are. What You know, All of this is going to be building on what you've already learned and just putting it into practice in a much larger scale. You are gonna understand all of this by the time we're done, because we're gonna go through this one step at a time, and I'm gonna reiterate how things are working, why we're putting code there. And by the end of this, you will have your own space shooter, which you will be able to alter whichever way you want. Put in your own sprites. Make it harder. Make it easier. Change up the level design. Whatever it is, you'll know how to do that to make the game exactly how you want. Or if this is just something you're learning, then you'll be able to take all of this information and put it into the game You really want to make because maybe not everyone loves space. Shooters don't know why they wouldn't, but a That's what we're gonna be working on. But first, I want to tell you a quote that I think is very funny, but also very true. It goes as follows. Weeks of programming can save you hours of planning. Did you catch that? Because it's opposite of what you'd expect. Weeks of programming conceive you hours of planning. Now it's funny, But the thing is that it's true, because when you start approaching a larger project like our game here, if you don't plan it out ahead of time, if you don't have an idea about what you want to make and how you want to make it, at least in a general sense you're going to spend a lot of time coding things that you don't need and possibly coating things you do need incorrectly. This game we're gonna talk about here, we're gonna look at what we're gonna want in it and then talk about how to make those things happen because planning out whatever project you're doing, especially when it comes to software creation, is vital to having a good and functioning product by the end of it. So let's take a minute and do that. So let us plan out our game now. We already know what it's gonna look like. The idea behind it is a kind of a retro arcade to D top down space shooter, where the enemies air flying at you and you need to mow them down to get to the boss to then move on to the next level. Perhaps you've played something like this in an arcade or and maybe even in the modern game , I don't see them very often. But I can think of several snacks games that I used to play that like this. Some of them go side to side instead of up and down. The idea is exactly the same, just like we were moving our smiley face, or we're gonna be doing of telling these ships to move down the screen. We're gonna be giving them a point so that they don't just go straight down. They're gonna be going sideways. But the idea is exactly the same. So some of the things that we're gonna need. We're gonna need a player ship, right? This is gonna be our main focus. So this guy needs to be able to shoot lasers. Uh, he's gonna have health. We need to implement some sort of live system. Unless you want to make this just a brutally difficult game where they have zero lives, what you could, uh, he also needs to movement, but he can't go on the whole screen because a lot of the times in the old games you can only go up maybe half the way of the screen. That way you can't actually run into the boss, so the movement needs to be, you know, tied down. It needs to be limited so that we control where he can move. We also don't want it to be able to go off the screen. That's important as well. And then we have enemies, right? We're gonna have a lot of enemies flying on the screen, so they need to be shooting at us. They need to have a health, whether that be one or five. They need to be ableto take shots and then die. We need to have them move from top to bottom. And then we're also gonna need Well, we need a boss for one that could kind of go in here. The boss is going to be a special kind of enemy, but he's gonna have the same kind of idea. He's gonna shoot. He's gonna have health, He's gonna move. Maybe not from top to bottom. He's gonna move around. We can set him up specifically for the game itself, though we need to have a score, right? We need a way to keep track of how many points the player has. We need to know when to bring out that boss. We need to have a start screen like we looked at a way to begin the game. We need to have a way to end the game. If the player either wins or the player dies, we need to have a way to go back to that start screen. So this is going to involve multiple levels. It's gonna involve multiple objects, maybe even a system object. To keep track of our score that we've got here, it's gonna involve, uh, sitting up health. It's gonna involve setting up a lot of different variables for all of these objects, but this gives us a good idea. It's a starting place for our game. We know that we wanna have our player ships at the bottom. The enemy is coming from the top, have a score system that when it is reached, the enemy boss comes out and he's gonna have special moves, things that Onley he does, like shaking the screen and shooting lots of different bullets all the ones. Now, if you're if you are thinking about doing a much larger game one with lots of different levels and dozens of enemy types and you have items and it's gonna be a big RPG or it's gonna be a platform or with dozens of levels that the player has to go through, you're gonna want to do a lot more game design thinking before you even begin programming. And that is an entirely different aspect. That's a video that could in the itself, take an entire course to talk about. There are things called game design documents where you actually write out down to the very last detail. What's your game is gonna look like we're not going to do that because our game is fairly simple, and we have a template to go off of, which helps a lot. But if you interested in planning at your game, I recommend looking up a game design document. Look up. The people who have made games and put out their resource is because they're out there. You can find out how the professionals plan out their game and then execute that vision to make it a reality. Weeks of coding can save you hours of planning. Let's not fall into that. Let's plan. Let's have an idea and let's make that idea come to life. So with that being said, we're going to begin working on our game. Little by little, we're gonna start with one object in one room with just a few lines of code. It's not gonna be overwhelming at first. We're gonna build it from the ground up. I'm going to give you all the sprites you need. I'm gonna link toe all of them. All the assets are going to be available for you all of the source code, which just means all of the code that we were looking at on the screen everything that you see here is going to be available for you to download right now. If you're someone who likes to tinker around with the code to see how it works, then you can download this, open up the project, and then look at it. Look at everything I've got here. I mean, I've got comments on things all over the place. You can look at this and know exactly what things do, but we're also gonna be doing this line by line bit by bit. We're gonna be building this from the ground up. So if you have questions, don't hesitate to ask if you get lost. Shout out, I guarantee. Like I said before, you are not the only one. Let's do this together as a team and make a great fun game. So let's get started. 24. 8.1 - Import Player Ship: okay, It's time to start working on our big project for this course to link up everything you've learned in tow. One game that's gonna be our space shooter. So in this video organisers, I'm going to go over. The resource is that you're gonna have available for you to follow along. We're going to get the player ship imported, and then we're going to start working on more things the next video. So I think a new project gamemaker language. I'm just gonna name this one space shooter and inside of here, you don't need to do much just yet is what we're gonna do is I'm gonna show you the resource is that you're gonna have available so you can download this on you. To me, it's gonna be a folder called Resource Is that will have all of the art and sound effects that you're gonna need to follow along in this course. Now, if you haven't downloaded already, I'm going to go over what's inside of it. So we're gonna have a background that I found That's gonna be the main background for the game. So this is looks like it's in space. Very pretty. All this is open to use. You do not have to attribute this art or the sound effects or music or whatever you definitely should. People that put this work into creating these for anyone deserve to be attributed. So I would encourage you to do that. We're gonna have this explosion spreadsheet, which looks really nice once it's actually in the game. They were gonna have a lot of stuff from a guy called Kenny. Now he puts out a ton of high quality free assets that you can search by just fine. But looking up Kenny and you'll find his art. It's very unique in his style. It looks a little bit cartoonish, I think. But it still looks very nice. So this is gonna be most of the art we're gonna be using inside of our game. We have a lot of different player ships of some bosses. We have some media is which we won't be using. We have some lasers which would get to there's enemies which will be pulling from as well. And there's got effects and damage and he has some you. I has got a lot of really great stuff, and we also have some backgrounds from him as well inside of here. And then we also have some music that we're gonna be using and some sound effects. And then this is the background for gaming studio right here. So let's go ahead and import the player ship, which we're going to be using. So I'm gonna right click on sprites. I'm gonna create a new sprite. I'm gonna click on Import now, Navigato, wherever you've downloaded those sprites at and then inside of there what we're gonna you're gonna go into Kenny space shooter going to go into PNG. I'm gonna open this up just a little bit, and I'm gonna be pulling in this one of these sprites right here so you can actually bring in multiple sprites if you want. But what I'm gonna do is I'm just gonna bring him with one green ship. Green's my favorite. I'm gonna click on. Yes, we want to bring that in. We're gonna middle center, this guy, and this is gonna be the sprite were using. If you want to follow along using a different sprite for the ship, you can There's just gonna be a couple of things that you'll need toe change as where we're gonna be firing the lasers from will be different depending on the ship that you chose. So I'm gonna name this guy SPR player, Ship Green, and we don't need to do much else besides that that gets our player ship inside of here. And then we're going to in the next video, create the object and work on the movement code for our player ship so that he could navigate around the world. 25. 8.2 - Player Movement: All right, Now, let's get our spaceship flying around the room. First we need to do is create an object going to right click on the object to create. I went in this O B J player ship and I'm gonna sign it are only Sprite that we have And I'm gonna add a creative int so inside of here I'm gonna label this create event, set up variables, and right now we just need one variable, and that's gonna be our fly speed. And we can set this fly speed equal to whatever number you want. But I found that 10 is a pretty good number. It lets you move around fairly quickly. And before we get to France going to our options, we're gonna make sure that our games per second this set to 60. If yours is not already, go ahead and change that. And then we need to add a couple more events inside of our spaceship. We're gonna move around by using the arrow keys. In the next video. I'm gonna show you how to assign those arrow keys also to other keys through a very simple function. But for now, let's just get the arrow keys up and running. What we want to do is add a couple events we're gonna do key down, left key down, right, and then key down, up and down as well. Now, the difference between these is that the key down is gonna register every single frame of the game that the key is being held down. So for movement that makes sense. If we want to move left, we hold down the left key and we'll move 10 pixels, every frame, But keep rest will only occur one time it is pressed and it won't trigger again until it is released and then press down again. So this means that if you put down key pressed, if you try to go left, you're only gonna move 10 pixels and you'll have to keep mashing that key to move a little bit at a time. That's not what you want here, but it might be useful for later on if you want to know how to just do key presses instead of key Daniel checks. Now, before we forget too much, let's go into our room here and let's just throw in our player ship right here at the bottom That way he's inside of our room Before we forget that I don't need to do anything else. So inside of our workspace, we're gonna go to the key Down left now inside of here I'm gonna rename this. I'm gonna say Fly left because that's what we want to do and to fly left. All we have to do is move our X over to this side and the ex corn that you can see down here in the bottom left corner changes it gets smaller as you approach the left side of the screen. So we need to subtract from our X and likewise for why it gets smaller as you approach the top and larger as we approach the bottom so inside of here to keep that in mind. And we want to say X r X coordinate is going to be subtracted and then set equal to fly speed. And you remember that this is the same as this right here. This is just a shorthand and much easier to type and agreed once you understand what it does. But these are the exact same thing. Now it's going to each of these other ones and change them as well. So this is going to be our flight up. And to fly up, we need to subtract are wide coordinate. I'm gonna say why minus equals fly speed and then to go right. We just need to say X plus equals Fly Street and then to go down. You probably already guessed it. We're gonna say why plus equals fly speed. If I press f five to compile and run this game, you'll see that now we can move around our level. Okay, I can go up, down. I can hold down, left and down at the same time to strafe all those directions so you can move anywhere you want around this level. Now the problem is that right now we can go anywhere, even outside of the game itself. That is not a very good idea. So in the next video, we're gonna look at restricting the player movement so that he can not disappear from the game and that you can only go up a certain amount in the level itself. 26. 8.3 - Restricting Player Movement: So what we're gonna do this video is tap into some of the built in properties that gamemaker studio holds to limit our players movement, no matter what size of room there in or where they're at in the game. So to do that first, let's talk about some of the built in variables that we're gonna be using. They are gonna be room with room height, sprite X offset and spray it. Why offset? So these are built in properties that if you middle click or you highlight and press F one on a laptop, you can go in and you can look at exactly what they are. And inside of you can read the description like the room with is holds the current width of the room in pixels so you can change us at any time. But you can also get the value any time you want, and then you also have the offsets. Now, the X and Y offsets are very similar, but you could just imagine that they are the offset from the origin that we set for the Sprite. So what that means is, if I double click on the spread here, you can see here that the origin is 49 37. So from this spot right here, to the top of the screen is going to be the Y offset. And then the middle point to the right or the left is going to be the X offset. This is going to be important because when we approach the side of the screen, we don't want our player ship to disappear outside of it at all. So we're gonna calculate exactly how many pixels are from the middle to the left or the right and say if they get this close, stop them from moving. It'll look a lot more natural than if we just tried to say, Let them move this far out. We'll be halfway off the screen. That would just look very strange. So instead, we're gonna use these built in properties that gamemaker has, and they're very useful. And now you know about. So let's start by using them. So first we're going to say the fly left. So we're gonna put this inside of an if statement because we want to say if we get close to the left hand side of the screen, we want to stop moving. And how close are we going to get? Well, we're going to say if our X coordinate minus are Sprite X offset is greater than zero that we're gonna be allowed to move. So this is saying if we get close to the side of the screen and suddenly this is not greater than zero, it's not going to work. So if I pressed F five to run this just the left hand side of the screen, we're not going to be able to go off of it now. So let's try that. Let's move over to the leftist. There's just loads up and bam! I cannot go off the side of the screen so I can still go off the right side. So we need to fix the right up and down next. So first, let's go to the right because it is very similar. So we're gonna say if X plus and you can see this pluses, they line up like that. So if we're going to the right when you to add our value are Sprite X offset and we need to check if it is going to be less than the room with. So if this is true, continue moving right. If it's not true, don't move right anymore. By using these built in variables, we can change our player ship. We can change our room size and they will still always work. So let's try that. Okay, We can't go off the right and we can't go off the left and I'll show you that even if we change things, we still could do that. So if I come in here and change their room with 12 lady in the height to 7 20 you can see now that we've changed the room size. But it won't matter because the variables that we are accessing are being updated all the time by gamemaker studio toe. Always be correct for the room that we're in and the sprite that we're using so we can change the room size. It doesn't matter at all. We still cannot go off the screen. This is called generalizing. What you're using It is always a good idea to use as generic properties as possible. That's what using these variables. Therefore, that's what using these built in properties does for us. We can change our sprite at any time, and as long as it's still the middle center, this is going to work just fine. Now it's do the down because it's very similar to what we've just done. We're going to say if our white co ordinate plus Sprite, why offset makes you use the white offset is less than our room height, then we can move down. Otherwise, don't allow it to Now the tricky one is going to be this flying up here. We need to decide. At what point do we want the player ship to not be able to fly up anymore? In most space shooters, you're stuck at about half for a little bit below half of the screen height. That way, you can't like fly up. You can't hit in a lot of the ships until they come down closer to you. So the way we're going to do that is we're going to say, if why is greater than room height divided by two? Then you could do this, and this seems a little confusing at first, but let's go into a room and look at this on dfid your it out. So we're going to say if why So are y coordinate right here. So if you look at this are white coordinate is about 500. So if our white coordinate is greater than our room height divided by two. So room height is all the way down here, 720. And divide that by two. You get about 3 60 So that's right about here. So we are attempting to move the player ship up and we can Onley move up if this player ship is inside of this zone right here, if it's greater than room height divided by two. So if I press f five to run this, you'll see that it works, even if your mind isn't wrapping the math quite exactly, the greater than is in this portion down here because we're attempting to go beyond that portion by going up, and we don't want to do that here. We can now not escape the room in any direction, and we can only go about halfway up. And that, of course, is a personal design decision. You can have them on the come up 1/4 of the way, or you can have them go up all the way. If you wanted to, but that will limit our player movement so that they have to stay inside of the game room while the game is running in the next video, I'm gonna show you how to translate the code. We just put on our movement here onto other keys. It say, if you wanted to use the w A s D, then you could do that with just a simple function for each translation. 27. 8.4 Key Mapping: all right. Now what we're gonna do in this video is mapped the code that we have for our arrow keys, the other keys on the keyboard, as you might want to use them. So you might want to use W A S D to move instead of the arrow keys. And to do that instead of copying this code and putting it in a key down for a or something like that, we're just going to use a function that gamemaker studio has built in to map the code from one to another. The benefit of this is that you don't have to copy and paste coat, because when you do that, if you make a change to the movement of one of them, you then have to go and make the change to the other. And if you forget, there could be bugs or glitches or things just start going haywire. Copying and pasting code is assigned that you need to either be using a function or a script or it's just very bad practice. I would not recommend it very often. So with that being said, I'm going to go into the crate event of our player ship, and we just need to do one function here to map one key to another. So we're gonna do keyboard set map, and this function is going to take one key and apply the code to the other. Now, the way this works is we need to give it the first key that we're going to be mapping to. And when you map keys on a keyboard with gamemaker studio, you can't just say a or you can't just say a like this. It won't know what you're talking about. That's because the arrow keys that we've been using actually a little bit different. They are actually what are called virtual keys VK and the keys, like a through zero or whatever are actually special characters that gamemaker interprets. And the way that we interpret them correctly is by using another function called O R D, which stands for ordinary character. And we pass in the character we wanted to interpret to translate. And then it knows what happens when we press that. So if I type org and then inside of here in quotations, I type w. Then I close that out, put a comma. This knows that the w key is what I'm referring to here. So then if I pushed VK up so I'm going to translate the code that we have for VK up, which is right here into the w key. So I press f five and run this. You'll see now that by pressing w, the ship will move up. I can use the other air, accused him of other ways, but W will work right now. So all we have to do now is apply this to the other keys that we want to apply it to. So I'm actually gonna copy and paste, even though I just said you shouldn't There are sometimes when it's useful like this just make sure you change everything that needs to be changed. So a yes and down to d. And then we're gonna change the VK ups. So a is VK left s becomes VK down and d becomes VK right now. If I press at five to run this again, you'll see you that I can now use wst keys to move around completely. You can still use the arrow keys as well. But if you hold down like the left arrow key and a At the same time, you're not gonna be going twice as fast because only one of them will be triggering at a time. So that works out great. And this could be very useful in a lot of different circumstances. So this still function. I highly recommend you remember keyboard set map. You don't want a copy and paste code into different areas of your objects. It's normally bad practice, copying and pasting. This was just fine because we're going to change them immediately, and we need each function for each key. So with that being said, let's move on to the next video, where we're going to be getting our player ready to shoot lasers. 28. 8.5 - Player Lazers: All right, now, let's get our player ship firing some lasers. So what we need to do is bring in the spray that we're gonna be using for the laser. We're gonna name this SPR, play your laser, and we're gonna import that sprite. It's gonna be found inside of the resource is holder Kenny, space shooter, PNG, underneath lasers. And you can choose whatever color you want. You can even choose whichever shape you want. I'm going to be using Laser Green 03 So I want to import that. And now for this one, instead of being middle center, we're actually gonna put this one bottom center. The reason for that is when we spawned this laser we wanted to spawn right here. So when I say spawned X and Y, we actually want the laser to spawn right there. That way it looks much more natural as it's being shot. It's a lot easier to say spawn it directly on the bottom of the laser instead of having to take into account the middle of the laser because it's being spawned in a strange way. So we're gonna do that for the laser. We're gonna come into the objects and we're going to create that laser LBJ player laser. We're gonna sign it that Sprite and then inside of our player ship. What we're gonna do is we're going to add an event just like we've done before, But it's gonna be a key pressed, and this time it's gonna be a key press for the letter Z. Now, the reason we're doing Z is because gamemaker studio is a little bit funny when it comes to dealing with virtual keys and the space bar. So if you have multiple virtual keys which are like the arrow keys control shift Ault, if you're pressing multiples of them, the space bar is not going to be able to pick up at the same time. So we cannot use the space bar and the arrow keys like if you were strafing, you wouldn't be able to fire. Now, the way around this is to use the W A S D. But some people might prefer one method or the other cell. What we're going to dio is just like what we did here. So we're gonna set laser shot to space bar using the keyboard set map function. This way we can have the best of both worlds. People can use the arrow keys, the wst keys, Z or the space bar in any combination. And for the most part, it's gonna work just fine. So we're gonna map the, uh we're gonna map Z key to the space bar, which means that we need to save Ikea space here. And then the Z key is gonna be with org quotes Z and then that will map all of the code in the Z to space bar, which is perfect. We're gonna name this Fire player laser and then inside of here. What we want to do is spawn the laser when we press that. So to do that, we need to use a function called instance create later. And this function will dynamically just means while the game is running, create this object for us exactly where we tell it to. So we're gonna tell it to create at the X and the Y coordinates of our ship, and then we're gonna give it a layer I d or name Now that is found in the room. You see, the layers are right here by default. You haven't instances and a background. Now you can rename these Ah a A if you want to, dio But you can also add multiple layers. That's why you have to specify whichever layer it's going on now for simplicity sake. What I'm gonna do is I'm actually going to say layer, just layer right there. This actually is the i d of the layer that the object are player ship is currently on. So inside of this room are our ship is in these this instances layer and you can tell because this layer properties is right here for added another one. You can see there's no ships inside of here. There are only inside of this instances and by saying layer right here, we're just saying spawn it on. The layer of this ship is on that way. If you ever make more layers or if you change their names, you won't have any problems. Now the object we want to spawn is the O B. J player laser. So I'm gonna press f five and will be able to spawn a laser with Z or space bar while we're moving. Okay, It works space. Barzee both work, but they're not doing anything. They're not going anywhere. And that's because we haven't put any code inside of that laser yet. So it's open up the laser, Let's go to step event. And all we need this to do is to fly upwards. So we're gonna say why minus equals, How much do we want to put in there? Well, good coating practice says we want to put a variable there, so I'm gonna create a create event and set up variables. We're gonna say fly speed is equal to 10 and this is a really good thing to point out. Inside of these objects, you can have variables that are named the same as other variables in other objects. So in our player ship, we have something called fly speed. But the game knows that this variable belongs to this object. And then this variable belongs to this object so they can have the exact same names, because sometimes the descriptive names are going to be exactly the same. And that's okay. Fly speed makes sense logically for both the ship and the laser, and as long as you remember what their names and where they are, you'll be just fine. So inside the step event, let's say why minus equals fly speed. It's run this one more time. And now we should have lasers that are shooting upward. Okay, that looks pretty good. That feels pretty natural. We can increase or decrease the speed as necessary. The one thing that I would say is that right now, if we're mashing Space Bar and Z, we can shoot an infinite stream of lasers. This is a design decision that you is the program or get to make, so you may do something different, but what I'm gonna do is implement something that says they can only shoot a limited amount . So I'm gonna come to the player ship. I'm gonna create a variable, and I'm gonna name this variable Max shots, and I'm gonna give this a value of five. And this is going to say the player can only have five lasers out at a time. This means that they have to think a little more because they can't just spam lasers indefinitely. So how to do that? We're gonna come in here and put an if statement We're going to see if instance number because, of course, is a function for almost everything you want to dio This function says we're gonna give it an object over J Player laser and it's going to give us back the number of the instances in the room. So this is going to return us a number. If I press f one, I opened up the manual on this one. The manual you'll see here that this you get how many active instances of the object air in the room and it returns a re A, which is a number. So with that information, we can say if this number is less than or equal to five, go ahead and make a new laser. Otherwise, you can't because we don't want them to be able to shoot lasers forever. So I'm gonna press and you can see here that Look now we got a problem. I can't shoot more lasers. I only shot five, but now the lasers are gone and I can't shoot anymore. So this is another thing that game maker studio has a built in property that we can take advantage of, So this is gonna be the outside of boundary event, so we're gonna add an event inside of our O B J player laser, it's gonna be underneath other. It's gonna be called outside room, and we're just going to say, Destroy yourself. The way this event works is it knows inside of a room, if it is in the coordinates, their designated as a room. So sometimes the lasers air going to fly out because they miss an enemy. And we don't want those lasers to go on in. Definitely. Instead, as soon as they're outside of the room completely, we want them to self destruct so that that instance number function will come back and say , Oh, yeah, there's only four of the now so you can shoot another. So to do that, we just have to destroy the laser. And that is a function as well called instance destroy. And this calls itself as soon as it knows it's outside of the boundary. So I pressed F 51 last time, and now I can shoot lasers so it looks like I'm shooting a lot, but I'm still only having five on the screen at a time, so that gives us a finite amount of lasers. The lasers are objects in and of themselves that we're creating dynamically, and they are doing their own code and managing themselves when they get out of the room. This is really powerful because we're gonna be using this on the enemy spaceships as well as we want them to be flying down the screen, shooting at us. But then, if we don't kill them, we want them to self destruct as well, so that more could be spawned at the top of the screen. So this right here, if you're able to follow along and understand what's going on, you have a huge chunk of the game ready to go. All we need to do is import the sprites, create the objects and just kind of duplicate the code for the enemies that we have inside of the the player ship in the player laser. But before we do that, we're gonna finish coating the player ship. We're gonna add in some sound effects and also some thruster effects to make him look cooler. 29. 8.6 - Lazer Sounds: Okay, Now let's add in some sound effects to our game. Adding sound is actually incredibly similar to everything we've done so far. We're gonna be using some functions were gonna be importing some assets just like everything we've done. So we're gonna right click on sound and we're gonna create a new sound and the prefix for sound. I'm gonna be using his S N D. You can use whatever you want to make sure you name everything similar so that you know how to find it and access it. So I'm gonna name s nd player laser. I'm gonna click on these three little dots to choose a sound file. Now, these are going to be inside of resource is Kenny Space shooter bonus. And you're gonna choose one of these two. I'm gonna choose the first laser sound effect, and I'm gonna click on this little play icon once it's loaded up so you can hear it there. And it's a little loud because you can imagine that this is going to be happening at least 2 to 3 times a second. So we want this to actually be down just a little bit and the volume that we actually adjust right here will be the volume it's played at inside of the game unless we change it through code. So all of this stuff down here, you don't need to mess with very much. The only one I would do is the output I would do stereo and the quality. You leave it 16 bit. Everything else could be the same. If you know the bit rate of the audio files you bring in, you can have them be higher. But 1 28 will be fine. Un compressed is okay. So now that we have that resource inside of here, we just have to call it whenever we press Z or Space bar. So let's go into that key press and inside of here we're gonna add in some curly braces because we need to have more than one line of code after our if statement. So we need to encapsulate all of that inside of here, and we're gonna say audio play sound. It's a function. The name makes sense. We're gonna pass in a sound i d which is gonna be the sound we imported. We're gonna pass in a priority and whether it loops. So the sound is gonna be sound player. And if we can see that comes up, which is very nice auto fill. And the priority means that if something is already playing, what is the priority of the sound I'm about to play is if it's higher than I'm gonna play over the other sound. If not, I'm gonna play under it. Sometimes this can mean cancelling out the other sound that's playing, but most of the time they're gonna jive together. For now, we're gonna do a priority of one. We don't need to worry about the priorities of the sound because we want them all to be playing at the same time, and then we want to put in whether it loops or not. And I'm going to say false. We don't want this laser toe loop. So with that, we can actually run the game with that one line of code. We can actually now get the sound effect, you know, really fine. And you can change the volume going into the sound resource. And if you think that's too loud, bring it down. I'm gonna bring it down just a smidge, and I'm gonna put that down to 0.5 just because it's gonna be playing a lot. And I don't want to get too loud. You can also choose where it starts if you wanted to. And you can also choose how long it plays through code. So if you had a long sound clip, you can actually play different parts of that sound clip dynamically through code. We're not gonna get into that. But that's something that you can dio if you're just to the manipulating, sounds even more. Okay. In the next video, we're gonna add thruster effects to our player ship. But then I think we've just about got him all set up. 30. 8.7 - Thrusters: Now what I want to do is draw some flames on the ship as we're going up and down. Now, this is going to be a little complicated as we're going to manually take over the draw event for our object. But it's gonna be very useful. Is it something that you'll probably end up doing in a lot of your games? This is going to be a little more complicated. And for this it's only going to be aesthetics. So if you're not able to follow along Exactly, that's okay. But it's still good to know what game maker is capable of and how the draw event works. So hopefully you'll be able to follow along. So what we want to do is right. Click and bring in a new Sprite. We're gonna call this SPR flames. I'm gonna import the sprite. Now it's gonna be in Resource is Kenny Space shooter P and G underneath effects. And I'm gonna use the fire. 03 But you can use whichever one you want now for this one. What I'm going to do is actually put it at the top center because we wanted to be drawn from this point where the flame is beginning. So once we have that done, we can exit out of it and we're gonna go into our player ship. And what we're gonna do is we're gonna draw the flame when we're going up and when we're going down. But the thing is that you can't draw anything unless you use something called the draw event. So we're gonna add that and then we have this new event here. We're gonna put a comment in here It says draws flames now, a very important thing to note. And one thing that you might get caught off guard is as soon as you add a draw event, you are telling gamemaker studio Okay, let me control the drawing for this object and everything else that I want to when it comes to this specific object. That means that gamemaker studio is no longer going to draw your sprite unless you tell it to so inside of our game. I no longer have my ship, but you can see it there. It's just not being drawn. So to fix that, the very first thing you need to put in almost every event for the draw is gonna be draw self. Once you have that, the ship will appear and it will act just like normal. Except now you can also add more things to be drawn. So there we go. Now, to draw the flames. We need to know if we are going up or going down. So to do that, we're gonna add a variable. This variable is gonna be called key down. It's just gonna be equal to zero at first. Now we're gonna go into our key down, up we're gonna add some curly braces because I need to add this inside of here. I'm gonna say key down equals on inside of quotations. I'm going to say up, not capitalize all lower case and inside of key down, I'm gonna do the same thing. Except now I'm going to say key down is equal to down. Okay, Now we have a variable that we can access that says I know exactly what I'm going up and when I'm going down. So then we want to go to the Dr Vint and we're going to say if key down equals, which is to double quotes to equal science. I equals up that we're going to do one thing else. If he down is equal to down, we're going to do another. So that allows us to then draw the flames, depending on which way we're going. So if we're trying to go up, that means that we want our flames to be below us. So to do that, we want to actually use some built in properties that gamemaker has. Were you doing that a lot? I'm sure you've noticed. We're gonna be using something called B box, which stands for Bounding Box. And the bounding box is there is a left top right and bottom. Now this stands for the top left the bottom left top right in the bottom, right in for the X and the Y coordinates of our sprite. So left in top are actually gonna refer to the top left right here. But the left is going to refer to the X coordinates. The top is gonna refer to the why coordinates. Same for the right and bottom. They're going to be along this access in this access here, but it's gonna be right for the X coordinates, a horizontal, and the bottom is gonna be for the Why coordinate now? There's going to be very useful because we're gonna be able to change our Sprite no matter where it's at inside of our room and draw these correctly. If we were to say, draw the flames at 200. Well, if the if the ship isn't there, we're not gonna be able to do that correctly. It's gonna draw them off where the ship isn't, which would not look any good. So instead, what we're gonna dio could use a function called Draw Sprite. And this is going to be for drawing a Sprite exactly where we want it. And the spread is going to be SPR flames. The sub image is going to be zero. We don't have more than one image inside of our flames. We don't have to worry about that. So we're just gonna draw zero for now, and the X coordinate is gonna be B box left. So we're gonna go to the very left of our Sprite, which is pretty much where I want to be drawing the flames coming out of these right here. So we're going to do to the left. So be box left and then we're going to say and the white corner is gonna be be box the bottom. So we want to go to the bottom. We're gonna say minus 10. And that's because inside of the player ship, remember, we want to draw them down here. So we want to go to the bottom of our ship, Sprite. Then we want to come up just a little bit. So we put them right about there so we can tweak those and play around with them as we need Teoh. But that don't get the left flame right about where we want it to. Now let's draw the right flame. It is going to be pretty much the exact same thing. B box, right for the X coordinate this time and it's gonna be same thing. Be boxing bottom minus 10. Now, let's go ahead and run that and take a look and see where the flames are and how they look . Okay, you can see them. They're right about there. They look pretty good. Um, I think we could move them in just a little bit, So why don't we go and do that? So, for the X coordinate, we want to bring them in just a little bit. So the left flame needs to move to the right, which is gonna be so eight plus five, the rate flaming cummings we're gonna say minus five and then let's run that and take a look. I think that looks pretty good. Currently, they're not disappearing. And that's because they are stuck because we're not changing the value of key down anywhere else. To do that, we're gonna add event he Ah, that's a key down. And this is gonna be no key. So as soon as no key has been pressed, we're going to say, uh, change key down. And we're just gonna set key down equal to zero. Now, if we run this, this will trigger Onley when we're holding down the up key. As soon as we let go, you can see that it's no longer on flame. Okay, that looks pretty good to me. Now we need to add the flames when we're going down to do that, it's gonna be a little more difficult because we need to draw the Sprite, but we need to draw reversed. Now. One thing we could do is coming here at an entire another Sprite and flip that or we could add another sprite inside of here and then draw that sprite. Both of those are valid and adding another sub image inside of here, it's probably actually a really good idea. Instead, I want to show you another function, one that allows you to manipulate the sprites that you draw dynamically. And I use that word a lot because it is very important were doing these things through code . They're not predetermined, which is great. Gives us a lot of freedom and flexibility. So what we're gonna do is we're gonna draw Sprite, and this is gonna be e X t, and that stands for extended, which gives us more options of how to draw. So the Sprite is gonna be the same SPR flames. The sub image is still zero the x and the y corn. It's gonna be similar. We're gonna say be box left will say, plus five for now, we'll keep that the same. The white co ordinate will be be box top and we're gonna say plus 25 this time because it needs to be down a little bit from the top of the Sprite and again I've messed with some of these values, so I know what they look like when you're doing it yourself, you'll have to play with them like I did originally. The ex scale means how large or small they wanted to be Compared to the original Sprite, the default value of one will keep it exactly the same. You could say I wanted half the size. You could say I want it twice the size of just the X scale. So, like stretching it out. We don't want to change that. We don't want to change the Y scale. What we want to change is the rotation. We want to put it 180 degrees on what it normally is that will give us a rotated sprite dynamically as the game is being run color because they see white, which stands for color white. And in this context specifically means don't change the default color. Alfa is going to be one because we don't want it to be transparent at all. Okay, Now, for sake of simplicity, I'm going to copy this and paste. And for the most part, these are gonna be exactly the same. Except be box right Hi. Let's run that and take a look and see how our flames now work. So if we go up our flames air there, If we go down, you can see our flames are right there. They're a little bit off. I don't think these ones need that plus and minus five. So why don't we go ahead and take those out? Run that and take a look at it. This is part of the fun of designing. So, Allah, that looks pretty good. I think we could move down the values here. Just a smidgen. Let's say 30 and 30 don't want to spend too much time on this, though I think this will look great. And hopefully you get the idea of drawing sprites to the screen. That way you can do something with them even more so that's really cool. So that gives us the flames for our ship now in the next section. What we're gonna do is we're going to set up our room that we are ready to start bringing in enemies for the most part. Are room is okay right now, but I want to go over a few things that are going to be fairly important, like the view port and the size and stuff like that, just so that the look and the feel of our game is consistent all the way through. That's what we're gonna be doing next. 31. 9.1 - Room Settings: Okay, Now we're gonna take a look at the room that we've got in our game. We're gonna make a couple of different rooms for the game overall, but we want all of them to have the same look in the same feel in the view port in the size that they represent. So to do that, what we're gonna do is we're gonna go into our room and I'm going to drag up these layer properties so that we can see a little more. So let's go over these settings really quickly. And then we already done it once, but we don't want to be persistent. We don't want these rooms to carry on. We want to keep the clear display buffer on now the width and the height, these air things that were going to change. So again, this could be a personal preference. But for me, I'd like to upscale my games that it is 1920 by 10 80. That means the actual room sizes that for the game itself, room science doesn't really matter. And it definitely doesn't determine the actual size on the screen itself. You could have a room that is 6 40 by 4 80 and still have it be 1920 by 10. 80 when you display it, it would look very odd. I wouldn't recommend it, but you could do that. I'm just doing this because it gives it a widescreen look, and it allows it to, in my opinion, have that kind of old timey space arcade look that I am going for. So I'm gonna put that science for the room, Then we're gonna go into view ports and cameras. Then we want to use the view ports. We need to enable those. We're only going to be using one view port. So we're gonna go into the camera properties here, and I want to change the camera properties to display the entire room at once. So I'm gonna change this to 1920 by 10. 80 and that gives us the entire room on side of here. Now, we also need to click visible, and then you can see that this white border comes up. And if we change the size of the room, that, of course, changes. The border itself becomes much smaller. But we want to have the entire room inside of here. Now we don't necessarily want to display it as 1920 by 10 80. That will take up the entire screen. And that's something that you can dio but for debugging purposes when you're building the game, I find that it's much more difficult if you're working at a full screen resolution because then you can't use debunker very well. And you also can't like, escape it very quickly. You have to, like, all tab out and then go close it. So for the most part, what I like to do is keep it as a smaller resolution. What we're gonna do for this one is due 12 80 by 7 20 That means the View port is going to show us 12 80 by 7 20 overall. And if we press F five to run this, we'll see that we're gonna look at the entire screen, which you can't tell very well, because it's all just a black background. But then the view port is just 12 80 by 7 20 so that gives us a good size room toe work with while not taking up the entire screen of the computer. Now, of course, you can change this anytime you want, and you can also enable a full screen option through code when the game begins. That's an option. That's something that you can do, and you can also have the enable maximize and minimize buttons. If you want to have those on your screen as well right now, we're just gonna keep it really simple. These are gonna be the properties that we use for all of the rooms in our game. That way they're all the same size. They're all the same ratio, and they all have the same look and feel because that's a thing that you want to make sure is consistent. If it's not, it makes your game feel a little wonky if we're going into different sized rooms and especially different size view ports. So I highly encourage you to pick one size and then stick with that for everything you do. Now, in the next video, we're going to add some background music. I'm gonna show you how to do that. I'm gonna show you how the creation code inside of your level works so you can get a good idea for that and no, in the future, it's something that you have access to 32. 9.2 Background Music: All right. So what we want to do now is at in some background music to make our game a little bit livelier. So what we're gonna do is we're gonna create a new sound, right? Click, create sound. I'm gonna name this s nd background music. Now, if you had more than one type of background music which for this game, we don't you would want to be much more specific in your naming, such as naming it by the in the name of the music itself. If you knew where it went or the level or just the timing of wind, the music would play that way you don't get too confused, but we just have one background music that we're gonna be playing. So it's not a big deal. They're going to go to to sound file. And inside of the resource is what we're gonna do is into the music folder. We're gonna choose this 1st 1 is space inside and we can listen to it from now. It's a little loud. So what I want to do is actually bring this down to about 0.35 And if we do that, play that again better, so that gives us the background music that we want. Now, when we actually want to play, it is inside of our room when it starts. And the best way, the easiest way to do that is by using the room creation code. And every room has creation code that you can tell it to do something unique for that level for that room, which is really cool. That option is found right over here underneath properties for creation code. And you could see it brings up a little window inside of here and weakens. Tell it to play music when this level loads simply by saying audio play sound. And we've seen this function before, so we're gonna pass in S and D background music with the priority of one. And this time we're gonna set loops equal to true. Then if I press f five and I play this, we're gonna have that background music load when the level starts. Okay, that's great. Now I want to show you one more thing. When it comes to audio that you can use probably very frequently. This is gonna be the function audio sound gain. This allows us to set, the individual gained the level the intensity of a music that is playing, and that is a very powerful tool to be able to use. Now the audio sound gained function asks for an index, and that's because when you actually start playing a sound, you get an index back. If we open up the manual, you can see here that it's plays a sound, but it returns an index, and that index is actually what is being played, this specific audio that's playing. So if you had multiple background music or multiple things happening that were the same sound, I d. You would actually be able to say, I want to target this specific one. If you were to save this I d. In the variable, such as background music or something like this, you could say background music is equal to this variable right here. Then you could pass in background music and you can set the level, said it 2.5, and the time is how quickly it adjusts. Now, if you're fading music out, this is very useful. You would say something like zero and then you would give it a time, and I believe it's milliseconds. Let's take a look here time change in milliseconds, so you would want it to be something like 5000 milliseconds would be one second. But for what we want to do is just said it to half the volume, and we want that to be instantaneous. So if I pressed F five now and I run this, you'll see that the music is actually a lot quieter than when we initially said it. And that allows us to manually just sounds that are playing in our game as long as we keep the index of them, which is very useful in handy to know. So that gives us the ability to change the music as it is being played, which is awesome. So that's all we need to do for right now. We're gonna add some more levels later on as we need them for a menu and accredits. But I think what we're gonna do next is dive into the enemy. We want to start creating some enemies, adding some ai so that we can actually start playing our game. So that's what we're gonna do next 33. 10.1 Enemy Ship Movement: all right, What we're gonna do next is get our enemy ship up and running so that it moves down the screen when it is in the level, and then we want to destroy it as soon as it's outside of the boundaries, just like with laser blasts. So to get started, we're gonna right click on sprites and create a new one. And for this, we're going to name it SPR enemy ship, and we're gonna import that Sprite. And that's gonna be inside of our resource is folder inside of Kenny, Space Shooter, assets inside of P and G inside of enemies. And you can choose any of these enemies that you want. I'm gonna choose just the 1st 1 That's what I've been working with for a while. So I'm used to that. Now we're gonna set the middle center right there, and that's all we need to do for that spring. Now we're gonna make a new object as usual. That's kind of the pattern that you follow. We're gonna do over J the enemy ship. We're gonna assign it that spray it. They were going to go into our room, and I'm gonna place a couple of these enemy ships inside of here right now, they're going to do nothing as we have not given them any code. But I want to put him in here that when I don't forget about it. Okay, let's open up the enemy ship. And now its added create event. We'll take this set Variables. Now we want to give the enemy ship a fly speed just like our player ship has. But we don't want them to be as quick as the player ship moves very fast. So I'm going to give this a fly speed. I'm gonna set this equal to seven, and you can change that value as you so desire. But that will give us a variable toe work with. And now let's go into the steppe event and let's add a step event that's just says lose this ship. And that's gonna be very simple in similar to what we've done before. Why plus equals fly speed. And if I pressed F five to run that we now have an enemy ship that will fly down the screen perfect. However, right now it's kind of boring. Right now, they just fly directly down the screen and there's no variety to their movement. So to fix that, we're actually going to do something by telling them to pick a spot on the bottom and then kind of move towards that spot randomly. So we're going to introduce a little bit of randomness to our game. This is important because randomness usually is a factor of chance. But it also means that your game is more interesting. If your game is completely predictable, sometimes that can be bad. There are, of course, times that it's good if you're making, like, a hard core game where the player is tested on their skills and they have to memorize the movements of the enemies. Repetition and memorization makes sense there. But if the ships are always coming down the exact same place, that's really boring to me, so we're not gonna do that. Instead, what we're gonna do is we're gonna use a function called Move towards Point and again, this is a very self explanatory function. It's going to choose a point and fly at that point now. The point we choose is going to be important, and this is where the randomness comes in. The X coordinate. It needs to be somewhere along the somewhere, along from zero to the height of the width of the room. So from 0 to 1920. And the way that we do that is actually through a function called say I random and the I stands for integer and then random means it's going to choose a random number. And the way that this function works specifically, it's gonna choose around a number from zero to the number you put in and I'm gonna put in room. What? Because our room with is 1920 and we want to number between zero in 1920. Now, the why is going to be broom hate? Because we wanted to go all the way to the bottom of the room and the speed that's gonna be fly speed. Okay, let's press f five and let's see if our ships now move in a little more interesting of a direction they do. But, boy, do they fly fast. And that's because we still have this step event, so they're always moving downward. Let's go and delete that. We don't actually need that because this function is gonna take care of that. So I'm gonna run it one more time and now they're gonna move in a much more unique and interesting pattern. So every ship that spawns in is going to call this function right here. It's gonna choose a different spot for that ship and fly towards it. That makes it a little more difficult to predict. And it also requires a player to actually be moving around to avoid getting hit by the enemy. So that makes it, in my opinion, a lot more fun. And now the last thing we're gonna do in this video is making so the ship is destroyed when it goes outside of boundaries. Otherwise it's going to go on forever. And that is done inside of other outside of room. And we can just say destroy itself. And all we have to do is put instance Destroy. Now we can't really watch this happen very easily, but we can run the debunker and we can watch the instances of the ships disappear. Ah, can't get to it quickly enough. That's all right. Inside of the instances, if we restart the game, you'll actually be able to see if we take global, You can actually see that there are several enemy ships right about there. And as soon as they disappear off the screen, they're getting destroyed. And there are no more enemy ships, which is exactly what we wanted. So that is great. That gets s a enemy ship moving down the screen in a fairly random way and destroys itself when it's off, which is great. In the next video, we're gonna set up enemy fire so that it is going to shoot lasers and that we can also have collisions with our player. In the next video, we're gonna set up lasers for the enemy to shoot and add sound effects so that they also have sound effects when they're shooting lasers. 34. 10.2 - Enemy Lazers: Now that we've got our enemy ship moving, let's get them firing lasers as well. First, we're gonna need a Sprite right click. Create a new sprite. Gonna call this SPR enemy laser. It's import something from here again Is going to be resource is Kenny P and G. And then it's gonna be any lasers you can choose any color you want, I'd recommend choosing red is that's the typical bad color I'm gonna choose Laser Red. 03 and import that. And on this one, we're gonna set it to the top center as Theo enemy ship is going to be firing mirror the bottom of it, as opposed to the player ship, which is firing close to the top. This top center will allow the Sprite to come in a little more naturally. Now, let's make an object for the laser O p. J enemy laser. And let's assign that sprite to it. Then we need to add a couple of events. So the first thing we're gonna add is a creed event. This is gonna have a fly speed on it. Now we want the speed of the laser to be moving pretty quickly. So we're gonna set this actually equal to 15 and then we're gonna add an event. It's gonna be other outside the room, and this is gonna be just like our enemy player laser. So we're gonna destroy itself and you do that through the function instance, destroy. And then the laser need to add one more event for the step, and this is going to be moving down the screen. So all this needs to do is say why Plus equal fly speed that will make us to the laser moves down the screen, destroys itself out of the boundary, which is what we need. Now you decide how we're going to fire when decide how often and we need to figure out exactly how that's gonna work inside of the player ship. So the best way, in my opinion, is by using an alarm. Remember, an alarm is like a countdown that when it reaches the countdown, it does something. So if we go into our alarms and we add a new alarm to alarm zero, we can name this fire lasers so inside of here. This will only happen every single time it counts down. It's only gonna run one time when it reaches the countdown so we can say instance, creates layer and we can say x y We'll give it just the layer i d that were already on And the name is gonna b o b j and emulation. And then we also want to play a sound. So is the audio play sound And we could use the player laser sound, But we actually have another sound effect. We can use this great sound say Snd enemy laser I'm going to browse and this is going to be inside of resource Is Kenny bonus miniatures Sound effects laser to I'm gonna play this. It doesn't seem that loud, But you also have to realize that we're gonna have about 15 ships on the screen at the time , and they're going to be firing one, maybe even two lasers in a second. And if we have any sound that is going to be even kind of loud happening that often, it's going to be way overbearing. So we need to bring this down a tremendous amount. I'm actually gonna put this at 0.1 now. It's a lot quieter, but it's still there. So when it plays weaken. Definitely. Still hear it, even if it's not that loud. So inside of our enemy ship, we're gonna play that sound sound and the means laser priority of one. And we do not want it toe loop. Now you decide when we're actually going to fire. So let's open this up to access an alarm to set its value. You say alarm square brackets and then the alarm number you're referring to. We want alarm zero. Well, say alarm zero equals 60. So that will mean that at the beginning of this ship when it's made is gonna set it's alarming call to zero, it will count down. So that's one number per frame. So 60 equals one second in the game time. Okay, so it fired, but it only fired once. And that's because inside of the alarm we have to set it equal to something new inside of here. So if we set this equal to 60 then every single time it runs, it'll reset itself. So it's gonna fire once every second. Okay, that's great. Now, inside of this game, I think a little bit of more randomness would be great. We don't want the lasers to fire just once a second. We want them to be firing kind of at a different interval per ship. And to do that, we can actually set up a random function so we can use a new function called I random. This is gonna be I random range. And the reason for this is that the i random select a number from zero to the number you choose. So if we just used I random, we could get zero. That would mean that the enemy ship would actually fire a laser almost instantaneously bright back to back. And that, I think, would be a little unfair. So instead, we're going to set a range to choose from. So we're gonna say, Ah, well, actually, this is a good time to add in some variables because any time you're putting in numbers or data you want to be able to put in variables That way you can change it, and you know exactly what they are. Much easier. So we're gonna put in fire rate low, so we need to have a low number open this equal to 20. We need to have a fire rate high. We'll set that equal to 60. And then inside this function, we just say fire rate low and fire rate high and that works out great because we can actually copy and go into our alarm, and we can paste that that way. It will always be a unique value for all of the ships that are ready. So if we press that five and run that now, they all shoot differently. That feels much more alive now. You may have noticed running your game a couple of times that your ships are always moving the exact same place, and there were always going to be firing at the exact same time. The reason for that is random functions, by default pull from the same seed, and that means that the way they're determined are the same every time your game starts, so they are random. But when you restart your game, they're random and exactly the same way they were last time, and at first that might not make much sense. Why would you want it to always be the same? If you're calling a random function well for debugging purposes, that is the easiest way to do it. If you're trying to test out a value that is always within the same limits or you're trying to play your game so that you get a good feel for it if it is different every single time. While it might be really difficult to figure out if it's working properly and my big difficult to get past that level for you. So instead gamemaker pulls from the same seed to fix that, you can actually just say randomize, and this will make it so that the seed is different every single time. You'd want to have this in your game in the end so that it is different every single time we can see here. If we run that again, the ships are gonna move differently, the gonna fire differently. But for our purposes, we don't want to have that in there just yet. At the end, we definitely will. But right now we don't want to have randomized in there because we want to make sure that it's working properly and that everything is going according to our plan. But that gets us our laser, our ship firing and moving down the screen, destroying itself with a sound effect. It looks and it sounds really good. So far up next, we're going to figure out how to do collisions. We're going to set up a health system for both our enemy and our player ship and get our lasers to collide with the other ship so that something actually happens. 35. 10.3 - Collisions: alright In this video, we're going to set up collisions among our lasers are ships and get those working properly . Now, the way collisions work in gamemaker studio, there's a couple of different ways. The 1st 1 is that you can actually add a event inside of here. That is a collision event with something so inside of my player ship. If I had a collision with the enemy laser, that means that's gonna be checking the actual collision mask inside of the Sprite. So if we go to our Sprite and if we open up the collision mask, which is right here, we can see what mask this Sprite has. And this is something that you can change as well. Right now it's set to automatic. But if I went to manual, I could say I only want the left wing of the ship toe have a collision mask. So the arrest of the sprite doesn't matter at all. Wouldn't make any sense. But you could do that if we change it back to automatic. We can see there a couple different types down here so you can add in ah lips. You can also go to precise which is what we're gonna put our player ship on. That way. This ship, the actual sprite spins the actual pixels in our ship will know when they get touched by something inside of gamemaker studio. Its precise and it says slow because it requires more processing power. So if you put this on a lot of objects, it's going to slow your game down quite a bit. So we're only gonna add it to our player ship inside of the enemy ship. We're not gonna worry about that too much. So we set up our collision mask here, which is great, But let's go back into our object and look at our collision. So inside of here, this is going to say, Am I actually touching that collision mask? You see, over here, there is an option for solid. We don't need tohave. That ticked solid is actually when it comes to other functions that you can use checking collisions inside of your game for this collision event. We don't actually need that. So we're going to say something inside of here like, uh, so we're gonna say take damage, but we don't have any damage to take yet. How do we do that? Well, let's go up to here and let's add in some health, we're gonna add in two variables here. The reason for that will be explained in the next couple of videos. But for now, just follow along. And trust me, we're going to set up Max. Health equals 2 10 and we're gonna set current health equal to Max Health. The reason for this is that we need to be able to display our health by having a max and a current. And if we are destroyed, we need to know what to bring our max health up to and by setting current healthy golden max self. You're always starting with the max amount, which is a good practice, in my opinion. Now we want to say current health minus equal one. Right now. Lasers Just do one damage if you wanted to make them do more damage to have multiple enemies, some that are stronger than others. Something you could do is created variable inside of here and say my damage equals two. And then inside of the player ship, you could say so. We're subtracting the amount of damage from the other object remember in a collision you have yourself and then you have the other object and other is a key word that you can access. You can say other, which means that we're then inside of our O B J enemy laser. And we can say other dot my damage because the laser that were colliding with has a very well called my damage. For simplicity's sake, we're not going to do that. But now you know how if you want to, I was gonna say minus equals one, and then we're going to say with other, and this is something that is extremely useful. We want to say, go into the other object that we are touching. The reason we're doing that is because we want to say instance, destroy. And this is a function that usually calls on the object you want to destroy. So now we're gonna be subtracting one from our health, and then we're going to destroy the laser that touched us. So let's run that and see if that's the case. Now we can't see the health, but you can see that the lasers disappear when they touch us. It's a little hard because they go through it. But you can play that again if you want. And see the lasers are being destroyed. Now, let's go in here and do the exact same thing for our enemy ship. That's at a collision event to the player Laser. We're going to say can't select stuff, just take damage. And we're going to say current health minus equals one. But we don't have a current health yet, so let's do that really quick, Max. Health equals will say to and current health equals Max health. Okay, so we're subtracting one, and then we want to say with the other instance Destroy. Now we've got collision set up for both the lasers. Let's see if I fire any what happens? Laser disappeared. E can't kill them yet because we don't have any logic that says if they have zero health be destroyed. But the collisions are there, and we are subtracting health from both the player and the enemy ship. And then when I also want to do is come into the player object and we're gonna add one more collision inside of here. We're gonna say collision with enemy ship, so take damage, destroy other ship this is something that you can do. And I'm just going to say I want to take one damage and you can add more damage here if you feel it's appropriate. So we're gonna take one damage and then with the other, which is going to be the enemy ship, we're actually going to destroy it. So this allows you to run into the ship's. If you want to to take them out. Let's try that. Who they disappear now? That's one way to destroy them. Right now, we don't have any way of shooting them down. And we also don't have any way of the player dying. So in the next video, what we're gonna do is set up destruction for the enemy ships. When we shoot them, we're actually gonna be able to track their health and control that logic. And then we're gonna work on the destruction of the player ship and making that look good 36. 10.4 - Destroying The Enemy: in this video. What we're gonna do is take a look at destroying the enemy ship and also changing its sprite into an explosion when it gets destroyed. So to do that, we're gonna be inside of the enemy ship. And what we're gonna look at is this collision event right here inside of the collusion event were already subtracting one health from our enemy ship. But now we need to see how much health we have left. So we're gonna do that subtraction, and then we're gonna put in an if stick remiss. Say, if the current health is less than or equal to zero, then we want to do something. We want to destroy the ship, but not just get rid of it like we did in the previous video. When we Clyde with our ship instead, we actually want to change the Sprite index to in explosion. And that explosion is gonna be something we bring in right now. So let's go to our sprites. Let's create a new one. We're gonna call this SPR explosion and go to import. It's gonna be in resource. Is Kenny space shooter? No, it's not. It's gonna be in resource is explosion art right here. So we're gonna bring this in, and it's gonna be an actual sprite sheet. So I'm gonna double click on this and go to image and go to convert two frames. So now what we're gonna do is change each one of these into one sub image of our Sprite. And by doing that, we can animate this in a really good, natural looking way. So this is gonna be four by four. So it's gonna be 16 frames altogether, and there are four frames per row, and the width is gonna be 64 by 64. So this is a really nice sprite sheet that is laid out in exactly the right size that we want. And if we click convert, it brings those in. If we say yes, it will bring those in. And if I press play, you'll see here that it repeats and it plays, and this is a really nice speed to play it as well. The speed that you change right here is the actual speed it will play at inside of the game unless you change that through code later on. So with that, we now have our sprite. One thing we need to do, though, is middle center and then come into the collision mask, something that we're going to run into if we don't change. This now is when we change the enemy ships Sprite to be this so that it looks like it's exploding after it got shot. It will still absorb bullets because it has this collision mask which we talked about in the last video. So something weaken Dio is actually change the tolerance, change it all the way up to 255. And essentially, this deletes the collision mask so it no longer will collide with anything else, which is going to be exactly what we want. So we can close out of that. Go into our enemy ship an inside of here once we have zero or less health. If two bullets somehow hit him at the exact same time, we're going to change our Sprite index and we're gonna set that equal to SPR explosion. So when our enemy ship reaches zero, it's going to change its Sprite index and it's gonna play the animation at the speed we set it to before because we haven't changed that anywhere else. Now we wanted to go away. Just be completely destroyed once it reaches the end of its animation. We wanted to not exist as an object anymore because even though we changed the Sprite and we have zero health were not telling it to actually be destroyed anywhere. So we're gonna add a new event and this is gonna be other. And this is gonna be an animation end. And this is a really handy event that you can look at and say at the end of this Sprite animation, do something. So I'm going to say destroy self at the end of explosion because our enemy ship doesn't have any more sprites. It's not being animated, but an animation end update will still think that this is an animation. It's just one frame of an animation, so we need to be very particular in our code. We need to come in here and say if sprite index is equal to SPR explosion, because the only time it's gonna be equal to that is when it already has zero health. So at the end of the explosion were going to say instance, destroy. And then if we come in here and we press f five. We're now going to be able to destroy enemies and have them turn into explosions. So let's check that out. That looks really cool. They keep moving. Their code continues to run, but they don't exist anymore. And we can verify that right now if we actually run the d bugger. But we can actually look and see that even the ships we destroy are actually getting destroyed when they change. So look right here that we've got old player ship. We have, oh, enemy ships and the lasers. And then all of the enemy ships disappeared. Even the one that we destroyed, which is perfect, because once we destroy it, we wanted to be taken out because later on, we need to know how many ships there are toe add more. Okay, so that looks really cool. That makes the enemy ship explode. It's a nice looking animation, and now we're actually doing something with the information that we have of their current health. So that works really great. And if we run it one more time, you can actually see that because the Sprite explosion index doesn't have a mask we can shoot. Wait. Can you shoot right through it? It's not gonna absorb the bullets like it would if it had a collision. So that looks really nice. In the next video, we're going to figure out how to get the player ship destroyed and also change its animation to be destroyed. 37. 10.5 - Player Death: So in the last video, we got the player destroying enemy ships in them, changing to an explosion, and it looks really great. But now we want to do the same thing for the player ship. The code is going to be fairly similar, but there's gonna be one difference. The lives of the player. Currently the player ship has no lives. And unless you're going for an extremely brutal game, I wouldn't recommend keeping it that way. So what we're gonna do is we're going to say my lives equal three. So we're gonna give the player some lives. But how do we actually code in lives for the player? Well, that's gonna be done inside of here. So we're gonna say if current health is less than or equal to zero, we're gonna change the Sprite index equal to s a PR explosion, just like we did before. And we're gonna do the same thing where we add the other event animation end. We're going to say update lives and position because we're gonna do a couple things here. First, we need to make sure that the animation and is the sprayed explosion. So if spread index is equal to SPR explosion. There were going to do several different things. The 1st 1 is going to be changing the position of our player. So we're gonna put them back to where they started in the room. And gamemaker studio has built in variables for that. They have next start and they have Why start? Which is really useful. So we're gonna put them right back to where the game started them In that level, we're going to say my lives minus equal one. So we're gonna reset the lives or move the lives down one and then inside of here will also need to check to say, if my lives is less than, let's say, less than zero. So we're actually giving them four lives and we'll come back to this. So right now, we're not gonna worry about that. But if lives are less than zero, then we'll deal with that in the future video. But that lets you see like how that would work. Now we also want to set current health equal to the max health. And we only want to do this if my lives are less than zero. So it's put this inside of an else statement because if we have no more lives left, we don't want to be resetting the health. And the other thing that we're going to do if we're going to keep playing in this level is we're actually going to say instance, destroy. But instead of destroying ourselves, we can actually pass in an I. D. You know what's really cool? Here's it has a little asterisk next to it. That means that it's an optional parameter. We don't have to give it anything, and if we don't, this will destroy our ship. But if we do give it something, it will actually go out there and destroy that thing. And what's really meat is we can pass in an I d of one object, and it will destroy all the objects in the room that matched that. So we're gonna destroy all the ships and all the enemy lasers, and the reason for that is we're gonna be destroyed. So we lose our lives and I want all the ships to disappear so that then later they can start coming down again, kind of like it's a re fresh of the level, but we'll still keep the score will still keep all of that data tracked. It's just that the player will restart here. Now, let's go into the damage here. The collision event of the enemy laser. And I'm gonna put this to 10 damage just so that we can see how this works because we don't have enough ships yet to take 10 damage if they're only doing one each. So let's jump up here and let's take a damage. So I turn into that. That's great. Ships disappear. I restarted May zero with position, but we're still that Sprite. Well, that's not what we want it all. So let's change that last thing. The Sprite Index is equal to SPR player ship Green, and that will reset everything that we want and put our ship right back to where it is now . I know we can't see the health yet and we can't see the lives we're gonna get to drawing those really soon. Um, we're not there yet, but don't worry. Drawing the interface, the heads up display. If you note by that, that will come in the future video. We're gonna tackle all of that right now. I just want to tackle the lives and the resetting of both the enemy ship, which we've done and now the player ship. I think that looks really great. I think that we are tracking the health even if we can't see it. And we're also tracking our lives, which is awesome. So in the next video, we're gonna be looking at how to dynamically spawn enemy ships so that we have more than just the four or six or 10 that we actually have placed ourselves in the room. Because I'm sure you've noticed that this isn't much of a game. They come down, they disappear when it's over. We need some way of creating these ships indefinitely as long as our goal has not been reached. And that's what we're going to tackle next 38. 11.1 - Dynamic Enemies: all right, Now it's time to start dynamically creating enemies for our level. This will make it so we can actually start playing the game instead of only having four ships fly down and then disappear forever. So to begin, we're actually going to delete all of these ships because we don't need any of them in our level. And they were going to make an object. This is going to be kind of more of a system object, one that we're not gonna sign a sprite to, and one that we're not actually going to see. But it's going to take care of the logic for us. I'm actually gonna name this over J Level one and then inside of our room. I'm going to place that in here before I forget, because if the objects not in the room, it won't do anything at all. Now let's go back to it. And let's add a great event. We have set up the variables now inside of here. What we want to do is actually, say, spawn enemies in this room until a certain point has been reached on. That point could be different, depending on the style and the difficulty of the game that you're trying to make. What I want to do is have a maximum number of enemies in the room at a time so that there is a good number in their but never too many to feel overwhelming. And the number that I came up with was 15. So what I'm going to say is, Max enemies equal 15 so there can't be more than 15 enemies on the screen of the time. More than that, it becomes too difficult to dodge. And it be gets a little unfair, in my opinion. But this is something that you can tweak as you see fit. Now, I do want to figure out how to spot enemies, and the way we're going to do that is once again with an alarm, they are extremely useful. So we're gonna create alarm event, and I'm going to say spawn an enemy if I can spell enemy correctly so inside of here, I'm gonna say instance, create layer just like we've done lots of times, and where we want to make it is actually gonna be at the top of our level. So it's gonna be up here and up here. The X coordinate is gonna be anywhere from about zero to the room. Winth. But the white coordinate It needs to be around right around here, about 0 to 50 or so. Because you have to remember that if they spawn outside of the room, they will instantly destroy themselves. So we can't have them respond outside of the room. We have to spawn them half inside the room. So to do that, I'm going to say choose an exposition. So I'm gonna say I, Brandon, and we'll say room with. So this is gonna be an exposition from zero all the way to the right side of the screen. I'm gonna increase this box that I can hopefully fit it all in there. Now we have the y position. So this is gonna be somewhere right around, Let's say 25. So we know exactly where it is. It's partially on its partially off. It looks like the spaceship is coming down from the top. Now, the layer is gonna be layer as we've done before, and the object will be over J the enemy ship. Okay, now this will create it. But we also need to reset the alarm. So that leads us to the decision of how fast should Adami spawn it? Well, let's go with a fairly fast spawned right. And with that being said, let's actually make the variables we're gonna say, Spahn, rate low. We're gonna do something just like we did with the fire rate of the enemy ship. So it was a spon rate low is equal to five and spawned rate hi is equal to, let's say, 20. So this is gonna be the time difference between when things are spawned. So I'm gonna say alarm zero is equal to I Random range spawned, rate low and spawn rate high. Okay, now let's go ahead and copy that portion and put that inside of the alarm as well. And with that, we should actually be able to run the game and see if enemies come in and fly down the screen. That's a that's a lot of enemies, and that's probably because we haven't actually said it, too. Have a maxim. So to do that. And by the way, I didn't change this damage back yet, So it's going to the O B J player ship. That collision event and turned that damage down to one. Otherwise, we die instantly. Okay, back to the object. So inside of here, we want to say, if instance, number that we've used this function before, so we're gonna count the enemy ships in the game. So if the count the number of enemy ships is less than 15 go ahead and make a new ship. But if it's not, don't make a new ship. But do reset the alarm so that if the ship leaves the level or gets the story by the player , you're ready to go again. So with that being said now, we should have only 15 ships on the screen time. It still looks like a good amount, but at least I am able to dodge this fairly well. And you can certainly check it out yourself so we could destroy 50 way, have 15 ships in the street. Okay, that looks really nice. Gives us a good amount to play with, and they're going to come down in. Definitely. They're never going to stop, which is really cool. It allows us to have enemies dynamically spawned forever based on one object and the reason that this is named O B J Level one is because I would recommend creating an object for each level that controls the logic for it. So this is level one. We're just spawning this one over E. J enemy ship. But if you wanted to have the next level spawned this ship and another one, then in that object you would put that logic so that it acts differently than other levels . And you can group those together and just spawned the levels as you enter the room through the room creation code if you wanted to do it that way. So this is a really good way of condensing your code exactly to where you want it and Onley using it where it needs to be used. Okay, with that being said, that gives us dynamically spawned enemies coming into a room at the amount we want and the rate that we want and that actually is everything that we need to do I originally planned is due to videos, but we covered in one which is awesome. So what we'll do in the next video? Because I'm going to go over the D bugger now. I've already shown you in a couple of times, and you may know how to run it, but you may not understand what its use is yet. It's an extremely powerful tool that, once you know how to use you, will be better than a lot of other. When I wouldn't say better, you're more prepared to use the tools at your disposal than most other programmers making games coating. In general, a lot of programmers are afraid of the D bugger because they don't understand it's purpose . But it is extremely powerful. And once you know what it is and how to use it, you'll be able to debug, find errors in your game, way easier and away faster, so let's jump into that next. 39. 12.1 - The Debugger: So we're gonna take a small break from coding our game to look at how to debug our code for our game. Because debugging is going to be an essential part of any programming you do ever. Nothing is going to work the very first time you coat it. If it's a large project, sure, a couple things, we're gonna work here and there. But once you begin building a huge game or creating a database or building a website, those are things that are going to encounter problems and bugs and errors. And that's why we use the D bugger. Now you can access to the bugger right up here by pressing this icon or by pressing F six, and if we run it, it's gonna look a little bit different. But the game is still going to come up and play just like it normally does. That's because the D bugger is meant to go in the background, and I'm gonna actually minimize the game. Press the pause button. Now the music is still playing, so I'll try to talk a little louder. Now The deep bugger is this window right here, actually, and it gives you a lot of information. You could see that there are some new tabs down at the bottom. Here you have variables which has locals Global's watch. You have instances, others and graphics. Some of these are very useful. You use them almost every time you use the debunker. Others, not so much Now, if we run our game again, it's gonna come up. But if we go deposit, you're gonna see here that it's pausing on a certain area and it tells you that it's the animation end tab inside of the over J enemy ship. That's because it's checking this line of code at the exact second. We paused the game, so that's very useful. These up here, these icons are F 11 for short Cuts of 10 and Shift Plus 11. This is to step into the function call, which is to go one line of the time. So if I press that it's going to go one line of code at a time, let's get a look at these and it's gonna do them as we need to, which is really cool. This allows you to basically slow down your game toe one line of code so you can see if code is running. And if it is where exactly it is. And if there's a problem, confined it right here Now then the F 10 is to go over an entire call. Now it's kind of similar. But if you had a large chunk like a big if statement that you were going into you could jump over the entire thing through this from the shift F 11 that kind of goes through the entire event. So if I click that it's gonna not even check inside of it is going to run that code still. But it's not gonna walk you through step by step. Now. You also have the restart key, which you compress to restart the game because obviously posit and you also have this enable real time debugging, which I'd recommend doing every single time you want to look at something, it gives you everything you need to be looking at all of the code for your game that is running in the background. So if we go back to variables, you can have something called global variables, which we haven't talked about yet. But if you enable the debugging, you can see all of those at once. And with the instances here, we can look at everything that is active inside of our game. Now, I know this is a lot of information to tackle once there's so much that you can see here. But what I want you to take away from this is that this T bugger allows you to one pause your game to step through every single line of code that you've written to see how it is executed when it is executed and the order of execution. And then you can also look at all of the instances that are in your game and all of the values for them. So if I open up o b J player ship, But I know it's kind of small, you can actually look at the fly speed variable max shots. But then you could do something really cool. And look at all the built in variables which we've talked about a lot. You have the I d. The x and Y position direction, the X start the wise start. You have the bee boxes, you have the Sprite index. You have all of this information that you can look at. You can look at that for every single object in your game. So if something that you create just doesn't seem to be appearing or doesn't seem to be working, you can run this D bugger. Take a look at it, see if it's there for the X and Y position is at. And if it's visible, there's something somehow that's been turned off. Now that's a lot to cover. In the next video, we'll talk about some very practical applications inside of D bugger. We're gonna look at that. Stop skating. No, stop the music. So in the next video, we're gonna look at a practical application of the debunker as well, so that we could look at how to set break points and what they are and why you'll probably end up using them a lot, even throughout this course. 40. 12.2 - Breakpoints: So in this video, I just want to go over what a break point is, how to use it and why you want to be using it. So a break point is a thing that you can put inside of your code, and it will pause the game when you run the debunker at that point, exactly until you all the information that it has surrounding that line of code and that objects that you're inside of. So if we went to our player ship and for some reason we were having a problem with it, like when we pressed Space bar, it wasn't firing. We just couldn't figure out why. Something that we could dio is come inside of here and I can press f nine or you can right click and you can say Add break point, which is right down here toggle break point. And then if I press F six to run the d bugger, it won't do anything when your game is running normally, break points on Lee work in debug mode. If I press space bar or a Z, it is then going to stop right here. It's gonna pause the game at that exact line of code, and then I could come into variables and I can look at the variables that are working, so I can also look at self and other, which we haven't talked about yet. But it's the variable. It's the object that is currently running this line of code, so you can see here by the variables that we have inside of it. It's my player ship, and I can look at exactly what it's doing. Then I can walk through and see. Okay, maybe I said, greater than or equal to five, then fire. So then I look at say Okay, I can see that I can figure that out and then I can make that change. Break points allow you to do that toe, have that fine tune control and look at every single thing that's going on. In that particular instance, it's very handy when you have larger games and you expect something to happen and it's not happening. You can put a break point on that piece of code and see why aren't you running what's wrong and then you can have that be answered inside of the debunker. Personally, I use break points all the time. They are quick, they are efficient, and they give you way more data than the obvious show message or show debug message. Inside of this thing event. You can put those and they help you out a little bit. But once you want to use the D bugger, you can just press F nine F six. The game will posit that exact instance, and you can see why it isn't working. It's not always obvious. It's not a magical fix. You will still encounter problems, I guarantee it. I still encounter problems on a daily basis, but using the debug er knowing what it could do, knowing how it works will help you be a better programmer in a better thinker. In general, utilizing the tools at your disposal is essential for doing the job the best you can. I hope that helps, and I encourage you to keep using the D bugger. I'm going to keep bringing it up as we go through this course that you get used to it. But don't forget it's there and continually use it when you have problems in the next section, though, we're going to be getting the gooey set up the graphical user interface or the heads up display or whatever you wanna call it. We're going to start putting things on the screen to tell the player the information at their disposal. 41. 13.1 Visualizing Player HP & Lives: it's finally time to get some graphics on the screen that are gonna work for Are you? I called a gooey a graphical user interface orgy You. If you want to pronounce it like that, we're gonna start by drawing the player health and the player lives onto the screen. That way we can have a visual indicator of how much health we currently have. Now, as usual, gamemaker studio hasn't function built in for this. It's a very useful function, but it's also one of the more confusing ones just because of its length. It has a lot of arguments that you need to pass in, and you also need to do a little bit of math inside of it. Fortunately, it has an example which, when I need to draw a health bar, I usually copy that example pasted in and just change what I need to change, because most of the time you don't actually want to memorize everything. You just want to know where the information is that. So with that being said, we're going to go into the draw event of the player, we're gonna add a little note here, and we're going to go down here and we're gonna draw health bar here. So the function rightly names is actually draw health bar. And there's, like, eight different arguments that we need to pass in here. So instead, I'm gonna open up the manual and we can look at this and see what each one does, and for the most part, they make sense. Um, it's a little more confusing because we've got x one y one x two. And why to we need to basically say the length of it and also the width of it, the thickness, so that we can completely design the health bar. However we like for the most part, you can just take this example, though it's very useful, and you just change what you want to change about it. Now the math part comes in right here, and this is why we have a current health and a max health, because we want to be able to display those in a way that the health bar will take down every time we lose one health. And you do that by dividing the current health by the Max health and multiplying that by 100 and I'll show you how to do that. We're gonna get this up and running, and it's not gonna take very long at all. So I'm actually gonna copy this and you can do the same thing. And we're just gonna change the values as we need, Teoh. So put this back here and paste. All right, so this is gonna take up a lot of room and one of the things that you can do with very large functions is actually you can enter a new line and nothing bad will happen because of it. Gamemaker studio will know that this is still the same function. This is still trying to do the same thing and all is good there. So you decided where we wanted the health bar. What I'm gonna do is put it in the top right corner of our room. That is a complete design decision on my part. You can put it anywhere you want. So for the top right part, I'm going to put it up here. And that means I want to actually use some built in variables. That way, if the room size changes, the health bar will still always be in the exact same place. So I'm going to say room with minus 100. And then for the why one this is going to be the the top part of the bar. And the difference between the Y one y two is how thick the bar is going to be. So the first y one I'm gonna put at 25 and then for the X two, I'm gonna say room with minus 25. That way, it's about 75 pixels long. And then for the y two, I'm gonna say 75 that way it's not too large now, right here. This PC, That was something that they had done. And you can create the variable rate before you call the function. If you want to do the math outside of it personally, I usually do the math inside because all it is is just the put in parentheses. Current health divided by Max Health Times 100. And if we do that math right there, the map that the health bar is gonna come out and it's gonna look really great. It's gonna take down every single time we take one damage. Now we have some options for design colors. You have the back color, which is going to be the actual fill of the health bar the whole time. Does that see Black? Right here. I'm actually gonna change that to B. C. Maroon because we have, ah, black background. Currently, it's hard to see if it's all black. Now, the minimum color is when it starts getting low. I'm gonna keep that see red, because that just makes sense. And lime, this is a kind of a lime green color. So we're gonna keep that right. Just do it. Is that because it looks pretty good now, direction means that you can actually change the direction of the health bar the way that it ticks down and you can change whether it's up or down, left to right stuff like that. I'm going to keep it just going from right the left that is simple and effective for me. I want to show the back color and I want to show the border. These are all options that you have. And this is also where you would do something called like overlaying your own health bar. So if you had a custom sprite, you would draw your health bar. And then you can draw the sprite directly over where the health bar would be and then just not show the border or the back color. Whatever it is that you would need to do to change that, you can have your own custom health barn here. You would just still use this function to actually draw the amount. Okay, so there's our health bar. You can see as we take damage down, and then as we die now, that's a little too small. So why don't we go ahead and change that? Let's add that to actually be minus 200. That way, it's a lot longer, and we can see each tick of damage as it comes off. That looks really good. You can see that it gets changes color as if it's low. And then as we go down to your health, Okay, that looks really good. Now that works for the health bar. But I also want to do something with the lives of the player. So what I want to do for the lives actually come into sprites and we're gonna right click and create a new one. I'm gonna call this SPR player lives, I'm going to go to import, and this is gonna be inside of the Kenny Resource is, and it's gonna be in the PNG. It's gonna be underneath you. I Now you see, they have some spaceships here that a little bit smaller that we can use. I'm gonna pull out the green space ship because that's the color that we've got. And for the most part, that looks the same. But you can also see that it is a little bit different. It's more pixelated, and this is meant to be used as a life icon. So I'm gonna middle center that I'm not gonna worry about. The collision masks. So we're not gonna have anything like that because it's just gonna be the spray that we're drawing. They're inside of the player ship. We're gonna do something new here that we haven't done yet, but is very important. This is gonna be introducing four loops to us. Now, a four loop is just a fancy way of saying I'm going to do a certain number. I'm gonna do something a certain number of times and you get to have complete control over how that's done Every time. So a four loop has some fairly strange looking syntax. What we're actually gonna type doesn't look like it makes much sense. But part of it is that you just have to follow what's there. It's been like this for a very long time. Almost every language has a four loop, and it looks very similar to this. So follow along and then I'll explain it as we go. So I'm gonna say four. I equals zero. I is less than my lines. Not may my liars. And you're using semicolons here after each one of these? No, you say plus, plus I and open those in close it within brackets. Now, this is fairly confusing if you've never seen it before. Also, the idea of loops might be really confusing if you've never seen it before. So I'm gonna actually get the sprites drawing on the screen. And then we're gonna take a look at how that works inside of the D bugger. Hopefully you have a good idea of that. So we're gonna put inside of here, draw Sprite. That spread is gonna be SPR player lives. I'm gonna say some image of zero and the X and y coordinates is gonna be right below the health bar for our player. So we're gonna say 100 room with my last 50 and the white corner is gonna be right underneath. It's gonna be 100 and the That's all we need for that. That's drawing the Sprite. Now, the thing is that this is going to draw Sprite a certain number of times, But we also need to change where that sprite is being drawn at every time. So inside of the x coordinate, I'm actually gonna add something I'm gonna say, minus inside the parentheses. I'm going to say I times 50. And that's because this I value is actually changing over time. Now, if I run this, we're gonna see those sprites show up. We've got three players. Great. And now, if I die, we only have to show. So how that works is the four loop is actually going through, and it starts at zero. So it says that I is equal to zero, and then it looks inside of this code right here. It says, I'm going to draw this sprite just like I'm told. And this math right here is for the first Bright is gonna be drawn exactly where we say it to say it's supposed to be drawn. Then it comes up and it looks at this condition right here Is I less than my lives. Well, if we go to create, we see my lighters or equal of three. Which means that yes, this is a current in a valid statement. So then we come in and we say, Increment I by one. And then we come back down to here and we say, Draw the sprite once again. And this time I is gonna be equal to one, which means that one times 50 is gonna move are drawing of the sprite over to the left, 50 pixels. And then it's going to come back up here and it's going to say Increment I and it's going to say, Is I less than my life? Still, it's doing this chick every single time, and then it comes back down here, and then it comes back up here to check it again. And as long as this is a true statement, it will continue drawing those sprites. As soon as it's not a true statement, it's going to say, OK, I'm done. It's gonna back out and go on to the next line of code, which would be down here or in another event or something. So a four loop is going to do something over and over again. And because we have a variable inside of here that is changing, we can change how, exactly? That thing is done like a drop vent being changed a little bit at a time. So if I press f f six to run this, we can actually watch the I value change an increment inside of our D bugger toe hopefully give you a visual representation of that. So inside of here way have instances and look at O B J player ship right now before Luke was about to start. So if I say step into the next line of code right down here, we now have a variable called I, and it is equal to zero. And now it is inside a few minutes gonna draw the very first sprite. So when I click on that and it draws the spring now it checks. It says, I'm gonna increment. I I'm gonna check my lives. And now I is equal to one, and it's back inside of here because one is less than three. And again, it's gonna do this again and again until all of a sudden I is equal to three and three is less than three, is a false statement, so exits out of the four loop and goes into the next code that it's going to run. And if the press play well, it's actually not gonna do much because it's gonna come back and run the four loop again and again, every single step game. But you can see here that the ships are being drawn and it looks pretty good. So hopefully that gives you a good idea of how that works. Four loops are very, very powerful. You will be using them all the time once you get into more complex games. But for now, we're just using them in this simple fashion because you can use them to do complex drawing things like that. You could have an if statement that says, If my lives is equal to three, draw Sprite, have three different draw Sprite functions, and then you could have an if statement says, If my life equal to to draw that. But that is a lot of code. This is technically two lines of code because we don't actually need these curly braces. So you've got two lines of code to potentially draw as many lives as you want. We could increase lives here. We could put this to 20 and the four loop doesn't care. It has math that works, and it's just going to do exactly the way. So that looks really great, James. I back to three. So I hope that gives you a good explanation of how it for loop works. We're We're not going to use them too much more, but they are essential for once you start getting into deeper programming and trying to do more complex things. So in the next video, we're gonna give the enemy ships a score and then have the player increased the score every time that they kill a ship and have that drawn to the screen as well 42. 13.2 Player Score: all right, so now it's time to get a score onto the game. That way we can see how well we're doing. The score could go in multiple objects that we have right now. You could put it in the player ship as that is persistent. As long as the game is running, you could put it in the O B J level one to keep track of how well it's doing. But both of those are kind of temporary objects. As soon as we make a new level and move into it, those objects are not going to stay. And keep that data the player ship. We're going to respond in a new level if we do that because we wanted to start fresh, although you might keep some of its stats, those air things that you can do by clicking persistent on an object. But what I like to do is when we have game stats, I like to have a specific object that controls all of those forests. So what we're gonna do is make a new object and this object is gonna be called O B J system . And you can name this whatever you want for the most part, I'm gonna put system related code inside of here. That way, if you need to know about, like, an option for the game or a score where that would be, it'll all be inside of one object. And you know exactly where that IHS So we're gonna create event here. We're not gonna sign this. A sprite. It's not gonna be something that you're looking at. It's going to be just keeping data for us. So I'm gonna say set up variables. This is gonna be the first time that we look at something called global variables. Now, global variables are super nice, but you've got to be careful because they can become dangerous. And they can also become a bad habit if you use them all of the time. So global variables are variables that you can access anywhere. So if I type global dot uh, players, score equals zero, I suddenly have a new veritable. It's gonna change color that I can access from any object anywhere. If I go into my player ship, I can say global dot and it's gonna know right away that player scores what I'm looking for . This is gonna be a variable that I can change inside of my ship without having to say other dot and access that veritable or seo BJ system dot and access that variable. This is very handy because we're gonna want to check the score inside of the player. But we don't want the player to have the score inside of them because if they get destroyed , if we change to a level where there is no ship because it's a menu or something, then we would lose that score if it was inside of the player ship instead, we're gonna have this over J system and we're gonna mark this as persistent. This is going to continue for every single level inside of our game. It is never going to go away. So inside of room zero, I'm actually going to add that right up there. So now even if we change rooms, even if we go anywhere else, this system object will stay there. And this global dot player score will always remain what it is. It's not going to be refreshed or reset unless you restart the game. So now let's go into enemy ship. We want to give this ship a score. So we're gonna say my score equals 10 will give each ship a value of 10. And now, in the animation end, when it destroys itself, let's add some curly braces. And when it destroys itself, what we want to do is, say, global players Score plus equals my score. That way, when it dies, it is going to give points to the player. But it's on Lee going to give points of the player if it dies, because the player blew him up. This SPR explosion, the animation, and this only happens when they get shot outside of the room. We're not going to give the player any points. Otherwise, that would be kind of silly. Now we need to be able to draw that on the screen, and that's something that we can do inside of our player ship. We're filling up this draw event very quickly. There's a lot here, but that's OK. So we're gonna come in here, we're going to say, draw the score so inside of this draw event for the player and the reason we have it in here and not in the system object is just because we only need to score to be drawn when we have the ship on the screen. There may be times you want to draw the score otherwise, but when we're actually playing the game fighting against enemy ships, our ship is going to be there and we can draw the score. If, like on a menu screen, you want to look at the high score, then you might want to put that inside of the O B J system. But for now, we only need the score to be on the main screen. So I'm gonna say draw text And this is a really handy function that is just going to draw text to the screen. And I'll show you how to do something called Can Canton a text right now as well. We're gonna draw this in the top left corner. So I'm just going to say 15 15. That's gonna be right near the top left of the screen. The string we want to draw is going to be first players score. So you gotta put that inside of quotations and then we actually want to put the players score immediately after this, so I'm gonna say plus and then we need to give it a variable. Now, we can't just do this. Uh, you can't actually use the plus sign When it comes to strings, This will say Okay, I think that's OK, but if we run it, it's going to come into a problem. It's going to say you can't add strings. Okay? Execution error can't do it. But there's a function for doing that. Instead, if you type string and you put whatever variable you want inside of that function, it's actually going to convert this variable into a string and then it will know beyond a shadow of a doubt. The plus sign is actually just a put to the two strings together. We're not trying to add a string and a number. Okay, player scores there, but it's time I can barely even read that. So let's look at fonts really quick. We haven't talked about them much, and we haven't used them yet, But funds are just a way of saying I want to change what's being displayed on the screen, and I want to look exactly like this. So I'm going to right click create a front. I would name this F N T. We'll name this event, Eguchi. So, like the graphics, the you I that's on the screen, This will be the font that we use so you can select your font eater and you can choose from any fund that you have installed on your system. And I'll just choose really boring one at the beginnings, we don't to look through those, but then we can change the size. So the size I'm gonna put up to 20 and then you can see it's much larger. So we have a much larger size fund, and that's what we want to be able to actually see the score on the screen. So then we're gonna go into the player ship and to set that fund, we have to come in here and say, Draw, set, font. We have to actually tell the game. Now we want to use the font right here, and you just give it that one font that you just set. Now, as a side note, this font that you just set right here is now going to be used as the default font for everything that is going to be drawn. We don't have any other text being drawn to the screen. But if we did this would now be it. And if you didn't want that, you would need to set draw set font before each individual texts that you're drawing on the screen toe have it be different sizes, shapes, fonts, whatever. So once you set the font once, once you said the color once. Unless you change that again somewhere else. That is now the default font color Alfa whatever for your entire game. So it's good practice that you set the font for every single U Y element that you put there . That way you haven't exactly what you want. All right, that gives us a player score right there now. So right now, the players Stewart is changing. Going out. Eso waits until the player ship is actually finished animating to give a score. But you might not want That's something that you could probably change if you didn't want that we could come into the enemy ship. We can change this part right here. If we cut this out and we go inside of here, we can change that to when it changes to the Sprite explosion immediately. That would give you a more immediate feedback might look a little bit better. Try that. Get destroyed Now, I didn't just notice one thing. If we run into a ship we're not destroying, we're not gaining those points. So let's go into the collision with the oh BJ player ship. And we do actually want to get points when we collided with another ship. That's what we go ahead and say with other global. That players score is equal to my score. Now, if we collide with the ship, will also gain those points. Okay, now, another thing I just noticed, which I'm not sure how I forgot, but we aren't destroying the ship with an explosion when this happens, which is kind of sad. So I think what we'll do to comments that line out and we'll say other dot current health equals zero. And then we'll say, I'm sorry, not other. We just worry inside of other. Then we'll say straight index equals SPR explosion. That way, we're actually changing the enemy ship to be an explosion. Right? When we run into it that way, it looks the same time. There we go. Balance much. Now we get points when we shoot chips. And also when we killed him. All right. I think that looks much better. I think that works a lot better again. Design decisions. If you don't want to look or function that way, hopefully you can see how you can tweak it now to do it exactly the way you think looks good. And that fits the game you want to make. Now, in the next section, we're gonna be looking at how to bring in the boss for the end of the level because every good space game needs a good fight with the boss. 43. 14.1 - Setting Up The Boss: All right, now it's time to get our boss set up. We're just gonna have one boss for the end of this level. So we're going to bring in his sprite, get his object set up and do a little bit of animation as well. So first, let's import the spray. I'm gonna call this SPR bus. And again, if you had more than one bus, I would recommend naming them a little bit differently. This is going to be under resource is Kenny, P and G. And we're gonna pick one of these UFOs you can choose in the color you want. I'm gonna choose Red for bad guys and bring him in now. Currently is only 91 by 91. So what we're gonna do is a little bit of image scaling. So double click on the sprite, click on image at the top, and we're going to resize all of these frames. I'm gonna make him 450 by 450 so we're going to blow up his sprite to be a lot larger. Now, to check that out, I'm going to right click and create his object really quickly. Se o B J boss, And I'm gonna sign that sprite. I'm gonna go to our room and then inside of our room. Let's bring in that boss and see if he is large enough. I think he's pretty big. He looks pretty good. Now, whenever you scale a sprite or an image up, you probably know that you lose quality. And that's why I always recommend that you have a large sprite and scale them down if possible. Right now, we can't do that. But the UFO also isn't too detailed. So the lack of quality that we lose is not gonna be that big of a deal. So let's go back into the Sprite, and we're gonna do a little bit of animating in here now. I'm not an artist, and I'm also not an animator, so we're not gonna do much. Instead, we're just going to a little bit of a trick. So I'm gonna copy, and I'm gonna paste that one frame. Let me to go up to image, and I'm going to flip That's selected frame. Now, the part that we're kind of animating is gonna be this lighter purple section. We're gonna have it look like it rotates around. And to do that, we're gonna copy and paste each time we change it. So I'm using control, See in control V to copy and paste. So on the newest one, I am then going to mirror that selected frame and then I'm going to copy and paste that one click on the new image. And then I am going to flip that selected frame and then it kind of looks like he's turning around. Now, that is very fast, So I'm gonna bring his speed down a lot. I think a speed of three looks pretty good there, so we will keep it at that Now, of course, if you want to add in more animation, if you like doing that kind of thing, you can definitely change that. But for right now, for what we want to do, that will work. Fine. Okay, So what's exit out of that? And now let's go into the boss object inside of here. We're gonna add a create event. What we need in the boss is some some of the same variables that we have in our other ones . So we're gonna say just setting up variables so we're going to say something like Max. Health is equal to 50. There is a current health is equal to Max Health. We're also going to give him a score of 100 so this guy will be worth a lot more. And that gives us the variables that we're gonna need to actually do something inside of here. Now, in the next video, we're gonna look at how to set up when he comes into the game, because right now we don't have any timer. We don't have any goal for him to come in. So we're gonna look at how to set that up with another global variable. 44. 14.2 - Bringing In The Boss: all right. Now that we've got a boss object, let's figure out how and when we should be bringing him into the level. Now, in most shooters, in most games, the bosses appear at a pre defined point whether that be at the end of the level when a certain score has been reached. Whatever the case, maybe we want to determine when the boss will be brought in. And to do that, we need to have a little more data, a few more variables inside of here. So instead of overdue system, I'm gonna make another global variable. I'm gonna say global dot max score is equal to 100. So this fable right here is gonna be what we're gonna look at, and this is gonna be different for every level inside of the game. But I'm setting up the variable inside of the persistent object so that it is always there , and we can always check it, but inside of the level that were specifically on, which is O. B J level one. Right now, we can come in here and we can say global dot max score is equal to 100 or we consider the equal to 150. The point of this setting the global variable here, is that this level object can actually change the global variable for later. On that way, it's always there because it's an over J system. But each level can have the max score be different. So we'll set this equal to 100 for right now so that it's easy to test now. What we want to do is check if our players score is greater than or equal to the Max score . And we're actually gonna do that inside half an alarm event. We're gonna add alarm one, since we already have alarm zero and I want to just say, Bring the boss it. And inside of here, we're going to a simple if check. We're just going to say if global dot player score is greater than or equal to global dot max score, then bring in the bus instance, create layer, and now where we're going to create him is going to be outside of the room above the room because right now the boss doesn't have the same code that the enemies do, where once they leave the room that get destroyed because we want the boss to actually look like he's appearing coming down the level. So we're actually going to set his appearance to be, uh, room with Divided by two is going to come into right in the middle. We're going to start right in the middle, but his Y position is actually gonna be equal toe negative. 400. We'll put him on the layer and we'll bring in the boss object. And here's where you could add in somewhere sound effects as usual. But what we need to put in first is an else statement here that says, If this is false, just reset the alarm. That 60. So this is just going to check every second to see if the boss should be being brought in. And then we need to go into the create event as well. And add in that alarm one being set. Otherwise, it's not going to run it all now. This could be inside of the step of it. There's no reason it can't be, but for our game, it doesn't need to be because the step event is using the every frame of the game to run all of its code Bring in the boss. I don't think needs to be as specific as on the frame that your score reaches greater than the max score bringing the boss some games for sure. But in this one, I'm just showing you a way that you can use an alarm and you're saving a lot of CPU cycles . So the larger your game gets, the more complex against the slow. It'll run on machines, especially older machines. So this is one way that you can say I don't need to check this 60 times a second. Instead, I'm gonna check it one time a second, because that is good enough. Now, we don't have any AI inside of the boss yet, so we can't actually look to see if he's created. Except if we run the d bugger. So I'm gonna go impressed. F six, we're gonna run the debunker. We're gonna reach a score of 101 in 10 Really quickly. Here. I'll leave this up. We can look at the instances and turn on this really bugging. OK, now let's get a better score way so we can actually see him right up there. So we don't need the debunker at all, But you can see here that are Score has now reached that Max cap on. The boss is now inside of our game. Now we're still spotting enemies, which is not what we want to do because I want the boss fight to be just between the player and the boss and again, design decision. You could do differently. But instead of level one, what I'm going to say is inside of here. So we have right now if the instance number is less than that, we're creating this. But what I'm gonna add in is one more thing. I'm gonna say double and percent or you can put. And both of these were the same thing. So ampersand and and are saying there needs to be this condition to be true and this next condition needs to be true. We're going to say if there's less than 15 ships and instance exists now, this might be new to you as well. Is this exclamation point? This is saying, if this is false, which is a little confusing to read, but it's good to know what it is out there. So this is a way to say if instance exists if this turns out to be false. So if there is no boss, continue doing this. If there is a boss, don't do this anymore. We can also phrase it like this If that turns out to be false, both of those air. Okay, there's nothing wrong with either of them now. We also wanted to be that once we defeat the boss, we don't want to be spawning any more enemies because we will. If that's the case, I'm going to say if global dot players score is less than or equal to global dot max score , then you can continue spotting enemies. But if it is not, then stop spotting the enemies this way. It's only gonna bring in enemies as long as it should. So what's the bosses? You okay, so that's the last of the enemies and there's the boss right up there. That's exactly what we want to happen. We'll have an epic boss fight between the player and the boss without any interference from the enemy ships. Now, in the next video, we're gonna look at how to bring that Boston and start giving him a little bit of a I 45. 14.3 - Moving The Boss Around: So we have the boss being brought into the room, but currently he has no ai. He doesn't move, He doesn't attack. He's not drawing a health bar for himself. So let's work on some of those things. The first thing we want to do is have him move into the rooms that we can actually see him proper. To do that, we're going to use the function we've used before called Move towards Point. Now he's already spawned in the exposition in the wide position kind of where we want him to be. But now we want him to move down. So we're gonna keep him at the exposition already. The why position is going to be room height to fight it by two, and the speed we'll set at five. Now, before we go any farther this speed variable right here, we actually want to change into a variable. Right now, it's just a constant five because we're gonna use move towards point a couple of times. Let's go ahead and change that, too. Fly speed equals to five, so we can change this to fly speed. That way, any time we need to change the how quickly is moving. We can change it here instead of having to go to all of the functions where we've put that five. All right, now I'm gonna show you something that you might not have realized yet when moving this function right here. This move towards point, this is a great function. But the thing is that you also have to catch it to show you what I mean. Let's go into the O B. J system and let's change the players score here. This is a cool way that you can test different parts of your game without having to play through them. So I'm gonna change. The players scored equal to 110 right away so that the boss is going to be brought in instantly. Now, when we use move towards point, it moves that object to the point. But if you don't catch it, it's going to continue moving forever towards that point in the room. So what that means is that our boss is moving towards that area, but he's not actually being told to do something once he reaches that spot. Our ships are moving towards a point, but it's okay if they just keep on moving because they get destroyed once they're outside of the room. But for the boss, that's not gonna work. We need to say, once you've reached a certain point, do something else. So to do that, we're gonna add a create were at an alarm event. And inside of this alarm, we're gonna say, move side to side the way I want the Boston function. And you can change this because you are the designer is once he's inside of here, I want him to reach about halfway down the level and then straight right to left shooting at the player of the whole time to do that instead of the alarm. I'm going to say if he's on the right side of the screen, moving to the left and then vice versa. And that's actually gonna be done through Justin if and else right here. So we're going to say if X is greater than room with divided by two, then we're gonna say move towards point zero, which is the far left side of the screen and is why position will stay exactly the same at a speed of fly speed and then else we're gonna move him towards the point room with which will be on the far right y position the same and fly speed once more. Now, how are we going to trigger this? Because we don't know how large the room is necessarily. I do. And I could count exactly how many seconds it takes room to move from right to left. But that would be very tedious. Instead, I'm gonna add a new event. We're gonna go to other Intersect boundary. This event will trigger as soon as an object touches the boundary. The wall of our room, the outside of room event will only trigger when an object has been completely outside of the room. And we don't want our boss object to get there. Instead, As soon as he touches an edge, we're going to set the alarm to trigger so that he switches sides of the room and start straightening the other direction. I'm gonna say trigger alarm here and we're just going to say alarm zero is equal to one. Now we have to be careful here really quickly because this Intersect boundary is going to trigger because, as you've noticed, our boss actually spawns Let's look at this at negative 400. So he is going to be spawning at negative 400. And that means that his Intersect boundary event is going to be happening immediately. So we want to say this is only gonna occur if his why position is greater than 300 or so. That's number that you can tweak if you have a different sized boss or a different sized level. But basically this is saying once he's completely in the level, then start activating the Intersect boundary. Otherwise, don't worry about it now we're almost there. We also need to add in inside the creed event alarm zero equals, say, around 180. Now, I'm putting this in here, but I'm gonna show you another way to do this. So this is going to say after three seconds when the bosses spawned, trigger the alarm to move him side to side. So let's watch that and see how it works. This is something that you can change the speed, the alarm number. But for the most part, it's gonna come in and it's gonna wait that exact time he's going to start moving now. This is a number that you can tweak if you want the boss at a different spot. So if we wanted to do just two seconds, then he will come down and then start activating that alarm and going back and forth. Let's take a look at that. Okay, Now that why events inside Intersect boundary didn't work, right? So let's actually change that 100. I didn't try that one more time. Depending on the size of the boss and where you have him stop inside of your room, that will work. Or it won't work. Okay, so right here, that works fine. So he's hitting the the edges, and he's going back and forth. But what if you wanted him in a different spot? Well, you can keep changing this alarm event to find exactly where you want, but that's kind of tedious. Instead, what we can do is we can create a variable inside of here, and we can say reached point is equal to false. Now, this is going to be a boolean variable that just holds true or false and is just going to be doing one thing for us. It's just gonna be saying Have I reached that point. If I have, then we're going to send ourselves to true that way. Inside of this step event were about to make something specific will happen. So it's at her step event now inside of here. Trigger alarm zero at a specific point. So I'm going to say if my Y position is greater than and here we can choose a very specific number. So inside of our room, if you knew you wanted the boss to be, let's say, at 280 you want him to be right there. We can say as soon as he is greater than or equal to 280 and reached point is equal to false. Then we will say alarm zero is equal to one and reached point is equal to truth. Now we have to have this variable inside of here. Otherwise, the alarm will never trigger because if you only had if why is equal to or greater than or equal to 280 alarm zero equals one that would trigger every frame of the game. An alarm zero would be permanently stuck at one, but when you have a boolean variable inside of here you can say set that equal to true And this will no longer trigger ever because that Boolean variable has been set true. So now we can come in here and we can check to see that this is going to run. So as soon as the boss reaches that 280 point right there, he's gonna stop. It is going to move forward to verify that we can come into our create event and we can comment that part out and we can look at this. Go. And now I'm showing you these things just so that you get the idea of how to bring in the boss and how to choose where he is at a couple different ways so you can use the alarm. You can use the step event with the Boolean variable to change it from false to true. And you can do that a couple different ways if you wanted to. The step event gives you more control. The alarm, in my opinion, is a little bit easier. Just a little bit more tweaking upfront, since you have to figure out how many seconds until he moves in there. So that's how you get the boss in? He's inside the level. He's moving back and forth, which is great. Now I'm gonna check one thing really quick, cause I'm not sure I set the origin of the boss properly. Yeah, he is in the top left. Now. Some of you might have caught that. I'm not sure how I forgot to check that, but he is in the top left. So now let's double run that and make sure that things still work properly. He looked a little bit off to me, but I had noticed why, until this point, let's run there now it's coming down the middle of the level and now his wife position is a little bit off, so that means the Intersect boundary is wrong. Let's put that to 200. And the reason that I'm doing this and not just cutting between these and picking the exact numbers is because this might be an area where you have some problems with your boss and I understand that, uh, this might be causing you some problems. I'm trying to show you how you can debug this kind of figure out what's wrong with it and what's not. So what I'm gonna do right now is actually comment out this entire step event because I actually want my boss to be coming in on the alarm at two seconds. So I'm gonna run that now and now he's gonna come in and he's going to be hopefully about halfway down the scream and then out. Nope, it's still too high. So it's the alarm event that's triggering improperly. So now let's change that to 1 80 and let's run that again because we changed the origin. He's now spawning up a little bit higher than I expected. And he's coming in in his wife positions a little bit later. So now the Intersect boundary is being triggered. Let's change that to greater than 400 run this one last time. And now we should be good to go. So if you encountered any problems, hopefully this helps you. Hopefully it also shows you that when you're making games, when anybody is making a game, they could have problems. Professional game developers and counter bugs airs problems all the time. Don't let it get you down. Look at it as a challenge way now have our boss in here. He might be a little too low, but again, that's something that you can tweak. Those are things that you can change. Change that toe 1 60 He'll be a little bit higher up, which is where I would want him. But I'm gonna leave that there for now. In the next video, I'm going to be setting up something called a screen shape. So when he comes into the level, I want something cool toe happen. I'm gonna show you how to do that inside of a script, which is a fantastic thing that gamemaker has to create your own functions. 46. 14.4 Screen Shake Script: So we're gonna take a small break from coating the ai of the boss to show you something called scripts. Now, scripts are basically creating your own functions that you can call went over and wherever you want. They are extremely powerful, and every programming language has them in one form or another. Usually called methods functions here. They're called scripts, and we are going to create one called Screen Shake so that when the boss comes in the screen that the players watching is actually going to shake back and forth like there's a big enemy approaching. So to get started, we're gonna right click on scripts. We're gonna create a new one and we're gonna name this just like we've seen other functions being named. We're gonna in the screen, shake, underscore between the words. Now this allows us to then call this script anywhere we want. And to do that we can I should just go into r O B J Boss. I was gonna call screen shake by typing it out screen underscore, and it's going to come up as a script right there. So if I click on that, close it out, it is now a function that we can call now, Something that I like to do is change the color that our scripts take on inside of our code editor. That way, we know it's something we've made and not something that gamemaker studio has built. In time, we'll click on file preferences down here under text editors. I'm gonna go to code editor and then I'm gonna find script names, and I'm going to change the color setting. Now, I recommend changing this to something that there really isn't anything else like it out there. And to do that, I'm going to change it to a yellowish color. So change the highlight here, see if we can get it to a nice kind of bright yellow color click. OK, in applying. And then we can look at it. Now, all of a sudden, our scripts look unique, and we know that they are things that we've written instead of built into gamemaker studio . Okay, so right now our script isn't doing anything, it has no code inside of it. But if I click on this and I middle click on it or press F one inside of it, I can have it actually brought up in a little window right here, which could be really nice, because then you can immediately look at the script that's being used, and we can actually change its code as well. So what we want to do is give our script information just like all the functions inside of gaming studio, or at least most of them. We're gonna pass in information that then tells it, the intensity and the duration, the length of the screen shake. So to do that, we're going to need to access the data that's being passed in the first. I'm going to give it a small description. So I'm gonna say, Fix the screen A a certain amount and for a a certain time, no, to get information that's being passed in, you have something called arguments Now. I've been using that word for a while, and you can see here if we type argument. There are a lot of different numbered arguments, and that's because you can have up to, I think, 13 different arguments being passed in to a script that you create now. Unfortunately, all of them are. If you pass anything in, it has to be used. You can't have an optional argument like instance Destroy where, if I type instance, destroy here. We've used it before, and you can see here that they're actually asterisks on here. You can't have that. You can't have an optional argument inside of your script, but you also don't necessarily have to use it if you don't want to. If it's of the wrong value, What we're gonna do is a sign the information we're passing in two variables for our script so that we can then use them. So this might be a little confusing, but follow along, and hopefully, by the end of this video, you'll see how powerful and how amazing scripts are that you will want to use them all the time. So first thing we're gonna get is the amount so screen shake amount that's available that we're going to create just inside of the script. And that's going to be the value of argument zero. I remember computers start counting at zero. Argument. Zero is going to be the first thing that the that we pass into this argument. Then we're gonna say screen shake length is equal to argument one. Now the way that let looks inside of here is that then we now have two things we need to pass into our screen shake so eventually this is going to come up. And if we start using these anywhere, it's going to require us to pass in those arguments. So I'm going to say, as an arbitrary number, I'm gonna say a screen shake of to in a length. We're gonna do this just in seconds of three seconds. Now these numbers will make sense once we start using them. And the way to make a screen shake is to actually alter the view ports inside of the room. So we have you ports were using, and what we want to do is actually change where the view ports are at every frame over and over again to make it look like the screen is shaking. It's not really shaking, but it's gonna look like it's really shaking. So to do that, we're actually gonna create an object that's going to do it for us. Inside of scripts, you normally want them to be and closed, meaning that the strip is going to do to do one thing, and it's gonna do that thing. Well, so what we're gonna do here instead of being able to do that exactly that way? Because we can't actually alter the view ports very well inside of a script. Instead, what we're going to do is create an object. We're gonna make an object called LBJ Screen Shake, and this is gonna be specifically made for just shaking the screen. And the thing that we're gonna do is actually within our script is we're going to create that object to really call this a shaker. Where's the instance? Create layer and where we make this doesn't matter. I'm just gonna put it at 00 and it's going to be on just layer, and we're gonna make O b J screen shake. And then I'm going to say with Shaker, we're gonna alter some of its properties inside of here. But for right now, we need to go into the O. B. J screen shake and actually set up some of the properties in there. So all of this function is all the script is gonna do is shake the screen. You don't want it to affect anything else. You don't want it toe. Have something called side effects, which I'm not really gonna get into. But basically, if you call a script called Screen Shake, you wouldn't want it to randomly increase the players. Score you instead. Onley wanted to do the one thing it's designed to dio. If you want multiple effects from a script, I'd recommend writing multiple scripts and having them call each other. That way you have everything they do enclosed inside of them. So this script is going to create a screen shake object. So it's gonna add a create event here. And then this object is going to shake the screen and then destroy itself after the allotted time has passed. So we're going to save our shake him out. And we're just going to say that because inside of this script, we're actually gonna set that. So we've created the object right here. That means that if we were to follow the d bugger code, which we could dio, we'd actually see it, creates the object, go into the object and set its properties, and then we're going to set the properties that we want inside of this with statement. So we know inside a shaker we just made it. We have a variable called shake amount. We're going to set that. We're gonna say shake him out is equal to screen. We need to save here, other dot screen shake. I'm out. So this variable, which is argument zero, is now being set inside of this other object, which is what we want to do. Now let's go back into over J screen Shake, and I'm gonna make two more variables going to say Original X and original. Why these are gonna hold the X and the Y view ports that they are when the game starts so that we can send it back to them at the end of it. So Original X is actually going to equal view export zero original. Why is gonna equal view Why port zero? Now this is important. We're just using the View port zero. So if we go into rooms, you can look and see view ports report zero. That's what's visible. That's what we're using. If you have more than one view and you're using the screen, shake inside of the screen shake object. You'll probably want to figure out which of you port is active. I'm not gonna get into that now, but there are a lot of ways to find that out, including the manual. So look at that if you're interested. But we're setting zero here because that's the view port we are actually using. And now we're gonna add a step event. And inside the step of it is actually what's going to be shaking the screen so called Shake the screen. And the way this is gonna work is we're gonna set the view, export zero. We're gonna set this equal to a new number every step of the game, and it's gonna be a fairly different number. That way it looks like things are changing and moving around so inside of here, we're going to use a function called Choose. We're gonna pass in a couple different values and this is a great function because you could just pass in any numbers that you want any amount of numbers, I think, and then have it just randomly choose those for you. So the numbers that work in the passage are actually gonna be random. So we're going to do random inside of random. We're gonna do the negative, shake him out remember, Shake amount is right here this variable that we actually have set up inside of the script . So the negative of that times 10 So that's gonna be one value that it can choose from. The next value is going to be random. A positive shake amount. Times 10 put two parentheses to close that off, and that is going to be the shake amount for the view export. And now we want to do almost the exact same thing. But we learned it with the UAE ports. I'm gonna copy and paste. That changed that to view why port and then both of those ports will be changing by the shake amount. Times 10. And now, to finish this off, we're gonna add an event in the ad in alarm. It's gonna be alarm zero. So you say destroy self and set vieux port to normal. So inside of here, we're gonna say view export zero is equal to Original X and went to the same thing for the y port because now we are done shaking the screen and we're going to destroy ourselves. Now, you might be wondering, when does this alarm actually get triggered? Because we haven't said it there. Nowhere in here is the alarm being set. Which is true, because what we're actually gonna do is set the alarm from our script. So inside of here, I'm going to say, because we're inside of the object right now with this with statement, I'm gonna set its alarm zero equal to screen shake licks. Now, I'm gonna say screen shake length times 60. That way we can just pass in seconds instead of frames. If you don't want to pass in seconds, just delete this and then whenever you call it, you would just pass in the amount of frames that you'd want to be shaking. So I'm gonna say it, shaking for three seconds at an intensity of two when the boss is made. So let's run this and let's watch it happen. Should happen right away. Okay, so we get an error right away because screen shape length is not actually set. And that would be because inside of screen shake, we need to say, brother, remember, other is accessing these variables here, Not the variables inside of the object screen. Shake itself. Okay, there's the screen shaking. It looks really nice. is a lot of shake for quite a while. That looks pretty good. Now. You can imagine that now we have a script that we've created and that we can adjust. Would we call it? But we can also come in here and change this function at any time that we want, because it is our function. So in over J boss, I wouldn't recommend setting the screen shake to be very high. I think about five was as high as I would give it, because we're multiplying it by 10. But just for fun, if we set that to 10 we can actually look and see. It kind of looks like the game is lynching out when it's doing this, which might be kind of fun to do in a different game. But it is a huge screen ship, so you can set up like an if statement inside of screen shake. If you didn't want it to be higher than five, then you can, like reject anything higher than five and set five is the default variable. Those were all options that you have as the designer of the game, but then again, you're the one calling it so maybe you don't need that. But that's up to you. So that gives our bus kind of an epic entrance. So I'm gonna set this equal to three. Then we set it equal to two seconds, cause I don't want it to be too long. So those air scripts, they are extremely useful. I encourage you. Any time that you were gonna have code that you want to happen more than one time, you put it inside of a script. They are extremely useful and necessary. Once you begin programming larger games, hopefully you've got a good grasp on that. If you didn't do ask questions, I will be glad to help you. And if you are confused, I guarantee you are not the only one. Now in the next video, we're going to set up the gooey for the boss to have a health bar for him and set him up. So he starts firing at the player so that we actually have to avoid the boss because he's actually trying to do damage to us 47. 14.5 Boss Lazer: So now it's time to give the boss some attack moves. He's in the level he's moving around, but it's not very interesting it because he isn't actually attacking us. So to do that, we're going to actually give the boss and a new sprite for his laser specifically. So we're gonna name this SPR boss laser and we're gonna import this. And this is going to be inside of resource is Kenny P and G lasers now the one we're gonna choose is gonna be the laser red. 01 that's gonna look a little different. It'll be unique to the boss. And we also need to do something that is going to make it much easier for the laser to actually be aimed at the player. So we're gonna go into this spray, and all we need to do is go to image and we're going to rotate clockwise 90 degrees. Then we're going to come out, and now we're going to set the origin to be middle right? The reason for this is that when we are going to fire the lasers for the boss, we actually want the lasers to be flying directly towards the player And no matter where the bosses at, we wanted to look like the lasers are being fired at the player like they're aiming at them . And to do that, you alter the image angle a built in property of an object. You alter the image angle and altering it is easiest when it is a image facing to the right and the origin is on the middle. Right? So any time you're using projectiles, this is a little trick that I'd recommend using Have the Sprite be facing right and put the origin on the middle, right? It will make it a lot easier. And it'll look really great for any kind of projectile that you're using. So let's make that object really quick, O B. J Boss Laser a sign that that spray give it a create event. We're gonna give it in a speed of 15. Now, the reason we're using speed here is that we're actually gonna be using some of the built in properties for a movement that gamemaker has, and that is like speed in direction and also image angle. These are things we're gonna be altering when we create this laser blast for the boss. Okay, so with that out of the way, let's go ahead and go into the boss. And to do this, we're actually gonna make an alarm. We're gonna create a new event, alarm one. And this is going to be just the regular fire at the player. And all we need to do is create. And this time we're gonna use the depth function, so we're gonna use instance, create depth instead of layer. And now depth is the way gamemaker rooms to decide what is drawn on top of another. So if we look at the room, we can see that Well, there's not much here, But inside of the instances, properties inside of the layers property there are depth. So the higher the number, the actual further back it is from this from the main screen is the way you can think about it. So the background should always have the highest depth. Because you always want the background behind everything else. The instances layer has a depth of zero, which means that you actually see this on top of the background. If we were to move the background on top than it would be covering everything else, which is not what you would want. Now, the lower the number is, the higher the thing is drawn on top of the screen. So when we are going to use this function instance create depth. What we're actually doing is creating the laser on top of the boss. What looks like it's flying out of the middle portion of him. And so this is a really handy trick. If you want the depth to be set when you actually create it, so the depth is gonna be that it's gonna be X Y. The death is gonna be depth minus one. So whatever the depth of the instance that's creating it, we're going to subtract one from it so that this will always be on the top of it. We're gonna make O B. J Boss Laser. Now, to alter the properties of this laser that we're creating, we need to catch it. So I'm gonna say it. Laser blast is equal to this instance that we are making now inside of here. We also want to do a sound. We want to play a laser sound just like we have for the enemy ships. But this time it's going to be a little bit different, so we're gonna use sound enemy laser, but as a priority of one, it's not going to repeat. But this time I want to increase the volume because right now it's very, very quiet, and the boss should have a louder laser shot. So we're gonna catch this. There's gonna call this audio, and we're gonna go to audio sound gain. We've used this before. We're gonna pass in that index of audio. We're gonna set this 2.75 It's gonna happen instantly in one millisecond. So this will play the laser blast, but it's gonna play it louder for the boss now inside of here. We're gonna go with laser Blast that we're actually inside of the laser blast, and now we're going to set the direction and the image angle. So to do that, we'll say direction equals and now the direction needs to be pointing wherever the players at and the player can move around. It's not like the enemy ships that are just shooting down. They don't need to be changed it all. We need to use a new function called point direction, and this is a function you will use almost every time you create a projectile or something that is moving. That is not gonna be static, that it's always going to be facing left or right or up or down. So this is going to give us the actual direction from point A to point B inside of our room and be able to then face Are Sprite the correct direction? So the way we do that, we give the X and y of where it's currently at and then with the X and y of where it's going. So it wants to find Obi J Player ship dot exe and oh BJ player ship. Why? And then we're gonna set image angle to direction that we just set, except for the thing that we forgot, which is to set the alarm. So let's go in here. Let's go to alarm One is equal to 1 60 Now we've got this in two places, So let's go ahead and put this in a variable so we can change it much easier in the future if need be. So we're just gonna say active time is a good 1 60 Copy that. That way There's no misspellings anywhere. So the active time is just like when it's actually going to start doing something now inside of here. We need to also repeat itself. Now I want to have it be arranged. So we're gonna use I random range, and we're gonna say between 15 and 30 now, we could put those into variable, and it wouldn't be a bad idea, but for right now, we're not going to just so that we can see how this is gonna work. Okay, that might be a little too fast cause he is firing. Very. But you can see here these are always going directly and looking at the player, which looks really cool. So if this is the kind of difficulty you want, then this would actually work. Really? Well, I'm going to turn that down just a smidgen. Someone actually crank that up to 30 and 60 that way, it's not firing so fast, and then we need to set up a collision. So we need to go into a BJ player ship and we can actually do something like this. We can actually take this over J enemy laser collision event, and we can duplicate it, and then we can actually change what it's colliding with. So we'll go down. Toby J. Boss Laser. And then we have a BJ boss laser and over J enemy laser. So they are pretty much the same event. But now we're colliding with two different things, which is great. So if I run that again now will take damage from the boss with just a couple of press. Is there? Okay, great. We're taking damage. We're going down shooting us. Okay, Perfect. Now, I've said before that copying and pasting code is normally a bad idea. And for the most part, that is true inside of here. What would be a better idea is to actually have a parent objects. That is a laser. Now, parent objects are a way that you can give a, uh, an object hierarchy. So you can imagine that all of the lasers in the game So the boss laser, the enemy laser, the player laser, these would all be Children of a parent laser object. And then any time the collision would happen with that parent object, it will know if it's colliding with any of its Children as well. It's kind of advanced, and I don't want to get into that now. But inheritance and parents and Children, especially when making games is really important. If you have questions on that, definitely ask them, and I'll answer them. But I'm not gonna get into that right here. And so that's all we're gonna do for right now. That gives the boss a laser to fire. We're taking damage from him in the next video, we're gonna look at setting up damaged for the boss that we can do to him and drawing his health bar so that we can see where he's at in terms of health. 48. 14.6 - Boss Collision & Health: So now let's set up a collision event with the player lasers that they can start taking damage as well. So we're gonna add an event that is a collision with over J Player laser. And we're going to say Take damage. Now, let's look in the O B J enemy ship for a second because we actually have a collision event with code that we could probably pull from. That's very similar. So let's copy that for now. And let's actually pace that inside of here. So current health minus one? Yep. That'll work because we have current health as the exact same name. If current health is less than or equal to zero make sense. We change this, we increase the my score. My score is the same here. Okay, Then we destroy the other thing. Okay, That all works really well. Now, there are a couple things that we want to do inside of here. That is not the same as the ship. I would like to play a sound effect like an explosion. So let's look inside of here and we don't have an explosion. Sound effect. Yes. Let's go ahead and right click. Create sound Let's name this s s and d explosion. That's browse for it. Inside of resource is, uh it's just gonna be sound effects explosion. I'm gonna open this up. I'm gonna press play. That's pretty good. Going lower that 2.75 and we're gonna put this on stereo and we'll call that good. So that gives us an explosion sound because it's a big boss. He should have an explosion. So we're gonna go audio play Sound S and D Explosion. Priority of one and Loops? No. So we want to play that explosion sound. And along with this sound effect, I also want to. So we're changing the Sprite explosion here, but the Sprite explosion is time. It is very small compared to the boss. So if we destroy the boss, the explosions actually gonna look really small. And we contest that out by changing his health equal to five. And if we run this, we'll see a couple of things that we need to fix right away. So we have lost his lasers, continue to fire, and he's not being destroyed properly, which is something we haven't set up yet. But we don't want his lasers to get firing way. Also want this explosion to be a lot larger. So something we can do with that is changing. The Sprite displayed the image X in the image y scale to actually increase that. So once we've changed the explosion, we actually want to say image X scale equals 10 image. Why? Scale is equal to 10. And we're actually gonna also say image speed is equal 2.5. We want the explosion to play out a little bit longer, a little more majestic than it does on the enemy ships. Now, let's also at an event other and animation and and this is gonna be just like the ships. We'll say if spread index is equal to SPR explosion destroy himself. Great instance. Destroy story self. Is the comment up here? Okay, Now we also don't want this alarm, the one that they keep firing to trigger. We wanted to disappear. So to do that, we're going to set an if statement inside of this alarm. So we're going to say if Sprite Index doesn't equal SPR explosion, then go ahead, go ahead and reset it. Let's rest F five and let's run this and destroy the boss and see. Now the image should be a lot larger, which is great shooting at us. Okay, We got the explosion. He moved across. That looks really great. That's exactly what we need it. Now. We also need to have a health bar. Let's go to add event draw. We're going to draw the health bar. I remember The very first thing to do is draw self. Otherwise, the ship won't appear at all. Now we need to draw a health bar for him. So let's go to draw health bar and where we want to do this is going to be important. We can put the health bar at the bottom of the screen at the top of the screen. We can put it above the ship. If you had a lot of time and love doing animation, you could actually draw the health built into the boss itself. Like the ship would take damage. After a couple of shots, it would begin to crumple so that you could visually see the boss beginning to deteriorate . The more damage it takes, that's something that you could do by changing the Sprite index based on the amount of health that the boss currently has. I don't have a fancy enough sprite to do that. So instead, we're going to just draw a very large health bar across the screen to show that this is obviously the enemy's health. And we get an idea for just how much health he has. So the exposition is gonna be on the left, were going to say it would be on the 15. So it's gonna be right next to the left side of the screen. The why one is going to be right near the top. We're gonna say 15 up there as well, and the X two is going to be on the far right. So we're going to save room with divided by 15. So this is going to go from the left side of the room all the way to the right side of the room, and we're gonna give it, let's say, Ah, hundreds, about about 85 pixels in thickness. So the amount Remember the amount we can look for Go to player ship and we draw his health bar. We can pull the math from there because we're using the exact same variables that we've got . So if I copy this, go back into the boss, paste that in and common and go down here. Okay, so that will draw the correct amount. So now the back color. Let's go ahead and do see. And then if you do see underscore, you can see all the constants here. You can choose from all of these colors. So why don't we give it a purple background? Just to be interesting, The minimum killer will still be red, and the Max killer will actually do see green. So the direction is just going to be the normal direction of one. The show back will be true and short border will also be true. Okay, Now let's change his health back to a high number and run this one more time. Now we should see a giant, enormous health bar that spans the screen. All right, shoot him for that. You need me from left to right. Kind of interesting work. Feeling. How much has your Not a fan of the purple? Obviously, you can change that to me. It kind of looks like he actually has, like, a second health bar, which is something you could dio, but not what we want to do. So instead, why don't we put a dark grain there that gives us the health, the collision for the enemy. Now, in the next video, I want to give the boss a special attack. Right now, he's just firing a laser. It's fairly easy to dodge. I want to give the boss a super attack that he does every now and then that the player really has to try to evade. 49. 14.7 - Power Attack: so to give the boss a special attack were to figure out what we want to do. First, there's a lot of different options. The one that I played around with and found that I liked is that he shoots a bunch of lasers just straight out in front of him, kind of in an arc. It looks really cool. It allows the player to still be able to dodge for the most part, but it gives it a feeling of power and makes the battle much more engaging. So to do that, we're gonna add an alarm. This is gonna be alarmed to and inside of here. We're going to call this his special attack, and we only want this to trigger every now and then. And an important thing when designing a game is that the boss needs toe. Have a tell. That means that you can figure out when they're going to do a certain attack. They don't need to have a tell for everything. But if you ever played a difficult game like dark souls or you just played a platform or against enemies and you can figure out what they're gonna do based on their animation or where they're at with their health bar that is giving the tell that helps the player actually be able to play your game and beat it. Well, if there's no tell if it is completely random, it feels very unfair. So what we want to do is Onley trigger this at a certain point in the point I want to trigger it is actually when he intersects the boundary. So I'm going to come in here and I'm gonna put in some curly brackets so that we can also call alarm to and will set this equal to 15. So every time he touches an edge about 1/4 of a second later, he will then charge up and fire his super attack. And that way the player can learn when he's going to do it and can evade it in if they can figure out his tell taken beat him much easier. So inside of here, we're going to use a for loop. We've used it before, and they looks like this because a four I equals zero. I is less than 20 and 20 year we could put in as a variable so we could change this out, come in here and say power attack equals 20. That way we can change the amount of his attack at any time that we'd want. Okay, so his power attack increase I So what we're gonna do is create a laser, create 20 lasers, and we're going to send them out into the room that the player has to dodge. They're not all going to be aimed at the player like the regular laser is. Instead, they're just going to spread out in like a fan. So let's go ahead and make that Glaeser instance create depth again. We're gonna make it just X and y depth minus one. And the object is the boss laser, and we need to catch that. So we're going to say laser bolt is equal to that. They were going to say with laser bolts, so we're going to set the direction of the laser bolts with point direction like we did before. But this time we need to decide where we're actually setting it, because we don't want to put it on the player we don't. Otherwise, they would all go towards the player and they would just look like one Big Bolt or if they got hit by and didn't realize that they lose 20 life, all of a sudden it wouldn't be very fair. We want to choose an arbitrary direction to send the laser bolt out in so it's going to be from their X and Y position and we're going to say other dot I because we need the other Since we're inside of the laser bolt itself, we want to access this I right here and then we're going to say Times 200 and the wide position is just going to be the bottom of the room. So the room height and we'll do the same thing with the image angle that we did before and that will set all of these lasers to be just firing outwards but still heading towards the bottom of the screen overall. And when this runs, let's go ahead and shake the screens of the player can know what's happening will do a very small shake of to and we'll just do it for about half a second. So it's really quick kind of intense, but it'll trigger. It'll know that this is gonna happen and now, before we run this. There's one thing we want to do inside of here has changed the speed equal to 10. Normally, it's 15 and that's OK for the primary laser blasts. But for this special attack, we don't want it to be firing too quickly. Let's take a look at it and see how it looks. There's gonna hit the side of the screen. He's gonna shoot him out. Okay, That looks really great. He sends out a lot of lasers there. You can dodge it if you know what you're doing, and it makes it feel much more engaging. Okay, That gives him a special attack, something that is unique to him, that the enemy ships definitely don't do so with that. That actually gives us most everything we need for the boss. There's two things left I want to do right tackle those in the next video. And that is going to be a change of music. He's gonna have his own boss music or learn how to fade out music and bring in other music . Then we also need to set up a end of level. Seen what happens when he's dead. We're going to figure out how to tackle that. And then we're gonna look at changing rooms and sitting in the menu in the next section. 50. 14.8 - GUI & Level End: So let's get to changing the music. When the boss comes in, the first thing we have to do is get the music we want to play in our game. So we're gonna say S and D boss music after we create a new one and I'm going to choose the sound file, which is going to be inside of resource is music. And this is going to be the 2nd 1 the power of 10. So it's played it for just a second. It's got kind of a dark, ominous feel, too, which I really like, but it gets very loud very quickly. Let's change that 2.5 overall. So it's a little bit quieter and then inside of the boss, the boss is going to say when the music is going to start and stop because his entrance dictates the new music. So inside of here, we're gonna say, change music. In the creative end of the boss, we're actually gonna say audio found gain, and we are going to change the background music that's currently playing. Now we don't have that saved anywhere, and that's okay because something we can also do inside of here instead of passing in the specific index, we can pass in the sound i d. And that will turn the all of those that are playing to the level that we set them to. So we go to sound gain found background music is currently playing. We want the level to be zero. We want that to happen about five seconds after the boss comes in. At the same time, we also want to play the sound clip S and D boss music. We want the priority to be one, and we do want to toe loop if somehow the boss battle goes on for three minutes. So that way we've got new music coming in. I press f five. We should be able to hear that transition take place immediately. Okay. So you can hear the other music fade out on this music takeover. So it's pretty loud. Uh, that is again a design decision. I'm gonna turn this down 2.4. I don't want the music to be too overbearing. I think that will be a little bit better now. We also need to decide what's gonna happen when the player defeats the boss. So if they don't if the player dies, we already have that set up. The game just restarts inside of here animation. And yep, So if my lives is less than or equal to zero, let's go ahead and add this to restart. So we have a function called Game Restart. If we ever die, will just restart the game. This isn't necessarily what you would want to do. You probably want to show a game over screen, and that's something that I'm going to show you how to do in the next section when we're actually setting up menus. But for now, we'll just say Game restart because you died, so that takes care of the player dying. But when the boss ties, what do we want to do? Well, we want them to act like they've done something awesome and usually in space shooters. When you defeated the boss, you kind of fly off into the space into the distance, and then you move on to the next level, where you go to a menu where you see your score and you can pick out new power ups, new weapons, stuff like that. So what we're going to do here is Adams of curly braces at the animation end of O. B. J Boss, and we're going to set up a global variable, not set it up here, but we're going to change it in the global variable that we're going to have. We're gonna make it in just a minute. Is level beaten? We'll set that equal to True. Now let's go ahead and actually Constand sheet. Let's create that variable here. Global dot level beaten, physical, the false. Originally, it starts out as false and inside of each level. You probably want to also set that this is going to be a way that we can say is the level done yet because different levels might have different requirements, some of them might have a boss at the end. Some of them might have an obstacle to avoid. Some of them might just have a score that must be reached, so this variable allows us to say once that condition has been reached once they have defeated the level According to my design decision, let's go ahead and do something. So inside a player ship, let's go and add a step event. We're gonna say end level. So the end of the level. If you wanted to do specific levels, this is something that you could do. So right now we haven't named our level. Let's go ahead and do that. Let's go and call this room Level one now in the general case, what I'm going to say is, if global dot level beaten and this is a shorthand, if you say just this right here, this will only evaluate to true if global dot level beaten is equal to true. But the exact same thing is this. This is a shorthand. We'll put that in there just for ease of reading it. So now we say, if it's true, if they defeated the level, we want them to start moving up to the top of the screen and say Why minus equals. It was a player speed or fly speed, but then we want to do something once he's actually outside of the room. So we want to check his wife positions. Do you want to say if it's less than we want to be completely outside of the rooms, that's gonna be negative. 64 for him. For now, we're gonna do game restart. This will change once we can transition rooms. And once we have, like a main menu screen, let's just check them out. Let's put the boss of bosses have really low IQ. Test this out. Now he's gonna fly up, He disappears from the room, the game restarts. Okay, that's exactly what we want. We're gonna change this soon, but for now, the player disappears from the room, he goes away and it allows the game to then move on to the next stage. So in the next section, what we're gonna be doing is setting up a room transition and actually a menu so that we can select what we want and then go into our main game level. 51. 15.1 - Setting Up The Menu Screen: so, up until this point, our entire game has been taking place inside of one level, and that one level, although it can do a lot, isn't gonna be enough. Overall, there are some games that you can create inside of one level. If you do a few tricks, such as creating a very large level having different view ports and cameras having different sections of it walled off so that different sections of the same room look like this different room that's not necessary for our game. We want to have multiple rooms. We wanna have a menu. I want to show you how to create a credit screen because you deserve credit for all the work you've done. And I want to show you how to transition between those rooms. The first thing I want to show you, though, is to do something that we forgot about doing. And maybe you did. It already has come into the background, and I want to assign a better background here. So I'm going to right click on Sprite. We're gonna import a sprite. This is going to be inside of resource is it's gonna be under backgrounds and submitted the Sky NYT background. So I'm gonna click Yes to that. It's a very large background, so it has to be re scaled. But we're gonna put it in the the middle center, and then I'm gonna name this SPR level background. Except of that come into the room, select the background. Then what I'm gonna do is actually horizontal and vertical tile. That and we're going to do a vertical speed of one. So if I just pressed F five really quick, that room now looks significantly better. It looks very different, and it has this kind of parallax moving background feel to it. And on a side note, it might also take a little bit longer load this time. But you can see here that now all of a sudden the background our game looks like it's actually in space. It looks a lot more interesting. She had done that a long time ago, but that is the power of just changing small things and having them be a very large difference. So with that being said, let's go ahead and let's make another room. We're actually gonna make two rooms. We're gonna do create a room we're gonna call this room RM main menu. Let's make one more room. This is going to be room credits Now. We did talk about the look and feel of a room. We want them all to be about the same Vieux port, Same size. The width and height aren't near as important as the view port and cameras are for them because the view port in cameras, they are what actually is going to be what's on the screen inside of the window and how the window is shaped. So inside of these rooms who want to enable Vieux Port going to view port zero make that visible. And on this one, we're actually just gonna keep the width and height exactly the same. But we do want to come down to view port properties and change that the 12 80 by 7 20 And now you might be wondering how we get to different rooms. Well, the first room on this list is always going to be the one that starts the game. So if you want to do this, you come up and you move main menu to the top, which might mean moving level one down. So if we do that? The order of those is actually very important. So now if we run this room, main menu is actually gonna be the one right there. Now it's blank. There's nothing there, so it looks really boring. But we're going to fix that. Let's go ahead and add another sprite. We're gonna call this SPR menu background. We're gonna import that image, and this is going to be under resource is Kenny backgrounds and you can choose any of these that you want. I'm gonna choose this purple one. I think it looks really nice. Kind of bright. Gonna put that middle center and then we're gonna go into our room through our tab right here. Hey, we're gonna go to Background Layer. We're going to choose that straight. Now, we want to stretch that to make it much larger and actually fit, and we'll do the same kind of trick. We're gonna horizontal and vertical tile this and we're going to give it a vertical speed of one. That way it looks like the background is moving when it actually kind of is but really isn't at the same time. So that looks pretty nice. All of a sudden big difference there now to create a menu. There are a couple of different ways to go about it. I'm going to show you the easiest way. In my opinion, it's not the most flexible, but for starting out. It's definitely what I would recommend. So we're going to create a new object. This object is going to be called O. P J Button. Make sure I spell that right. So this is gonna be the button that the user is actually click on to do things. So we want to give it a spray to start out. We're gonna create another Sprite is gonna be SPR button, and we're gonna import this one as well. It's going to be under resource. Is Kenny PNG underneath you? I and I'm just going to do the blue button and import that, and I'm going to put that middle center and we don't need to do anything more with that. But we want to come down to our button in a sign that sprite to it and then inside of our main menu room. Let's go ahead and drag this onto there. But of course we have to be on the instances, layer. Yeah. Now, that's a little small for my taste. So I'm actually going to blow this up a little bit, so I'm gonna make this a little bit larger. I mean, a copy and paste that two times kind of spread them out, hopefully somewhat evenly. And that gives us three buttons that we now have to work with. But you might be thinking this is one button that we've copied three times. How do we actually do anything interesting inside of here? Well, that is what instance creation code is for, So I'm gonna add a drop vant, and I'm going to say draw my text. So the first thing is drew ourself, and then I'm going to say draw text, and I'm gonna put it at the X Corn is gonna be on the far left, so we're gonna use the b box left because we want to start it on the left. And we probably want to start on the left with a little bit of padding about Let's say five pixels. The why will just be why? And the string is gonna be a variable called my text. Now we don't have a variable at the moment. So let's go ahead and go to create. And we're gonna say Just set up variables we're gonna set up my text is a var my text because we don't actually want to give it a value. Now, this is a little trick that I've used that I really like. It can cause a little frustration if you're not used to it. But right now we're saying draw my text and we have available called in my text. But if I actually run this, we're gonna get an error. And we want to get that air because we haven't actually set up my text inside of that object. Yep, not set before reading it just as expected. So to set it, we're actually gonna go into the room and I'm gonna right click on a double click on these objects here, and then I'm gonna zoom in a little bit. And the button down here called creation code is what we want, because I can actually come inside of here and I can save my text equals new game, and you probably see what I'm doing here. All of a sudden, I'm accessing the variable inside of this button to give it text, and I could do the same thing on the middle button. I could say my text is equal to endless mode. I mean, exit out of that and we're going to come into the last button and do the exact same thing. My text equals credits, and now that gives all three of our buttons unique text to be display. The last thing we need to do is go into R. O B. J Button and inside of the draw event, we need to say draw set front. And here we need to give it a font. But we only have font gooey. And if we look at that font, it's not very large because that's being drawn on the level one. It's not supposed to be too large, so we actually need to create a new font for this. We're gonna say font menu and the type of font we choose will have it be consistence. Let's go and find Al Gary in, which is the one right there. Now the size is going to be significantly different. We're gonna put it at a size of 40. We're gonna bump that up to twice as much. Maybe even 50 would be appropriate. If we go to our button, we can then set that fund So font menu draw set front. Oh, I didn't take that correctly. Let's change that F and T menus what we want and we're also going to sit the color. Change this to see Red. We want that to be unique and to stand out on that blue button against the white background . So if we do that, we now have our text, and each button is displaying it just a little bit differently now. Obviously, that's not in the right spot, So why don't we fix that inside of our button? Where we are, where we're drawing it needs to be changed a little bit. We're going to change the why position to be, Why minus, Let's say 20. We also want to come into the fonts menu, and we're going to turn that down just a smidge into 45. That way it's a little smaller should fit inside the buttons better. This is something that you can tweak, but it's really cool that we can have one button here and we can have different things on all of them, so that actually fits. Looks really nice. We could only move the y position up just a little bit more. But doing that is just the design part which we don't need to worry about too much. Now we have those on the screen doing something interesting, so that gives us the buttons for our main menu. But you might be thinking, How do we actually do anything with those buttons and how do we distinguish between them? And that is what we're gonna figure out in the next video. I'm gonna show you how to set up a click event and distinguish between each button. Even though all three buttons are the same object, they have different instance properties that we can call upon. 52. 15.2 - Changing Rooms: So we've got the buttons on the screen now. We need to make them interactive. We're gonna add an event that is going to be a mouse event. There's gonna be a left down. So when the mouse is pressed, something was going to happen. The way we can figure out how we do and what we dio is based on their my text variable. Remember inside of here we've set my text to be something specific for each button. So inside of our event, we could have an if statement that says, If my text is equal to a new game now, this is a little dangerous simply because you have to be sure that this is spelled exactly the same. If it's different at all of his different space, if it's different capitalisation, it will not be equal. So doing this is, I think, easier. But it can also lead to more errors. So make sure that when you put that in there, you might want to actually copy this over and then paste it inside. Otherwise you might get some spelling errors. But if you do, you can figure out exactly why income and look here. That's part of why I really like setting up the variables like this as well is if you forget to set it inside of the creation code, you're gonna get an error you're gonna know right away. I forgot to set this up, and I know where to do that. So if my Texas equal to my game, we want to change the level. And how do we change levels? We haven't looked at this yet. It's actually really simple. A function called room. Go to and you pass in the room index or the room number, which is room level one. Now, if we click on that button, it will work. I press f five to run it and let's click on it and see what happens. Boom. Now we haven't set the boss to not spawn instantly, so you can see that. But we changed rooms. We are now being able to go to a different room and we can also say inside of here we can check out the other buttons. So if room is if my Texas equal to endless endless mode, I believe it. Waas, we're going to do something. If my Texas equal to credits we're going to send them to room credits. An endless mode is gonna be something that we're gonna work on it a little bit. It's basically the same game, but no boss, just seeing how high of a score that they can reach. So that gives us the buttons. Now, usually, when you have buttons, they need to have some kind of visual feedback that you are about to do something with them so we can actually go into the mouse and we can actually have an event called Mouse Enter and mouse leave, which is really great. So we're gonna add both of those right now and what we're gonna do is actually change the color that's being drawn so that we know we're actually over that button. So what we need to do here is now change. How were being drawn the color here? So we're actually gonna change the sea red to a variable called my Color And inside of here we're gonna set my color equal to see red by default. Now it's going to be different because whenever we come inside of here, we're gonna set my color equal to see blue or change it to something else. Then we're gonna change the color back when we leave that button and this will work for each button because each button is gonna take care of its own object code. So if I enter the new game, it knows them inside that button. If I come into endless mode, you can see here that now we're changing it and we can get a feel that we're actually gonna click on the button is actually active and work. So that's awesome. It allows us to transition rooms. It allows us toe have objects that we can change their creation code toe, have multiple instances, but have them be entirely unique and do something based on their uniqueness of the creation code that we set up inside of them. Now, in the next video, we're gonna set up menu music, and I'm gonna show you how to do a credit screen kind of have a text crow if you want to do it that way, 53. 15.3 - Credits Room: okay, instead of our main menu, we want to have a creation code for our room so that it starts playing music just like we did in room level one. And speaking of room level one, we should probably take that out because inside of here, we don't actually want to play it there. So I'm gonna cut that out, and instead I'm gonna put that in the main menu because otherwise it would start playing the music twice, which is not what we would want it to do it all. So now the main menu is going to have that music start and the main menu. The music is actually going to continue even if we change rooms. So if I click on New game, the musical continue, So that's OK is gonna keep going even if the room is outside of where we currently are. So that gives us the main menu, the buttons on it and the music. Now I want to look at the room credits for the quickly something we need to do first inside of room credits is changed. The enable view ports and make it visible for zero and change this to be the same. So 12 80 by 7 20 Now to have a credits menu. There are a lot of different options that you is the designer. Have you could have them, Uh, just have the text crawl. You could have them appear. You could have them fly. You could have them play a little mini game where the credits appear over time. Or like they unlock them, you could have the credits room appear at the end of the game at the beginning of the game . All of these things are things that you get to decide and put in the way you want. I'm going to show you how to do a kind of credits crawl so that you can put your information and that kind of comes down in a fancy looking way. We're going to create an object, and that's going to be a O. B. J credits object. It doesn't need to have a sprite, but we do need to give it a variable. That variable is going to be scroll amount. Then we have set a equal to zero for right now. And then inside, we're gonna add a new event going at a draw event, We're going to draw the text on the screen and inside of here. You do not want to draw self. If you try and do this, you're going to get an error because it doesn't have a sprite. It can't draw itself. So the only time you don't want to use draw self is if you have no sprite to draw because you'll actually get an error. So what we're gonna do is we're gonna draw the text, and we're gonna put whatever text we want in here. Now it's going to start at the top. So we're gonna say it's gonna start a 25. So a little bit in from the left side of the screen and the Y position is actually gonna be negative. 15. So it's gonna come down from the very top of the screen and the text. This is where you can put it inside of a variable if you want, or you can just type it out right here. So we're gonna say, made by your name here. So that is the text that's going to scroll down. And I would encourage you to put your name right there Now the way this is gonna work is by using this scroll amount variable, because right now it's only gonna draw the text at 25. Negative 15. Not much is gonna happen. So instead, we're gonna go inside of here. We're actually gonna add scroll amount, and that might look a little weird because scroll amount is zero. But if we come in here and we say scroll amount plus equal five all of a sudden now school amount is increasing, so the Y value is going to increase, which is going to bring it down. We wanna have one if statement at the bottom that says if scroll amount is greater than room hate plus 15 room go to room main menu. Now, this plus 15 is this offset that we have right here. But with those two things right there were now drawing the text is actually gonna move from the top of the screen to the bottom of the screen. We also want to set the front so draw set font, and we'll do font menu. We want this to be large, will draw set the color as well. We'll put a green there since we don't have anything like that yet. So we have all of this set up. The last thing we need to do is actually put this object inside of room credits. Otherwise nothing is gonna happen. So if we run this by pressing F five, we can click on credits and go into the credits room and see that text crawl. Okay, that's really quick. It takes us back. Why don't we adjust that O B J credits scroll amount? It will put that down to two. We can run that again and you can see that this is one way you can display all of text. And you would just have a different if statement here something like if all of the text has been shown on the screen, then go ahead and go back to this room. But that gives you a credit. Now, you may also be noticing something here that is important. Every time we go to our main menu room, the creation code is going to fire and and music, as we've talked about before, continues even though the room is not in view anymore. So we need to add if statement here because we don't want the music playing again and again because it gets louder and louder. It compounds upon itself. So we want to say, if audio is playing, we're gonna pass in that sound background music, and we're going to say, if it's false, that we want to start it. But if it's true, don't start it that way. This music will only begin once, and it's not gonna compound. It's not gonna grow louder. It's not gonna get out of control. So we go to credits again. You could hear the background music plane we get to the bottom of credits and music doesn't start again. So that gives us a credits room that allows you to put your name into the game because you definitely deserve it. And the next thing we're going to dio is gonna be the endless mode. So inside of here, I'm gonna show you how to change just a couple of things so that you can have a endless mode where the boss doesn't come out and the player can keep on planning to see how high of a score that they can get inside of your game. 54. 15.4 - Endless Mode + Levels: So now we're gonna set up an endless mode for our player. However, when we set up endless mode, I'm actually gonna show you how to set up levels in general. What we're gonna be doing in this video is actually generating away. It's gonna create a little bit of complexity, but we're gonna be able to then have the player click on any number, any level, continue whatever, and you will be able to spawn them into that level. That way they can play through your game. Or if you had, like, a numbered system and they could tell like they wanted to replay level two, they cook onto it goes directly to that. That will be OK. The way that's gonna work is gonna be setting up a global variable in O B. J system. We're gonna call this global dot current level. We're gonna set this equal to zero. Now we're gonna define zero as endless mode and level one as everything else and moving on and so forth. So we have a BJ level one, and that's what we've been playing with. That's what, uh, we set the global Max score inside of here for when the box to come out. This is what we've been doing for the most part. Now we're gonna change that up just a little bit, and we're gonna make a new objects and we're actually gonna duplicate O b J level one and I'm going to click right click on that and duplicate and call this O B. J Endless mode. And now I'm gonna open that up because we're gonna have a lot of the same stuff. We are going to get rid of a few things, though, So I'm gonna comment out of the global dot max score because we don't want to give it a max score anymore. We also don't have a level beaten, and we also not gonna use alarm one, so we can actually delete alarm. One says that is bringing in the boss and then an alarm zero correction. Take out this if statement because we want the alarm to trigger forever. We never wanted to stop Now is open up over E. J button and we're gonna set over J button and endless mode. We're gonna say global Dr. Current level is equal to zero. We're gonna say room, go to room level one. When we click on new game, we're actually gonna set global doctor current level as well. We're gonna set that equal to one, and then we're gonna go into our room, level one and we're actually gonna delete O B J level one because we are not gonna be dynamically spawning the current level, the players going to play through when this level loads. So really, we could also rename this room level. It doesn't need to have level one. Of course, if we rename it, then we have to change all of the room go to so we're not gonna do that right now. But basically, room level one is now going to be where they fight all the enemies from now on. If you wanted to change the background, if you wanted to have a unique room for every single level you definitely can for the trial version of gamemaker studio to your limited on objects and levels. So I wouldn't recommend doing that unless you're planning on buying for the system we're using. Currently, you will also run out of space when it comes toe objects because you'll only be able tohave . I think 15 objects, so that would limit the number of rooms you, the number of objects that you're allowed to have, which is then the levels. But if you created 10 B J level object, you could have an if statement inside of here saying, If current global duck current level is equal to zero, set it up just like this. If it's equal toe one, set it up like this, it's a little messier. It puts a lot of code inside of one object which I wouldn't recommend doing. It makes debugging very difficult, but if you're making A if you definitely don't want to buy it. If you just want to make your game the trial version, you can do it that way, and that would work. Great. Now, inside of endless mode, we've got this set up just the way we wanted. So we need to set up now inside of O B J system. What happens and how we actually spawn those things in the in the level, we're gonna add an event. This is going to be an alarm event because we just wanted to trigger once it was a spawn level. Objects, we're gonna have a couple of statements in here. It's gonna say if global dot current level is equal to zero, then this is endless mode. So we're gonna say instance, create layer. We're going to say 00 And now this is where things get a little confusing. If you just say layer, it's not going toe work. The reason for that is that our O. B J system is a persistent object, and the way gamemaker studio handles persistent objects is a little confusing. But basically they start on the instance layer where they are, which right here it's over Jay system on this instance layer. But when we change levels, if we go into room level one while we're playing the game, O B J system object does not end up on this instances layer, it ends up on a unique, persistent layer that you cannot add objects to. So if we try to say, add this created object on layer, we're going to get an error message in the D bugger that says specified Layer does not exist and the object will just not be made. So we're going to be spawning this once we're inside room level one and in room level one, we have a layer called instances. So we're going to spawn there specifically, So inside of quotations, I'm going to say instances and this needs to be spelled just like that so you can rename this layer. If you wanted to be easier to be spelled, that would be fine. I'm gonna keep it just that way, and I'm going to say we're going to spawn. Oh, BJ endless mode. Now down here, I'm gonna say else if global dot current levels equal to one same kind of thing instance create layer 00 inside of instances and this is gonna b o b j level one. So how do we trigger this alarm? Because we don't want it to be going all the time. We only wanted to trigger once we actually inside of room level one and there's an event for that. Inside of other, there is a room start in a room end, so I'm gonna say room start event and I'm going to say if room is equal to room level one Alarm zero is equal to one. So whenever a room begins, this event triggers all objects. Have this and We're pulling this into the open J system because it's a persistent object. We only want this alarm to trigger when we're in the right room and we don't want it to be running all of the time. So this will allow it to run just one time and allow it to run right when we're in the correct room. So let's press f five. Let's run this and see if our system is working properly. We should be able to click on endless mode. I have have you spotted and they will continue spotting forever. Yeah, Now let's exit that press F five and run new game. We should have the boss immediately appear because we haven't changed those settings just yet. Okay, so now we have different levels based on the buttons that we are clicking, which is awesome because now we can have unlimited levels. We can have a continue based on the current level that the player has, and that is how you would do this insight of this method. There are a lot of other methods, but this one, I think, is simple. It's straightforward, and now you can create different level objects and just spawned those in and then have the player play through unique levels but still only have one room. Keep it nice and simple. And that is actually everything we need to do if we jump in tow. BJ system the create event and we set the players score equal to zero and we go into over J Level one and we've set this Max score, maybe to something higher, like 250. This will give us an entire game that we now can play. This is a working fun space shooter with a goal with a score with a boss, and it all works great and you are just about done. The game is done as it is right now, but there are a few more things that we can add to it. And there's one more major component of programming that I want to introduce you before I set you wild in the programming and game making world and that is a raise. So the next couple of sections I'm going to be talking about some extra functionality that we can add to our game, including game controller support and a raise that you can use in your game and really in programming in general that are essential once you begin growing and building larger games . So let's jump into those in the next section and I will see you there. 55. 16.1 - Arrays and Choosing A ship: alright, in this section, we're gonna be looking at a rays which are really the building block of anything you want to do. That is, larger projects a raise are essential in every programming language that you're going to use any games that you make your going to need a raise because they are so important and so flexible. But what are they exactly? Well, there are several different kinds of a raise, and depending on the language or using, it can be very strict. Or it can be be very easy and free flowing. Now gamemaker leans on the side of easier. So when I talk about a raise, it's really more of a ray lists, not the traditional race, but the difference isn't very important. Basically, what an array is is a list that you can put data into and you can access by an index, which is just a number. So here they visualize an array going top to bottom. Normally, I like to visualize it from left to right, but it's the exact same thing. You have a box that you can put some data into here. They have numbers, integers, strings and then values. Whatever it doesn't matter. You put some form of data into the box, and then you can add more. And that is going to be what's called a contiguous section of data. So it's going to be accessed just like that. So if you store, let's say, um, different inventory items inside of an array, you would you would name it any kind of variable name you want, like player inventory, and then you would access it just like this. With the brackets, he would say Player inventory zero, and then it would pop out whatever that is, that's how you assign it. And that's also how you get data from it. So I'm gonna be showing you some way that we can use the arrays inside of our space game. That is actually pretty cool. What we're gonna do is set up a system that allows the player to choose from different sprites, that they can choose to be their ship, and we're gonna do that by storing different values inside of an array, accessing it and then saving that back to our player ship. So let's jump into that First thing we're gonna do is load up all the different sprites we're gonna need. So I'm actually gonna make a folder here so that we could be a little more organized. I'm gonna say add group, and this is going to be, uh, player ships. So I'm gonna put in the SPR green ship in there. I'm going to create a couple sprites on the import one. Now, these are gonna be in your resource is folder in Kenny Space Shooter under PNG. Now, you can choose any of these because the code that we're using for the most part looks at the actual sprite itself to go from left to right up and down to keep your spray inside. Where should be so I think you can choose any of them. But you may have to do a little bit of tweaking here and there. We'll try it out and see how it goes. So we'll do a blue ship of this design. I'm going to say yes, and then it's already set up. But we want to make sure that it's gonna be middle center right there and we'll give it a name. Over here will say SPR player ship blue and we'll bring in a few more. So we're gonna do a orange one. I don't want to see that message again. Middle center, this PR player, ship orange and now they don't have to have the same names. But I'm just doing that because it makes the most sense to me. And when I'm typing them out, I'll be able to fight them a lot easier. Now, do this red one middle center and name it. All right. That gives us four different options for our player ship. Now, when we're shooting the lasers, it's possible they might come out looking a little different, depending on where we do that. Not just a little bit tweaking that you might have to do later on. So we've got that group, which is great. Now we're going to go into R. O B J System and we're going to make a global variable. This is gonna be the variable that we assigned the Sprite that the player chose. And then we can access that in different objects that we're gonna need. It was a global dot player ship. Sprite equals SPR player ship green. So this is the default choice, and you can change that default if you wanted to, that would be totally up to you. For now, we're gonna do green, because that's actually what we've been doing. And that's the default that we've used to so far. So what we want to do is use this global variable in all the places that we assigned the Sprite to our player ship and one of the first places that we do that is the inside of the great instance here. So the first time that we make our ship, we're actually going to set the sprite that we've chosen. So we're going to say, um, that's right. Index equals global dot And then you can see it comes up here player ship spray, so set spray to chosen by player. And then we want to come in to the animation end because inside of here, after we die and explode, we're sending our Sprite index directly Teoh the green ship. And if we do that, then after we die the first time will no longer have the sprite that we chose before. So we actually want to change this to setting it to what we have chosen, which is gonna be global. The player ship spray. So with that now, once we can actually choose will be able to respond and start with the ship that we've chosen, which is awesome. Next, we need to make another object. We're gonna create an object. I'm gonna call this over J selector. And for this sprite, we're gonna go ahead and, uh great. A new sprite. This isn't gonna be pretty. I'm just gonna come in here and draw a quick arrow. If you have, like, an arrow online, you can use that. You can use anything you want. I'm just gonna make this really simple. So I'm going to pick green, make it a little bit larger of size and bring it up and make it look somewhat. That's a narrow. I'd buy that. That's a narrow. So we'll name that s p r. Arrow. And we only need one, and I'll show you a little trick that we can do with that. Just make sure that you middle center that and save it, and we're all good to go there back to our object. So inside of our object, what we want to do next, we're gonna make a creed event with commenters set up variables. Now we're gonna make a variable called my direction and we're gonna be using this to kind of say which way it should be facing and also which way the array should go once we click on it. And we're gonna do a little trick that we just need one arrow for this. Now, we're gonna set this in the creation code. So if it's not set properly, then I wanted to give us an error, so I'm actually gonna say that is equal toe undefined. Next we're gonna choose. We're gonna set all the different sprites, all the different ships that the player can choose from in the beginning of the game. And the way you do this is by assigning them to the array and what we're gonna do is make a global variable, and then that way we can access it anywhere that we need to. So we're gonna create a ray here, and the way you do that is by giving it a variable name, just like we've always done. But then we're gonna open the bracket, put zero in there, close it and then say equals a real say spr clear ship green. So This is the first index of the array. Now the terminology is kind of important because if you ever go to a help form or anything like that, you'll see Index. That's just how it's called. So it's the index, what we're talking about and this is Index zero a raise start at zero. So we're gonna fill this up with all the different options that we want. Someone saying my ships one is equal to SPR player ship From here, it's really up to you. You can have a specific order if you want, or you can just put them in randomly. I'm just going to put them in randomly. What? Everyone comes up first. So I got blue Player, ship orange, this PR player ship red. So that gives us all of those. Now, we are also going to come down here and make one more variable, and this is going to be called current ship choice, and this will be equal to zero. We need this because as we are going through the array, we need to know which one were on so that we know which one to display. And now we're gonna go into our main menu. We're gonna put in the selector objects here, you know, throwing two of them. I mean, a double click on this one, and I'm gonna flip the y value. That way, it turns upside down. Now, we also need to indicate which direction these are for when we're actually clicking on them . So right now we've got this one being up and this one being down, someone to go to the creation code. So I'm gonna say my direction is equal to up. Save that. And then we're gonna come over here and I'm going to edit this creation code and say my direction is able to down and say that. Okay, that gives us those right there. And we're actually gonna use these to draw the ship that's being selected in the middle. But we only want one of them to be doing the drawing, because if they both are, it's unnecessary, can computation power. And it causes some errors when you're, like choosing different ships. So back to our workspace, we're gonna add a drop vent, and inside here, we're gonna draw the shipping chosen. First, we're gonna draw self as you should always do when you're taking over the Dr Vint at least most of the time. Now we're gonna say if my direction is equal to up, this is clearly my choice. You could have it be equal to down if you want. Really? I just want only one of the OBE J selectors to be drawing at all. So the up one will be doing. The drugs were going to say draw Sprite. This is gonna be global, and my ships curry ship choice. And this is something that is this is the variable that we're actually going to change that will rotate through the array. So we're drawing the one that we've currently chosen, and sub image is going to be zero, and then we're going to draw it BB box left, plus 32 And bring this out a little bit. And B box bottom plus 1 50 And this is these air specifically to just draw underneath the arrow. So if you were having the bottom arrow draw or you had them in a different location or whatever, you might need to change these values. But for the most part, this should draw the ship right underneath it. And if we actually press play right now, we should be able to see it drawing the first ship. Okay, so it's right there this era. We could move down a little bit, have it look a little nicer. Bring it down to and over one. I had that off and press play, and there we go. So right now, there's no functionality, but that's what we'll add next. So inside of here, we're going to add a another event. This is going to be a mouse event and specifically the left pressed. You can do right Prester middle press if you want left press is just the default when you like choosing things but not left down. Otherwise it will cycle through the array incredibly quickly. That's not what you want. Left press will fire win. It's when the left mouse button is pressed once and then not fire again until it's lifted up and pressed again. So instead of here, we're gonna change this to change chosen ship. Now, if my direction is equal to up, we're gonna do, one thing goes, my direction is equal to down. We're gonna do something else Now What we're gonna do is visually we are going to cycle through the array, remember that. It's it's kind of like a box. And they had it going down from they had to going vertically. So imagine a vertical box. We're starting at zero. If you want to go down. If you I guess if you want to increase the index, you are going down. So you're going from zero toe one. And if you want to go up, you're going from 1 to 0. And when you're doing that, that means that you want to actually be got that you want to actually be increasing and decreasing the number that is currently the index. Now, the index variable that we're using is current ship choice. So we're gonna start with down because I think that makes the most sense. So when we're going down the array, we actually want to increase the variable we're gonna be using now. One little tricky thing since we're using one object here and we're not using the global variable, we need to say over j selector dot current ship choice is what we're increasing. That will actually take all of the LBJ selectors and increase them. And the reason for that is because we have two of them. So inside of our main menu here, we've got 20 b J selectors and they each have all of the data that the other one has. So we could increase the variable specifically to this selector or we could increase it directly to this one. And if we do that, it's going to cost some issues. Now the work around for that is saying this right here, just on the down section because the down section is not what's being drawn. So we have to change that. But for the up arrow, we could actually just say minus minus current ship choice. And now if we run this, it'll actually work pretty much the way we want my press down. You can change them. If I press up, it'll change, Mac. And if we start on the ship, it won't do it yet because we haven't assigned it. But that's just one line of code. Now you may have already run into a problem, but don't worry. I didn't show it intentionally so we can scroll through here. But if you try to go outside of the array, it's called indexing something out of bounds, you're going to get in there. So it's because they push execution error variable index out of range. So the way to read this error and it is important to read them, because when you start working with a raise, you'll probably end up with this error quite often. So it's a variable index zero negative one. So that means that you are attempting to access negative one when zero is the lowest limit that you're supposed to go to. So it's out of range on the variable my ships. So we are saying, if we press that up Arrow minus the current choice but that current choices saying right here, draw the ships of whatever current choices and are a only go 0 to 3. So if we try to access negative one or put a four in here when we're trying to draw something total crash, complete failure. So to do that, we have to have a couple checks and balances in here. It's not very many. It's actually pretty simple if I come in here and I just say if current ship choice is less than zero, so this will check the value of it before it gets to the drop vent, which is just fine. And here you can choose as the designer to do two different things. You can choose to go to the top of the array like you are circling around a list, and that's usually how I like to do it. And I'll show you how to do that right here. If I say current ship choice equals, there's gonna be a new function. Is gonna be array length one d My ships subtract one. Uh, what this function does is it gets the the length of an array that is one dimension. That's what this means. So there are such things as two D arrays, which we're not gonna talk on, but basically they are just an array of a raise. So, basically like a grid, you can put anything in there. They're more complicated, but of course, they have more power. What we need is just a one dimensional array, one row, get the length of that. But it returns the length of the array inhuman counting, which kind of bugs me. So if we look at this, we have four. That's the length of four, right? 1234 But toe access the ray. You have to start counting at zero. So Gamemaker decided to return the number of that humans would expect. This is a length of four. But if you try to plug the length of the array into the array, you're gonna get air because four is not a valid index. So we have to subtract one immediately from that. Hopefully that makes sense in context. This is all you need to cycle through the array. You're basically saying, if you try to go below what we have, I'm just gonna put you at the top. It's gonna go through a loop and you'll never be able to break my array. The other way would just be to say equals zero, so this would just keep them at the bottom of the list. You could never cycle through it, and you could never go out of bounds. Either. One works, and it's purely from a design decision what you choose to do. So if I run this by pressing on that up era, we should never be able to break our system. We should be able to scroll right through it now pressing down We can still break it. So let's fix that. We're gonna see if o b day selector dot turned ship choice is greater than or equal to. We're going to use this array function again. You're gonna see it a lot. Array length one d my ships. And this time we don't need to say minus one because we're actually saying greater than or equal to. So if it is four or greater than we know, we've already gone too far. We're gonna say, Oh, BJ selector dot current ship choice is equal to zero. So if we run that now, we should not be able to break the array at all. You can scroll up and you can scroll down and go straight through those just fine. The last thing we have to do is assigned the sprink that we have chosen to the overall ships Bright. We do that by saying global player ship Sprite equals my ships. Great ship choice. We run this and now you can choose from all of those ships in there so we can scroll through. I'll choose the red one and now we've got a red ship there If I die, I come back as the red ship and you can see that for the most part, it maintains the boundaries. The thrusters on the up don't make much sense anymore. But, hey, that's something you can deal with. And everything works really well. So that is a raise in a nutshell. They are extremely versatile, and you'll find yourself using them all the time. This array length is an extremely useful function that you'll want toe memorize how to use . Make sure you use the minus one when you're plugging it right back in there. Um, but really, you're gonna use these just in every program in every game of any good size. And the reason for that is that raise a race can hold any kind of data, and it doesn't matter what you put in. There they are. What is dynamic? They allow you to fill up data without having to know the specific variable name. This is how you're going to do inventories. Party systems experience, right? You can't name. You can't give a name variable toe, every single piece of data that your game is going to have. It's just not possible because a lot of it is generated on the fly. So instead you might have an array that says player inventory. And at any time a player gains a new item, you just say, added to this array, put it in the right spot and then you have access to it. A raise and four loops are so powerful we're not gonna get into that right here because it's a little more advanced. But a raise and four loops allow you to do so many things here. You can see that we've got a different selection of ships by just a one dimensional ray with just a little bit of code in here, you can add in more ships you could add in any kind of sprite you want here, and then all you have to do is adjust for inside of the player ship. If on the Z key here, if you had a totally different looking sprite, you could just say If Sprite Index is equal to weird ship, then you instance create these not an ex and why, but instead at different locations X and Y over here and over there, so you can do whatever you want with a raise, and that's really cool. It's really powerful. If you have any questions, though, feel free to shout out below. I'll do my best to answer them. A raise are definitely confusing because they're not as straightforward as variable. You've got indexes, and you've got different kinds of data types, which can really mess you up when you start using a raise frequently. If you don't, I know that you're gonna get a sprayed in here. For some reason, you put a string in here and you try to assign the string hi to your Sprite Index. That will be a problem, but for the most part you want to just Philip in array with similar kinds of data. And then you can use it however you need to, which is awesome. So that's what I've got for you. Want a raise and that's it. For now, 56. 17.1 - Controller Support - Part 1: So in this section we're gonna be setting up game pad or controller support for our game so that you can fly around the ship and shoot with a controller instead of just using the keyboard. Now, any modern game controller is going to be supported by gamemaker studio. And for the most part, if you have a control that could be plugged in and recognized by windows than gamemaker, studio should also recognize it. If it's not like an Xbox or PS four or a controller that looks fairly similar to this. With all of the buttons, you might have a little bit of difficulty setting it up, which we're not going to cover here. But if you do have, like, an Xbox 13 60 ps four ps three controller, this should work just fine. So it has list of all the functions that we're gonna need down here. We're just going to use a few of these and then all of the Constance that relate to what buttons They actually are on here. So with that information, we're going to start by setting up movement for our character. So instead of a game, we're actually gonna go into our step event and inside of here is where pretty much all of the code is going to be. And that's because there are no events for specifically game pad functionality. You've got keyboard and mouse, and you have gesture controls for touchscreens. But you don't have the events for game pad support, so we just have to do them all inside of the step event, which is fine. It's actually not going to be too difficult once we jump into it. So the first thing we need to do is say, if game pad is connected. So this is a function that just literally checks to see is their game pad connected? And this is important because if it is connected, we can start using it. If it's not, then we won't even try to do anything inside of this if statement. So depending on how you want to make your game, you could set it so that if the game pad is connected, all of these all the keyboard functions no longer work properly. You could do that by wrapping them. Just saying if game pad is connected equals false, then you can wrap all of that inside of there, so this will only work. This will only work when there is no game pad connected. If you wanted it to be one or the other, that is one way to do it. I'm not gonna worry about that because I'm just going to say if we have a game pad connected will use that. If not, then we'll use the keyboard and we won't have to worry about that. So I'm just going to say if the game pad is connected and the device we're gonna use is gonna be zero Now, most of the time, when you connect a controller to gamemaker studio, it's going to pick it up as zero as the default device. If you're using a non modern controller, it's possible it will pick it up as a different device. And if that's the case, you'll need to do a little bit of searching and discovery to see which device it's actually connecting it as and then plug that inside of here. And if you have multiple controller support, which we're not going to touch on right now, because that's a little too advanced. But if you have multiple controllers support, then you would need to keep track of which controller is is designated to which player, and then you can move that specific play around with that specific controller. So with that being said, let's start coating the actual movement because it really is not too difficult. There are just a couple of variables that we need to have and just two functions to three functions that we need. So the first thing is gonna be var left joystick H that we're also gonna do far left Joystick V. Now the H stands for horizontal and the V stands for vertical. And that's because we're just using the left stick to move the player around. But we need to get to the actual axis of that joystick, whether it's going up or down or making it a local variable. That way, it doesnt stick around. That way, if you disconnect the controller, it disappears. We don't have to worry about the extra memory associated with that, so we're gonna get a value back from a function. The function we're going to get back is game pad access value, kind of like what I just said. So we're gonna get the access value of the left joystick, although we have to specify which one we want. So the device is gonna be the exact same as the device here. And you can see here that if we have multiple game pads than you would just need to have a variable that says player one or whatever players currently selected. And you just replace the zeros with that that variable that you've got. So the access index here, we need to use a constant. We're gonna use GP access, and there are four options to choose from. Left, horizontal left, vertical, right, horizontal and right vertical. So we're just using the left stick, and we want the horizontal for this variable. There were into the exact same thing over here, but just change that last letter. So we want vertical on the vertical and horizontal on the horizontal, which makes sense. Now we're gonna add another set of variables here called far H speed and of our V speed, and that stands for a horizontal speed and vertical speed. And this is going to be a little different in the movement that we did with the keyboard. Because with the keyboard, when we press. W we know we want to move up. There's no question about that. We already know which direction we want to be doing and which coordinate off of the X and y that we want to either add to or subtract from with a game pad, though it's not the same. You've got a joystick here that could move 360 degrees, and it can produce a value that it can't tell you exactly how much it's moving and also which direction it's moving. So this completely opens up free for movement, which we don't have on a keyboard. Because if we press d, we go to the right. If we hold the left joystick to the right with an up a little bit, we need that to go up and to the right and less. Of course, you wanted to simulate keyboard movement. If that was the case, then you would probably do something like simulating these key presses, which were actually going to do. But you probably similar to those key presses based on the value that this function returns right here, we're gonna look at this value in just a little bit. But for Now we're going to say we want free for movement that is much more dynamic than the keyboard can allow. So the H speed is actually gonna be our fly speed multiplied by our left joystick each and this down here is gonna be fly speed multiplied by left joystick vertical. So the reason for this is these values right here can actually be positive or negative. So let's look at that. I'm gonna show debug message left joystick. I'm gonna tape in left. H I'm going to say plus strings so that that way we can put this value in there and it will show up. So I'm gonna show debug messages just that you can actually see the values that we're getting back, and they make a little more sense to you. So we've got that. So right now we're not actually gonna move around because we're not changing the x or Y according it. But that's okay. I'm gonna go ahead and run the game and look at the debug messages that will appear down here and show you the values. That way, while you're coding and makes a little more sense because you can copy this but controller support gets a little confusing on the values that you return. So if I hold up on the left joystick just straight out, you can see here that left V becomes negative one. If I let go, it becomes 0.6 If I go down, it becomes one. So it's a value between one and negative one on. The exact same thing is true of the left. Going to the left produces a negative result. Going to the right produces a positive result. So because of that information, we can actually do this equation right here. And it's going toe work because this is ranging from negative to positive. So for our horizontal speed, if we're trying to go to the left inside of our room, that means we want to be going to this side of the screen, and our X coordinate is lowering over here. And that means that even over here when we assign HB two fly speed, which is 10 by default, two are then multiplied by left joystick. If this value is a negative, this is going to be a negative value. So when we come down here and we say our X plus equals h speed and why plus equals v speed . All of a sudden, that's okay, because fly speed is not always going to be positive. H speed V speed can move between positive and negative because they're being multiplied by the value of negative 1 to 1. That also means that our fly speed and R H speed and V speed is going to decrease an increase based on how much we push on that stick. So if I press f five and I run this now, I'm gonna be able to inch forward little by little with my player ship. You see, I go quick. I could go very slowly, but I could move completely around free for moved. Now, you'll also notice that you could go off screen and you also drift just a little bit, depending on how New Year patrollers reason for that is values you're looking at over here in the d bugger, I see vertical of 0.6 horizontal 0.10. And that's because the Axis values are never gonna be perfectly zero. It's a mechanical joystick that after some use, it doesn't stay perfectly in the middle. But there's a function for that. So I'm gonna take off this show debug messages, and the last thing we're gonna do is fix that drift. And inside of our create event, it's actually just one function. It's called game pad Set access Dead zone. The device is gonna be zero. And the dead zone is How much Mawr does the player have to push beyond this to start actually registering that the joystick is moving. So the values that we had were point of six and 60.10 when it was sitting still. So if we wanted to be safe, we could say that dead zone was around 0.15 or even point to that way. Any values on the joystick below point to it's not going to register his actual human input . So if we press that five to run, that the drift that we were experiencing will be gone completely, you have to move the joystick a little more than we did before to move. But not a whole lot in eliminates that drift, which is important in the next section. We're gonna look at how to set up the limits that we had and also have to fire our laser from parking 57. 17.2 - Controller Support - Part 2: inside of the last video, we got movement set up using a joystick. But we were allowed to free roam around the entire game even outside of the boundaries. And we couldn't shoot lasers yet. So not everything was working perfectly. So now in this video, we're gonna set up lasers, the movement and also the flames. That way they show up even when we're using our game. That so the first thing we're gonna do is work on firing lasers. So right below this, we're going to see fire lasers. And this is actually very similar to keyboard input because we're actually gonna be simulating a key press. So we're gonna say, if game pad So this number checking game Pattinson of keyboard button check pressed se zero once again for the device and now the button index. If you look at the help menu, you can see all of the constants and where they go. And it's a very unfortunate naming, in my opinion, that the trigger is R B because that looks like for right, bumper or left bumper. But it's not, so we need to actually say GP shoulder RB If we want to do right trigger. Now, as the programmer, you can set up this button wherever you want. If you want GP face one, which on the Xbox isn't a then go for it. If you've got a different controller than set up, whatever makes sense. If you're left handed, then I guess he was the left trigger, it doesn't matter. You can see that all of them are right here with actual descriptions of where they go right up here so you can choose whichever button you want and set that up. I'm going to be using the right trigger. So let's put that inside of here. So it's gonna be GP shoulder our beat. We'll see if that button is pressed. We're gonna simulate are key press of Z because we already have all of the code inside of the We don't need to copy and paste this because we can just simulate the key press. To do that, we just use the function keyboard key press, and we're gonna say cord Z, so this will simulate the press of Z. Now there's a caveat here. We could simulate that, but this is actually kind of like pushing down the key and it's holding it down. So if you run this, you're gonna fire one time and that's it. Which isn't really what we want. We needed toe also simulate a key release of the exact same keep. By doing that, we simulate the key press of both pressing it down and releasing it. So let's look at that firings working. It's just like the presidency on the dribbler. Exact same thing, which is great. So later on, when we set up power ups for our guy for our spaceship will be able to put all of that inside of Z. And it'll still work exactly the same no matter where it's being called, because it's just simulating that key press of Z. So now let's set up the flames because that's actually a fairly easy thing to do. Someone would comment, and we're going to say if we're gonna check two things, we're gonna check the joystick going up and the joystick going down. How much is it going up? How much is it going down? And if it is, then we want to draw those flames Now, In the last video, we actually looked at the left joystick H and the V values. So they go from negative one toe, one on both the horizontal and the vertical. Now, the one we care about is going to be the verticals. We're gonna say if left joystick vertical. Now, this needs to be going up or down, depending on which flame we want to draw. So when we are going down, the left joystick is actually at one. Now, that means that we want to check if it's greater than a number. Now you can put in zero here, and that basically means that as soon as your past the dead zone of point to then you are going down and those flames will appear. And I think that actually looks really nice. So let's put that in there. And all we have to do is set our key down variable, just like we're doing inside of here. We just have to set it to the correct one. So inside of down, we actually have this key down equals down. So we're gonna basically put that same thing in there. So if it's greater than zero, we're going down. We'll put else if left Joystick V is less than zero down equals up. And if we run that, we're gonna have the flame show up and look great. They are moving side to side. They don't show up moving up and down they here. That's exactly what we want. If you want them to appear in a different value here, just change that 2.254 point five that whether going straight up, straight down, whatever looks good to you that will work exactly the way it is supposed to. Okay, on to the tricky part, Keep ship building boundaries. Now, this part is tricky because we need to change the logic that we're using to keep our ship in the room because of how we're actually moving the ship around. We're not just adding X and y based on key presses. This time we actually increasing X and Y based on H speed and V speed, which gives us that freeform movement that we set up in the last video. So to organize this correctly, we need to bring in the logic for the key presses, but change it a little bit and see which direction the player is attempting to move the joystick and make it so they can't move outside the boundaries. So the first thing we're gonna do is take the X and Y who actually gonna cut that out, and we're gonna put it as the very bottom. This is gonna be the very last thing that are inside of this. If statement checks, the reason for that is we're gonna alter the H speed and V speed depending on where the player is. Apt. So let's start with the left. So I'm going to copy the if statement right here and put in that there's copy and pasted inside of there. Now, we can't just simulate the key left press because we don't want to limit the joystick movement like we had, uh, in the last video. We wanted to be free form. And that means we need to just take this check and see where we're at with our movement in Rh speeding v speed. So what we need to do is alter this if statement a little bit. So currently, when we go into the left were saying as long as we are not on the left boundary, keep moving the X like you can move fly speed, which is totally fine. And that makes sense for a keyboard but not gonna work for the game bad. So what we need to do is actually say if it is less than or equal to zero. So if we are on the left boundary of our room and we're right about here in our exposition minus sprite, the X offset is less than equal to zero. Which would mean that we're outside the room. We no longer want h speed toe work, right? We're on the left border. We don't want to be able to go anymore left. So if we run this and check it out, it's going to keep us from going left. But then we're gonna have a problem. We left quarter. We can't go left. But we also can't go right. And that's because the H speed controls left and right. So to fix that problem, we need to be checking specifically If we're trying to go left, don't let the H speed be a