Absolute Beginner's Guide Part 7 - More Events!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] i hope so far you're following along and you've got your project up and running what we're going to tackle today is adding in a few more events to allow us to actually control our object and our player in our game to be able to move them around at will right now they just move off in a direction and we have no control of that besides the code we've put in so today we're going to actually tackle putting in some controls so that we can control our player let's go ahead and jump right into it so what we need is a few more events remember that events are registered by game maker when they have code inside of them and there are dozens of different events that we can have all we have right now is this create event the create event triggers one time and in it we have set our speed and our direction so it goes well right now it's going to go to the left at a speed of 50. i'm going to bring this speed down we're going to put in 2.5 so it's going to bring it down significantly and the direction well we can just leave that the way it is right now because we're going to actually change the direction when we press a key so let's go into the add event and what we want to do here is specifically we want to have the key pressed event so this event will trigger just one time when the key is registered being clicked if we held it down it doesn't do anything if we release it it doesn't do anything there are events for those as well key up and key down but key press just triggers one time the reason we want to use key pressed in this case is we're going to change the direction of our player when we press that key but if we want to be able to move quickly around go left right up down super super fast we want to only register when that key is pressed if it was being registered the whole time that the key was held down we would have an issue where we're going to the right and we press down and we might still have our finger on that right key as we're lifting it up but because we have two fingers on the keys at the same time and we have an event that's trying to register both of them it can only pick one and you may not know which one it's gonna be so key pressed is what we want here and if you're a computer gamer you're probably familiar with the wasd keys which are normal movement keys in a lot of games so you can follow along with either the left right up and down which is for the arrow keys on your keyboard or you can go into letters down here and choose wasd just make sure that you're putting the correct code in the correct letter otherwise it's not going to work now game maker has a way of creating code for one key and then copying it over for other keys but it involves using a function with some arguments and those are things we haven't touched on yet so we're not gonna try and do that right now that i will cover later on because it's extremely useful to be able to code your movement for one and then have the player be able to use it for another if that's what they're more comfortable with or you can rebind keys like in games you've probably rebound keys before or you've seen it where you can just change what a key does in a game that's something super useful it's also something that you kind of add in later into the game i'm just talking about it because those are all things that game maker can do and you might be familiar with if you play pc gaming or even console gaming now they have remappable keys and buttons on consoles which is kind of cool but all of that aside let's go and we're going to do the key pressed left and this i'm going to put the description as move left so that's pretty easy because the key press is left and we know we're moving left now recall that in our game we have well apparently we have a lot of different players so i'm going to go ahead and delete several of these to do that down over here in the instance properties we can actually select them if you click on the first one hold shift and move down it will select everything in between that second one and the first one then you can press delete which is the d e l key on your keyboard or you can right click oh no you cannot right click on that but if you have it selected over here you can come in here and i think my right click might be broken what is going on here okay it works over here no okay so this is something new to me and look at that you learned something new every day uh you there's no right clicking on the objects in here and there's no right clicking on the instance layer properties so you will have to use the delete key so d e l stands for delete then i'm going to move the object player back into the middle of the room this is that's new to me i i've never tried i guess this is okay it's fascinating no right clicking on objects good to know you can double click and it will bring up a menu but in this menu you can't delete it so the point still stands okay player object in the room just one player object go back to our workspace i'm going to press f12 and it minimizes all the windows so we can just focus on what we're writing right here no because we were just talking about in the room here the direction is if you're going to the right it's zero if you're going up it's 90. if you're going to the left it's 180 and if you're going down it's 270. remember it's a circle around the player with direction starting at zero to the right and going up from there so with that knowledge in mind once again if we want to go to the left it is direction because all we need to do is change the direction the speed is set at a constant right now direction equals 180. this is how we move to the left or at least we tell it to start moving in that direction so now we have the left movement if i come into the create event and let's say we actually delete this direction line because we don't need it by default the direction is set at zero so it's going to move to the right and if we press left on the keyboard it'll change it to the left so let's go ahead and run the game and now we're moving to the right i changed the speed so we can see it and i'm going to press left and now we're moving to the left now nothing else works and if you press left again it doesn't change anything because we're already moving that way but you can see that now we have some control of our player so let's go ahead and add a few more events make sure they are key pressed we're going to add the key press right i'm going to add the key pressed up and i'm going to add the key pressed down now it opens new tabs in the object window for all of these events so you can click on a tab to move between it or you can double click on an event right here remember if you don't like double clicking on it you can actually change it to a single click so if i went up to file preferences and it's going to be in the object editor if i change this double click to a single click then it actually changes it right here and i kind of like that i always thought the double click made more sense but now that i'm looking at it it makes a lot more sense to be a single click so let's do that okay so i'm in the move left now i'm going to go to the move up and i'm just going down in the order that game maker put them up here so we've got direction and we're setting it equal when we're going up which is 90. so don't forget a semicolon it ends your sentence and please be adding spaces so that it's very legible if you send me your project and it is all wonky without spaces well i'm gonna have a few words with you just so you know but they'll be kind words and we don't want to forget the description of moving up now let's go to the right event and give it a description very useful description here as well this might seem really redundant but when we start adding more events like the step and the draw event things that are not obvious what they're going to be doing having a useful description and comments inside is a lifesaver so moving to the right direction equals zero and moving down you should know this one direction equals 270. so all we've done so far is added a few more events and set the direction to change when we press a key so if we click play or we press f5 to run our game we should now be able to press our keys and change our player there we go we now have control over our player now the player is continuously moving so depending on the kind of game we're making you might want that you might not so let's set up a system where we can actually control our speed as well so i'm going to go into the create event and i'm going to delete this speed line because we don't need it anymore instead i'm going to go to the add event and on key pressed i'm going to go to digits if you have a number pad so that's like over here on the right where you can actually have extra keys you have the numbers you have plus minus num lock you have an enter key and stuff over here if you have a numpad you can do that if you want for the keypad but that's what that is if you're on a laptop or you don't have a number pad make sure you're on digits which is what i'm going to use here otherwise you won't be able to use the keypad event because you don't have a keypad so i'm going to add a keypad event for 2 and i'm going to give it a description called set speed and in here i'm going to make it set the speed equal to 2. speed equals 2. i think you can see what i'm doing here what we're going to set here is just add a few more key presses for the different speeds that we might want to allow and then inside of our game we can actually just have it set the speed when we press these numbers so i'm going to go ahead and instead of adding an event like we could do this is the description i want and this is the same formula that i want so i'm going to right click on this event because right clicking on events works i'm going to right click and i'm going to duplicate this event now when we do that it brings up the context menu for adding a new event and that's because in an object you can't have two of the exact same events if you did you would just have more code in one of them so instead it prompts you to choose what this new event is and it will copy everything inside of the original event into the new one so we're copying the event but we have to change it to a new uh specific event so i'm going to change it to key pressed digit we'll set it to five and then i'll go to click on five and i will set it to a speed of five and then i'm gonna duplicate the event one more time and key pressed digit zero and in this one you can imagine that we're actually going to set it to zero so now we got three keys in here we can set ourselves to stop moving move medium speed and move fast speed so i'm going to press f5 and run this again and now we don't move because our speed is at 0. so i'm going to press 2 we start moving i'm gonna press five now we're moving a lot faster and then i'm gonna press zero and we're done now we can control when we're moving it's not exactly elegant you probably actually want a system where when you move like when you press a key you start moving that's one way to do it this is just showing you another way of setting it up in the next video when we actually start doing a little more complex coding not a lot but when we start doing a little more coding i will introduce you to some of your own variables and we're going to set up a much better movement system here this is just getting you introduced to events now i know we stuck with the key pressed event but you added a lot of events into your object we got it working and you can now control the speed not fine grain control but we're getting there just starting really really slow making sure that you're comfortable so if you ran into any errors on this one well they're probably because you didn't add the correct event so if you did you can go back into the error video which is right before this because i think it's going to cover everything that you could have run into on this video as well so i'm not going to take too much time on that but if you're all set and all good then what we're going to do next is setting up a little more fine-grained control and introduce you to custom user-made variables where we can store our own data that we want and why we want to do that and how it is really really useful so i'll see you then and as always keep making keep learning and i'll talk to you later [Music] you
Info
Channel: Let's Learn This Together
Views: 173
Rating: undefined out of 5
Keywords: GameMaker Studio, GameMaker Studio 2, GameMaker Studio Tutorial, GameMaker Studio 2 Tutorial, GMS Tutorial, GMS 2 Tutorial, Let's Learn This Together, GameMaker 2, GameMaker Projects, beginners guide, absolute beginners guide, part 7, more events, code runs, event code, code event, coding event, event coding, coding key, game dev collision, game collision, game development for, game development for beginners, game maker studio 2 beginner tutorial, beginner tutorial, GMS
Id: X-52y83tl1A
Channel Id: undefined
Length: 14min 54sec (894 seconds)
Published: Mon Dec 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.