Movement and Collisions | Farming RPG Tutorial: GMS2 [1]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome to the second video today I want to go over movement logic starting simple and working our way up so the first part of the video is going to be more explanatory and I'll be showing you coding examples so that we can compare them quickly and we'll start live coding after that I know I mentioned importing assets last time there have been more sense to separate them into different videos so we'll do that next time so just so that we have an end goal in mind I want to go over what I want our movement system to have by the end can I have four directional movement so the player moves left right up and down we're not going to have diagonal movement I also want our system to have different speeds so that if we hit the shift button we'll run a bit faster and I want us to have a collision system as well so those are our goals I started the card that we had in the last video so we remember we were in the step event of the player so this right here is movement at its simplest right be able to say if I hit a button then change the coordinate so there's a few ways that we can refine this from the get-go so for one there's a quicker way to write x equals x plus 2 we could just write x plus equals 2 it means the same thing or if you're writing x equals X minus 2 you would you could put x equals minus 2 or you could put X minus equals alright so now let's say that I want to change the number of pixels my character moves when I hit a button so instead of 2 let's say I want to change it to 3 so I could go and manually change all the twos but what if I want to change it again later so we can make life a bit easier on ourselves by using a variable instead of a number and then all we have to do is change that variable once so I'm putting SPD for speed we can't actually use the variable speed because it's a built-in variable in game maker if you type it in a code block you'll see it go green and anything green and game maker is a built-in variable so that includes things like x and y but anyway I'm going to put SPD but you could put player speed on life speed or anything you like so remember this code is in the step event right it's running every frame and we need it here because we need to check for input every frame and we want it to be updating the players decision every frame but we don't need to be declaring our speed variable every frame do we so we can actually move that off to the create event because we only need to run once when it's created let's refine us a little more I'm actually going to set up a few more variables and I'm going to make them equal to our keyboard checks so these variables are storing the keyboard check which means they'll either be equal to true or false I'm pressing the button or I'm not pressing the button and just like with our speed variable we can substitute these in and so now like we don't have to do this but it can be handy to store this variable because if we use it later in the code for example maybe I wanted to check if the player was idle and not moving I might want to change it sprite to the idle sprite so I could just check if we're not pressing the name of our input keys then I could plant occurred in there and it also just makes everything a bit more readable in my opinion but this isn't really in a crucial step all right let's move on to collisions so we'll go into how to actually create a collision in a couple minutes but for now I'm just putting all caps collision as a placeholder that's an actual card okay this is just to show you the logic so we could set up our system like this we could check for the input move the player according to that input and if we end up colliding with something we could move the player back so that it's not colliding with anything anymore but this isn't really ideal because it would move the player twice we've moved it forwards of them we've moved it back because we're colliding it would be more efficient to just prevent the player from colliding in the first place so to do that we would have to be looking ahead so we would when we do our collision checks we're checking if we're about to hit something instead of if we are colliding with something and if it is going to hit something we don't want it to move it's full movement let's say our players speed is six and it's four pixels away from a wall we want it to move only those four pixels and then stop right in front of wall so to do this we have to set up the logic for our system differently first we check through the players input then we calculate to see how much we want to move this is kind of our intended movement let me check that intended movement to see if we're going to collide and if it passes then we apply the movement and then and only then do we change the players x and y-coordinates so in terms of code our input code it's the same as the fall but now for the intended movement we're going to set up two new variables move X and move Y and these are going to be the amounts that we want to move in the X and y direction so we'll be putting X plus equals move X at the end so similar logic to before if I press X the amount I want to move the intended movement will be equal to my negative speed negative because they're going left and the same goes all of the other inputs so next is our collision check so remember we're checking here not if we're currently colliding but if we're about to collide if there is a collision then we're going to set our intended movement back to zero because if we're going to collide we don't want to move there so we're going to prevent the movement in the first place but if we pass the collision check and we're not going to collide then we go ahead and we apply the movement to X so if there's no input or if we fail the collision check then move X and move Y are going to be equal to zero and we won't be moving anywhere so I'd suggest adding comments to your code at the stage because it's getting a bit more complicated just so that you remember what each section is doing and we can add comments by just adding these double slashes at the side of the line anything after those double sessions they won't actually do anything at runtime they'll just be ignored so you can put anything you like there so at this stage let's just take a minute to think about what we've done so we've coded the four directional movement but we haven't constrained the movement or only one direction at a time at the moment it will still be allowing diagonal movement if I'm pressing left and the Alt key I'm going to be moving up and to the left so I'm going to be moving diagonally so to change this and only allow movement in one direction all we have to do is change our if statements to elsif so now these statements are sort of linked together so when occurs runs if I'm pressing left the first statement will be true and it will run the code inside the code brackets but it's not going to go any further it's not going to check any of the following else if there are any checks if the previous condition hasn't been met so the only way we're ever going to go up or down is if I'm not pressing left or right so we could leave our code here it's totally fine it works fine in a game if you have it like this but there is a slight bias in the movement so if I'm pressing multiple keys at the same time you can see because of the way that we've structured that if and else if statements that left is always going to win so let me show you so here I have my player object and some errors and these are going to light up and show you which buttons I'm pressing so if I press right and left at the same time less wins and the same goes for up and down so there isn't really anything wrong with that some games actually they have movement like this but there is another way that we can corrode our movement to prevent this problem and to make it so that if we're pressing left and right at the same time that they cancel and we just don't move so we can actually get rid of our previous intended movement courage and simplify it into just two lines so let me explain these two they're a bit trickier so remember how I explained that the keyboard check functions they return for a false right so I'm pressing it or not pressing it true or false in gaming a true or false they're actually equivalent to one and ER so if I write 10 x false that's going to equate to 0 so these input variables here are going to be equal to 1 or 0 and this means that we can use them mathematically so here I've got input right - input left so that's going to be 1 or 0 - 1 or 0 let's work out an example so let's pretend I'm just pressing the left key only so this means that input right will be 0 and input left will be 1 so we'll get 0 minus 1 and that will equal negative 1 and then negative 1 times our speed will equal negative speed which is exactly what we want because we want to be moving left we want to be decreasing our x value ok let's try another one let's say I'm pressing let's say I'm pressing both keys so input right will be 1 and input left hook B 1 so we'll get 1 minus 1 which is 0 and then 0 times SB will be 0 so move X is going to be 0 and we're not going to move finally if we're not pressing any buttons both input left and input right are going to equal 0 so again we're going to get 0 times our speed and move X will be 0 and obviously this is all the same for the move Y so our maths works out exactly the same as before but now we require our inputs cancelling each other if we're pressing two of them at the same time so the only thing is we're back to our previous problem of allowing diagonal movement so we can move up and we can also move so that's allowing diagonal so to stop that we can just add a little if check so will only allow moving up or down if we're not moving horizontally so if move X is equal to zero so this means that if we're pressing left and then we hit up we're just going to keep going left but if we're pressing up and then we hit left will stop moving up and then we'll just start going left because the if statement is not going to be true anymore so if you prefer the other way around so that there's a bias to moving vertically instead of horizontally then you can just change those over all right so we're done for the movement the next thing that we're going to do is write the code for having multiple speeds so we're going to come over to Gamemakers - - we're going to come over to our player objects create event and we're going to declare three more variables so these are going to be our walking speeds normal speed and our run speed and I'm just going to set these equal to one two three but you can have other speeds if you want as for our speed variable it doesn't matter what it equals here because we're actually going to be changing this in the step event alright so over in our step event we're going to declare two more variables two more input variables so we're going to have input walk and we're going to have that equal to the keyword check for the control key that's going to be my walking key and we're going to declare input run and I'm going to make that equal to the keyboard checked for the shift key so now to alter the speed we're going to change our SPB our speed variable depending on which of the input buttons that were pressing so either going to be pressing walk run or nothing and so this people equal the walking speed the run speed or the normal speed so if we're pressing the input walk so if that's true then our speed is going to equal our walking speed and then else if we're pressing the Run button the speed will equal the running speed and if neither of those are true then our speed which is equal the normal speed and just to make it a little prettier a little bit more readable sometimes I like to tab over these just that they will line up and I can read it really quickly okay so let's see how that looks so you can see the speed variable in green here changing so if we are pressing the Run button SP goes up to three and if we're pressing the walk button speakers down to one but the thing is again there is a slight bias to the walk the pressing walk and run because of how we set out our card logic again there is a bias to the walks a progressing book and run it's going to just stop and do the walking because that condition is being met okay so we're going to look at another way to write the speed so we're going to say if we're pressing the walk or the Run button and we're just going to write this out and I'll explain it once it's done because it's a bit long so speed value is equal to the absolute value and that means even if this works out to be negative it's going to make it positive and then input walk times the walking speed minus input run times the run speed so remember that the keyboard checks they can equal 1 or 0 so we're going to use this again to do a bit of math so if we're pressing both the walk and the run speed input walk is going to be 1 so we have 1 times 1 so that equates to 1 and then minus 1 3 and then that's going to equate to negative 2 and then we're taking the absolute value of that so that negative 2 that will become a 2 and our speed will equal 2 so by doing this we're getting the midpoint between the two speed and then we can just put otherwise if we're not pressing walk or run then just make us speed equal to the normal speed changed our speed code you can see that if we're pressing walk and run at the same time it's no longer just defaults to the walk it's becoming - so the midpoint between the run and walk speed I just to make it slightly more obvious let's go and change let's go and change our speeds a bit we're going to change the walk speed to the normal speed to three and the run to six now if we're running speed six if we're walking it's two and our normal speed is three and if we're pressing both 1 and walk at the same time our speed goes to 4 that's exactly what we want okay and the last thing we're doing is the collision so before we go any further let's change something in the project settings so let's go into options main and we're going to change the game frames-per-second we're going to change that up to 60 just so that we have 60 frames per second all right and actually before we go any further we should change our asset names so it's really important in your game projects to have a good naming organization so what people normally do is they have a sort of naming strategy so they have all of their sprites begin with the prefix SPR or S or something like that and then whatever the sprite is also SPR player and then for the objects all the objects will have a prefix obj test and then player because we can't call them both player you can't have two assets name the same thing game maker is going to complain and if you ever reference that name game maker isn't going to know which one you mean and it's going to be a conflict so you have to call them something different and for room we'll just call that RM 0 all right so it's coming to the player object so this is the code that you should have right now in the seventh and in the create event here's what we have actually I might just change the speeds back to one two three that's going to look a bit better for our RPG game so for our collision we're going to be creating a new objects that we're also going to need a new sprite let's go over to actually we can just duplicate the place right and we'll call this SP our collision and we'll come into it and we'll just make it a different color so I'll make this red so while we're here something important for collision is an object's collision mask and this kind of it defines where on the sprite we're going to be checking for the collisions so we didn't have to check the collisions on the entire sprite we could just check for a certain area so we can manually change what's called the bounding box of the sprite to be something different but for now we'll just make it equal to the B entire sprite another thing to note is the sprite origin so this is where your object is going to be anchored in the game world so if it's at the top left when you place the objects that's where the object is going to be anchored to in our game will probably be changing these to be the bottom center where the characters feed is but for now we can just leave it at the top left so we're going to create the collision objects we're going to call this obj collision and we're not going to do really anything in here we'll just assign it the sprite that we made and we'll head back to the player object into the step event and we're going to actually be coding the collision now so we're going to get rid of this I'm going to have to close and checks we're going to have a horizontal check and I'm going to have a vertical check and this is for our moving X and move Y so the code that we're going to use for game maker for a collision is called a place meeting so it's checking if two objects have overlapping boundary boxes we're going to type if theis meeting if there is a place meeting at our x position plus move X because we're checking where we're going to be not where we are right so X plus move X Y and then the objects that we're checking for a collision with is with obj collision so this function is going to is going to return either true or false so if there was a collision it will return true and then occur it inside these brackets it's going to run so now let's just put move X equal to zero so that we stop the movement and it's coming through the room editor and I'm just going to get rid of some of these player objects we only need one and I'm going to drag in a few collision objects and just to note you can only drag in objects into the instances layout so you can't drag them into like a background because it's not the appropriate asset type for this layer in the background there and we put all of our instances all of our object instances into the instances layers although you can have more than one instance layer alright anyway let's just create a little level for our player so you can you can click and drag on these objects and stretch them make them a bit bigger all right that will do let's run the game all right so we can see that our code is working power player stops but you can see there's a little gap between the player object and the collision object and it's okay at this speed but at higher speeds it's going to be leaving a bigger gap let's just change the running speed to ten so you can see it's leaving quite a noticeable gap you whereas the desired movement for us would be stopping right next to the collision object so let's just set the speed back and we'll come back into our SEF event and we're going to alter the collision code so instead of just setting I'll move X to zero we're going to do something a bit more sophisticated basically if this turns out to be true if if we are going to have a collision at X plus move X the next thing we're going to do is check if we just have a collision one space away from us and if there is an occlusion there then we're going to move closer to it and we're going to keep doing that until we hit the edge and then we'll stop so we're going to go if there isn't a place meeting if there's not a place meeting at X plus the sine of move X so this is going to get one in the direction that we're heading so either negative one if we're moving left if move X is equal to negative speed or positive one if move X is one and we're moving right so it is not a place meeting that X plus the sine of move X then we put Y an object collision and then we're going to move X to that position because that spot is free so now we can't just do this one so we're going to have to repeat this code as many times as there are spots free to move to so we're going to repeat and a number that we're going to repeat by is the absolute value of value of X so whatever our speed is obviously we don't want to be using negative speeds and we're doing this also to account for different speeds that we're walking or running this code should still work so we keep checking the places free and if we do find a place free then we can come out of this repeat loop if there's only four spaces free we don't have to repeat this six times so if we find that there is no longer a free place then we're going to put break and that will stop the loop and will come out of it and because we've been altering X directly we don't want any more input from our intended movement so we want to set move X to zero so that it isn't moving because we've done on the movement ourselves through the collision system so because this code is kind of bulky so sometimes the way I like to do just to neaten up my code and reduce the number of lines I might just put it on the same line as if conditions like that just to make it a bit shorter and a bit easier to read but you don't have to do that if the other way makes more sense then just leave it as it is so now we can just copy and paste this horizontal collision check and we'll just alter the variables to make it appropriate for the way all right so we're done let's have a look at that move around so you can see that it's working and it's coming right up next to the television blocks and it is a behavior that we want it's pixel perfect collision so one little thing that we can do to make this system a little bit more efficient is that these collision checks we don't actually have to be executing them all the time we only have to be checking two collisions if we're moving at all so before the collision check we can actually put move X is not equal to zero then that means that we're moving so if we're moving then go ahead and do the collision checks so we can wrap this whole thing in this if statement and just tab it forwards and we can do the same for the vertical and it will stop running unnecessary curve so it'll make the whole thing a little bit faster it's only checking for collisions when it has to all right so we're finished we've got our movement system and a collision system in place in the next video we'll definitely start importing actual assets and making our game a bit prettier and having a bit more fun but I wanted to get this down and out of the way because it's really important to the foundation of the game and it's good to consider different methods for movement because there's literally infinity ways to code something and it's just going to depend on what you want out of the collision system and what's most appropriate for your game so I hope that's helped and I hope to see you in the next video bye guys
Info
Channel: FriendlyCosmonaut
Views: 138,048
Rating: undefined out of 5
Keywords: farming rpg tutorial, farming rpg tutorial gamemaker, movement tutorial game maker studio 2, movement tutorial gamemaker, collisions gamemaker studio 2, collisions gamemaker, farming tutorial gm2, friendly cosmonaut, friendly cosmonaut farming tutorial, rpg movement gamemaker, gamemaker studio 2 tutorial, game maker tutorial, gms2 tutorial, game maker
Id: Tc4ijzjplZs
Channel Id: undefined
Length: 22min 16sec (1336 seconds)
Published: Sun Jul 02 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.