Absolute Beginner's Guide Part 5 - Wading Into Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right it's time to start learning how to code now we're going to be starting really simple inside of game maker picking up exactly where we left off two videos ago when we were just exploring game maker studio if you're jumping in and you haven't done that yet let me show you what we've got so far because there's not a lot but there's two things you're gonna need first off we have a sprite this one is called spr player it's just a circle with a smiley face in it then we also have an object called obj player and this object has that sprite attached to it to do that you just drag the sprite into it or you can click right here on the sprite and navigate to where you have it and it will be right there once you've got that we have an object and we have a sprite the sprite is what you see in your game it's the image it can be animated it can be static which means still but it's what you actually see and then we have the object which is what runs the code now game maker studio uses an event driven system and what that means is it has specific events things like when an object is created or destroyed or if you want to draw something or if someone touches a screen or does a drag on it because you can make mobile games here if any of the events that game maker has registered is triggered and there is code inside of that event then the code will run so let's give that a try over here in our object we're going to click on add event so there are a lot of different events inside of here some of them are fairly self-explanatory create well maybe not self-explanatory but a little simpler than other ones a create event occurs one time when the object is first being made so code inside of here only gets run once destroy the opposite it gets run one time when we destroy it cleanup we're not going to worry about i'm not going to touch on every single event here because as we get down to these ones there's actually a lot of different ones but the important ones are create step a step event you can think of as like a new frame has come into our game now if you're familiar with gaming you may have heard the term fps which stands for frames per second game maker uses 60 frames per second and their word for the event is actually step so there are 60 frames per second happening so that means every single time your game is running for every second 60 frames it's being refreshed that quickly some games are at 30 some games are 144 or even more depending on the monitor you have the hardware you've got and what the game developer actually put in to allow the game to run at but a good standard number is 60. so game maker went in and created an event called the step event and the step event runs the same amount as your fps so for our game it will check this event 60 times a second this is where we put code for things like collisions and if we're checking for input on the keyboard or the mouse or the touch screen we need to be checking as often as possible to for these things otherwise you can get stuck in that rock because imagine you're playing a game where you're moving and you only check to see if you're colliding with something every one second that might seem like a lot but you can move really quickly in one second if you're jumping or falling or running or teleporting you have to be checking that 60 times a second so that's the step event the draw event is very similar to the step in that it runs 60 times a second but it is only used for drawing things onto the screen which we'll touch on later then you also have things like mouse events so this would be left press left release if you're holding it down if it goes into and out of a section stuff like that key down key pressed and key up are all keyboard events key down will check to see if a key is being held down so this will also run 60 times a second if you want to have code that checks to see if you're holding the right arrow key down and if you are you move well you put it in a key down if you want to check to see if they press the a key one time you put it in key pressed or key up key pressed happens once when it goes down key up happens once when the key is released then we also have things like gesture we have specific collision events we also have other which we're not going to touch on and asynchronous events which we're also not going to touch on so all i want to do is make a create event so when we make that and we click on it it brings up this whole section right here so it adds the event right here and then it brings up a window with code in that we can write in now we have some green code some green words in here already so this isn't really code this is actually notes or more specifically they're called comments so when it's green like this we can type whatever we want and the computer will just ignore it so anything we put in there is going to be ignored by the computer it's not going to try to run it this is useful for us now what is really useful as well is this description part is we can actually say uh well we can say whatever we want but if we keep this at description up here it actually changes it over here in the event and that's really handy now the default with like the insert description and type your comments here i find that really obnoxious so what i'm going to do is i'm going to show you how to change that so we haven't even started coding yet but we found something or at least i found something that i don't like and the beautiful thing is in ides and game engines they give you so much power and flexibility so if we go into file preferences up here in the top left you can see here that there are a lot of options that we can change so we can change how game maker looks we can change the size we can change what comes up by default and all of this so we want to change one default in here and this is going to be in the object editor so we click on this right here and now we can see how we actually open things up so we can do a single click or a double click which is something i didn't actually know this i might point out if you find double clicking to be obnoxious you can change it to a single click very useful but what i want to do is right down here this is what gets added in every single time we make an event so i'm going to highlight everything except the at description delete it and then press enter one time this will give us a second line inside of our code that way we don't always have to press enter now if i press apply and ok let's just add a second event doesn't matter what it is we're going to delete it i'll just add a create event or a destroy event here and you can see that now we have two lines in here and the description is empty so adding in that extra line actually gives us a third one which for me i actually like to leave one line right here just as a buffer between the description and when i start coding so this works really well for me so i'm going to go ahead and right click on the destroy event so if we left click it doesn't do anything if we right click it brings up this context menu and we can delete it if you don't delete it that's not a big deal we're not going to put any code in there so it won't run but it's good to only have events that actually have code in your objects otherwise when you're looking through them it can get a little confusing just a quick side note if you encounter any errors keep trying to go through with the video and what i'm going to do is i'm going to post a video after this that's going to cover all of the possible things that could have gone wrong so keep following along try to get it if not go into the next video where i'm going to cover i think everything that could have gone wrong for you so we have an object we've got a create event the create event runs one time and right now we have nothing inside of here except a comment i'm gonna press enter to give me that one line buffer that i like and now we're going to type speed equals 5 and then we're going to put a semicolon and i'm going to press f8 to enlarge this so that you can see it really easy f8 enlarges the code size on your screen f7 will make it smaller so let's break down what this one line of code is doing first off we have color coordination super handy and very nice you can change the colors i would not recommend doing that if you're following along once you're super comfortable in game maker go ahead and you can change colors however much you want but if you're following along or you're pasting images for someone else to help you debug or something like that if you've got custom colors in there it makes it really hard to know what is what so this green right here this is a built-in variable a variable is a piece of data it holds information about our object about our room about our game it holds information this one is one that game maker itself created sets and can update based on different factors but we can both read it so we can get the value and we can set it like we're doing right here so we are telling game maker hey you know that built-in variable called speed we want to set it which is what one equal sign does we want to set that to five and then this semicolon right next to l you don't need to hold shift it's the default semicolon uh just the default button press this is like a period to a computer so you press that game maker says okay this line of code is now done i won't look any further game maker is very lax in their programming language if you don't put a semicolon no problem if you don't put any spaces between this equals no problem i do these things because it's what i have always done and i find it much more legible if you don't use semicolons it will get you into trouble in pretty much every other programming language out there and sometimes it will get you in trouble in game maker too so it is always a good idea to be putting semicolons and spaces as you type your code i tend to think that i have really nice legible code so follow along with me even if some of it doesn't make sense that it will help you learn to read your code other people's codes and for other people to read your code if you ever ask for help so this sentence right here is your first line of code and the way we would read it is speed equals five so we're setting speed to five now speed controls the speed the movement of our object so by default speed is set at zero we are changing it to five so now if we open up our room that's the other thing we did for anyone who did not watch other videos we did put our player in our room so if you need to you can minimize the docs here on the side with these little arrows on the right and the left or you can press f12 which is also super handy and that will bring up most of them sometimes all of them but sometimes not all of them so we dragged our player into our room if you drag your player into your room and it shows up perfect if you try to drag your player into your room and it gives you an error that means you're actually bringing in a sprite so you don't want to do that or you don't have your instances layer selected and it wants to create a new one that's also not okay so make sure you have instances selected here and you can drag your player in you can select and press delete to get rid of it because you only need one so with the one line of code we've got we can now run our game if you remember it is this triangle up here or f5 and our player object is now going to glide across the screen and off into infinity because it's actually never going to stop just because we can't see it doesn't mean it isn't moving so there's your first line of code and let's go back to our workspace we have tabs up here so room is here this tab is over here so we've set speed to five speed it tells it how many pixels per frame to move that means that when we set it to 5 it's going to move 5 pixels per frame now the other question is how does it know which way to move because right now it's moving to the right but we didn't tell it to and that's because the direction of our player also has a default it also has a variable so if i say direction you can see it comes up here as green and this right here is the autofill which is super super handy so we can actually look at it and say this is a built-in variable because it says that then we have functions down here with the word direction in them ignore those for now but this built-in variable called direction controls which way our player actually goes so if we set this equal to 90 with a semicolon after we can control which way it goes now to imagine and to know how this is actually going to function let's go back into our room for a second so i'm going to draw you a really crude example take a quick screenshot of what we got right here and now in here game maker has direction built in like this so imagine we have a circle that's actually not that bad of a circle this is zero to game maker going to the right is zero going directly up is 90 going directly to the left is 180 and going directly down is 270 and then you have all the numbers in between so 0 to 89.999 repeating indefinitely is between right here and so on for each of these so when we want to change the direction this is what you've got to think about to go to the right is 0 up is 90 left well i sure hope my camera is actually pointing the right way here left is 180 down is 270. so over here we just said direction equals 90. so i'm going to press f5 and run this now and you should be able to tell it's going to go up so with direction we can now change this whichever way we want so if i said 156.82 [Music] just a random number and we throw that in it's going to move that way now to know about direction one thing to know about direction is we could change this to like 500 so it's gonna start at zero go all the way around to 360 and then it's going to bump up 140 and that's 500 to it so if we wanted to do 720 this is actually going to be pretty much directly to the right because it's 0 to 360 and then 0 to 360 again that's 360 plus 360 is 720 so direction zero right up 90 left 180 down to 70. so by default it's zero but we can change that to whatever we want and this is your first foray into coding this is writing code this is gml this is the same kind of syntax which is stands for like what you're actually going to see the what you type on here this is the same kind of syntax for almost every single language you're going to put a variable on the left you're going to assign it data that data is right here and then you end that assignment with a semicolon so this is real code doing something really useful in your game you want to get up and moving this is how you do it now if we change the speed we can play around with these two settings and there it goes so anything above like 25 20 it's gonna be really hard to see because our room isn't huge and we just fly off the screen but that's okay this is all i wanted to do right here right now so if you have any questions if something doesn't make sense if i went too fast let me know remember if you're confused someone else is confused and i can help you guys but you gotta let me know so i hope this made sense i hope you've got a player object moving in your game world because that's the beginning that's how we're gonna start next up we're going to look at doing something a little more interesting and being able to actually control our player at will and not just have them fly off the screen we're going to tackle that next video by adding some more events and discussing some more code with our own variables not just game makers as well so i'll see you then and as always keep making keep learning and i'll talk to you later [Music] [Music] you
Info
Channel: Let's Learn This Together
Views: 189
Rating: undefined out of 5
Keywords: GameMaker Studio, GameMaker Studio 2, GameMaker Studio Tutorial, GameMaker Studio 2 Tutorial, GMS 2, GMS 2 Tutorial, Let's Learn This Together, GameMaker 2, GameMaker Projects, writing code for beginners, writing code for video games, code writing, code writing for beginners, video game code, game maker studio 2 beginner tutorial, beginners guide to game development, beginners guide, not complicated, programming for beginners, can be intimidating, absolute beginner, learning
Id: CHFsfxQ8lMk
Channel Id: undefined
Length: 19min 54sec (1194 seconds)
Published: Mon Dec 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.