GameMaker: Studio - Movement Tutorial Part 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alrighty then so now that we've covered both of the those main options in the drag and drop system what we're going to look at now is coding these by hand in gml it's really not all that different at all um and you'll see as we go through like how those drag and drop events translate to code events and vice versa so if we go into our object player now and we get rid of basically everything we already have so we get rid of all that we get rid of get rid of the step event um get rid of this friction in fact that's almost no we won't start with friction actually we'll get rid of the creative for now as well so we're just back to just our left and right events um once you learn a little bit more gml you realize you don't even need these you can just in the step event um use a piece of code that checks if the left and right mouse uh not mouse buttons keyboards uh buttons are being pressed but for now we'll just we'll stick to just using these left and right uh events because they work fair enough so if we go to control here and uh pretty much the only action you ever need when you're working with gml is execute code so we drag that into there so whenever you're moving left i'm pretty sure we covered this in in the first basic tutorial is to basically just say x minus equals three and then that in itself is identical to the very first thing we did in this tutorial which was jump to position uh minus three relative uh that is basically an exact translation of that we're taking our x-coordinate and we're literally just subtracting three from it and then we're ending the line of code with a semicolon just to tell the game that that's the end of the line of code so we hit take there and then we'll do the same thing over here with right only we'll do we'll do x plus equals three semicolon and if we go okay run that and we have exactly what we have to start with literally no change but it's just written in code as opposed to written in drag and drop so yep that's literally exactly the same as the very first thing we did how we do the next system however in gml is a little bit more complicated only in the sense that there's actually a billion or one ways you could actually approach this um which is part of the strength of using code really is you can do things in a huge number of different ways to have pretty much the same outcome but then depending how you do it it allows you to do other things with the code and so on and so forth so it really gives you that sort of flexibility and exact control over exactly how everything in your game happens so we're going to look at pretty much the very simplest way of replicating what we did before in code which is basically to set our hp to be a value so i hate speed uh i mean we already know the like you can tell in gml when you've uh written a variable that the object recognizes as sort of one of its primary sort of variables is because like as you can see when i'm timing here speed it's just great text there and it's even come up with the auto correct there look variable speed and that will tell us what our speed is but what we want specifically is to change our h speed value which is our horizontal speed so that's a a variable the effects uh where the object is moved to every frame so if we set that to three every frame so h speed equals three oh no actually minus three every frame because that's the left uh arrow key and hit okay and we go to right we do the same thing speed equals three notice how we're setting them straight to three and minus three so as you might have guessed this does pretty much exactly what we had before only you'll notice i'm only well you won't notice because you can't see my keyboard but i'm only tapping left and right to make the object move left and right because we're just setting our hate speed and we don't have our friction set up yet so we're literally just setting our speed to three and minus three and then there's no friction to slow it down at the moment so it's moving forever so what we do is we go to add event create and you already know how to do it through drag and drop but to do this through through code it's very simple you just type friction and you can see it's turned red it recognizes the variable friction we type equals and uh what did we have before um not point two five i think so or is it just not going to well no point two will do click hit f5 and there we go now we have the very basics of what we had before again where we're accelerating when we're not accelerating actually we're setting our speed to three and then when we let go of the burn we're sliding to a hole because the friction is reducing our speed every single frame until it reaches zero so now uh what we want to do is uh recreate the acceleration effect that we had before so in order to make your object accelerate instead of just move is you can pretty much guess what we have to do here since instead of uh when we move whenever we're holding the left key setting our hp to minus three we just said hey speed minus equals which you know we subtract from this value uh what did we use before i think it was one yeah so we just subtract one from our hay speed every time we're holding left tick right and the same we'll do the same thing over here so instead of setting our hb to b3 we just set h speed plus no not equals plus plus equals one so now that should accelerate as one in either direction but of course we need to set our maximum speed otherwise we'll just go to crazy velocity super quickly so to set a maximum speed what we want to do is once again create our step event uh put an execute code action into the step event and we're basically going to do the same thing we did over here with our variables which is to check uh if the speed is greater than a certain value set the speed to be that value in whatever direction it happens to be so if uh and then we want to open a pair of brackets like that and we want to put our condition in here so if speed is greater than greater than symbol six so how if statements work actually while i'm here is we type once we type if here that registers that it's an if statement to the code and then we put our brackets here and it checked the game will check to see if the stuff inside this bracket is true so if our speed is greater than six it will perform any actions we put here blah blah blah blah and then end with the semicolon it will will activate those if we want to do multiple actions as a result of a if statement what we want to do is use the sort of squeegee brackets i can't remember the exact term for these and then we write code in here and then all of this code that we write in here will get performed if this happens to be true we only need to set one thing to be true though so we don't actually need these open brackets all we need to do is put speed equals six so if speed is greater than six speed equals six and you can see how that is exactly the same as the drag and drop system we had before so if we hit take now we go okay that should unless i've forgotten anything drastic yeah leave us with pretty much exactly what we had before so press left press right you you slide so if you start accelerating and then you slide to a hold slowly so that's the basics of movement and left and right the only other thing we haven't covered which will be quite important for our top-down shooter is how to stop because at the moment you could just move straight out of the screen so you could you could just keep moving left forever or moving right forever and we want to keep you contained in this space here so we're going to look at doing that now so i'm not actually immediately sure how you would accomplish this through the drag and drop system and but i know a very very simple way of doing it through the code so my general rule of thumb is if something is very easy to do in code and can be done in like less than a line or so like there's no point like using a complicated sequence of drag and drop actions to do it you know you're just you're wasting time so we're just going to cover the very simplest way to do this using the gml code so if you think about what we actually want to do using gml is we want to make sure that our x position uh our x coordinate of our player object is basically always inside the playable space so inside the room so we can easily do this by setting our x to be equal to one of two values if we set type the word min open a bracket what this does what min does it will so we will set our x co one to be whichever is smallest so the minimum of however many values we put into these brackets so if we set our x to be whichever is smaller its own x coordinates or the uh the room underscore width which as you can see it's gone red there um which is equal to the width of your room so imagine if the game was trying to set your x value to be 700 and the the width of the room is only 640 um it would check here every frame and it would set your x to be whichever is smallest out of those which would be 640 and would keep you inside the room um otherwise if your x is currently like 500 and you're within the room it just sets your x to be itself again therefore meaning you know you it hasn't done anything so that all that literally does it stops you moving out of the right hand side of the room now to do the opposite of that for the uh the left hand side of the room we want to say x equals max you might have guessed what this does is sets x to be whichever is biggest out of however many variables you put in here i mean you can put more variables into here as well you could be like y or from 25 or anything like that you know but obviously we don't need those so we don't uh we don't put them in but um we want to set our x to be the maximum of either whichever is biggest its own x coordinate um or zero so basically if you would be like a position of minus 20 that's outside of the room so it would set um so zero would be bigger than x therefore it would set your x coordinate to be zero so on the furthest left of the room uh otherwise if your x is 20 and you're inside the room um it would set your x to be itself again because x is bigger than uh bigger than zero so those are the only two lines of code you need hopefully i think if i recall correctly to uh to keep you inside the rumor pretty much all times now we're going to finish this compiling we move left and right as you can see now we can't quite we can't move out of the room we're overlapping a little bit because uh the origin of our player object is like directly in the center so that's where it's tracking our x-coordinate to be so like as you can see it's only tracking us as being outside the room if uh our x-coordinate which is like dead in the center of this player object would go over the edge so if we want to keep ourselves like fully inside the box um like without like overlapping on the edge we have to make a small slight change to the code we'd bring it's very very simple though all we need to do is basically um actually first of all we need to check how wide our sprite actually is in the first place um it's 32 by 32 so we go to step and we go to x we make x the smallest instead of x in the room with we make it the smallest out of x and the room width minus 16 which is half the size of our ship so like we can't go beyond uh so the center of our ship cannot go beyond the room width minus 16 and we set down here instead of zero we just set 16. okay and then hopefully what that'll do yep and now we can't we can't move off the edge of the screen at all and that overall is more or less everything you need to know about moving in sort of 2d space uh with no real collisions or anything like that so one bonus sort of extra thing i may as well show you guys now is how to put basically all of that stuff that we've done into a single block of code more or less um so instead of having our left and right events we're basically going to condense ourselves down to just being in the step event so we don't need uh this stuff anymore we can delete these events entirely and we're just going to rock our step event so what we need to do is basically replace those events uh with some if checks inside of our player object so all we're going to do is type if uh keyboard check and then another open bracket bk left well that line of code does there you can use f1 to look up the exact definitions of these functions and exactly what they do but um all that's doing is basically this line of code here is exactly a replica of the event we had here that was the the keyboard left this is basically that that event there and uh instead of the actions in that event taking place what will happen is the lines of code we put inside these brackets will take place so anything we put in here will happen when we hit left and supposed to uh as opposed to the actions we put in place or the stuff that we put on the end of here and then with the semicolon so i mean you already know what we did before is we put hate speed uh minus equals one on the end of that so if i now copy and paste that again and change v k to right but h speed to plus equals one here we basically duplicated what we had in those left and right events but kept them all condensed into one uh one block of code just so it's easier to keep track of more than anything so now all of that line code there we still need to keep this uh friction inside our create event i mean we could set this in our step event but there's really no point because we only need to set our friction once and that's when the object is created and there would be no point putting it in the step event because it would happen every single frame so there's no need for us to set our friction again every single frame would just we would just be wasting like uh the processor's time so now that that's done uh hopefully there should be absolutely no difference yep to what we had before hold left and right move left and right and we're golden so that's everything uh for like a bonus challenge you could try now and use everything you've learned here to make the ship move up and down using the up and down arrow keys um it's pretty much you know you just basically have to copy and paste everything um you've already seen here but instead of using x coordinates you use y coordinates instead of h speed horizontal speed you use v speed which is vertical speed um otherwise the code is pretty much exactly the same um the code is almost exactly the same for making you not move up of the top and bottom as well so you could you could have a go at that next time around we're just going to be looking at a basic like shooting and all that kind of stuff and how to how to create new instances on the on the fly with uh code and how to shoot bullets and stuff so i'll see that if anyone has any questions about anything i've talked about in this tutorial or wants to learn more about sort of 2d movement very and uh like the drag and drop actions or um any other gml i've i've gone through or anything like that please leave a comment uh so please like please subscribe if you you like the content you learned anything from it please share it around if you know people who would also benefit from this and otherwise just thanks for watching and i'll see you guys in the next one
Info
Channel: Shaun Spalding
Views: 172,648
Rating: undefined out of 5
Keywords: Game Maker (Video Game Engine), Game Development, Indie Games, Indie, Games, Tutorial, Information, Learning, Tutorial Series, Game Maker Studio, Steam, iOS, Android, HTML5, Windows, Making Games, How to make games, Basics, GML
Id: lhWHgRL05Ek
Channel Id: undefined
Length: 16min 46sec (1006 seconds)
Published: Mon Dec 03 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.