GameMaker Studio 2: Complete Platformer Tutorial (Part 5: Gamepads / Controllers)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to a complete platform game this week we're going to be looking at adding controller input but now we've just looked at keyboard and mouse and honestly that's mostly the way things are going to stay like things would get a bit too complicated if I were to try and carry keyboard control and gamepad controls throughout the entire of this series but I do want to touch on them as they could be an important thing for you in the game you want to make so I want to touch on them and give you an overview on how to get started with them and it is also important to talk about how you want to implement them in the sense that if you wanted a game with gamepad controls it's likely a decision and I'm going to make very very early on okay honestly I've left it a bit late for us to just fling controller inputs in and we haven't even done that much and you'll see why as we go in but the reason I did that was because I didn't want to overload from the early episodes with with stuff with controller input as well as doing keyboard and mouse stuff at the same time or at least that's the excuse I'm giving I think it's a pretty good one so yeah but we're going to do we're going to do gamepad controls we're going to add movement so left and right and we'll make it so it varies a bit as well since we'll be using an analog stick I've got 360 controller here that I'm going to be using for this and we'll use the axis so you can like walk a little bit by holding a little or like going a full speed by holding it full tilt because that's one of the advantages of a controller right normally I suppose you wouldn't include both being able to analog and like you've digitally as a design thing but I do it here just to show it off and also make it so you can aim with the controller and shoot with the controller okay as I say I'm not going to carry on like making sure everything works perfectly between gamepad and keyboard for the entire of the series but I am going to show you how to set it up so that if that's something that's important to you you know how to get started and you can read more or less read the manual for all the rest of the information you're going to need in that regard the important thing as I said is if you want to do it do it as early as you can for a lot of reasons that we'll go into more as we actually set this up so let's get started so let's start off by opening our player object okay as you can see the way we currently move around is just by getting it without some variables and we literally just assign them to our keyboard checks okay now because of this it makes it a little bit okay this is why you need to do these things earlier if you want to put gamepad stuff in you might have been able to think of a more elegant way of doing it than the way I'm about to do it because we've just assigned this this value and these are the absolute values so keyboard check is always 1 or 0 it doesn't really have any room for so in between it kind of does a bit and we're going to exploit that later but on the problem with that is now we basically need to say well but also if we're doing controller stuff on kind of overwrite those inputs rather than kind of extremely checking them both at once so how we're going to do is I suppose so I'm going to go into actually ignoring the step and I'm going to pop quickly into the create event and touch controller equals 0 ok and this variable is going to decide whether or not what currently using the controller the reason that's important is because the way we aim at the moment is using the mouse which is fundamentally very very different to how we would aim using the sake of right analog stick okay just I am looking around so what we want to do is track whether or not we're currently pressing buttons and moving around using the keyboard or whether we're currently moving around with the joypad and basically swap between the two modes you're basically able to do either/or once but like and you can swap between them at any time as long you just pick up the controller and you can start playing the controller but we need to track which one we're currently using to decide which method of mouse which method of gun aiming we want to use ok because otherwise at the moment our gun tracks like our mouse constantly so we need to tell it when to ignore the mouse and just do whatever we last told it to do with the analog stick so that's the reason for that variable you see that coming into a back layer and this is what I mean by doing this stuff early is because if we don't build from the ground up with gamepad in mind we might decided ok Mouse anything it's fundamentally very different when I like is that the way we want in and so on you know Maya phrased other questions we might thought about how we were going to structure it a bit differently ok it's not too bad we're not really too late when everything is not destroyed but just a little bit later but it is some you want to think about like right from the very beginning or your control system should be a very very very first thing you can a nail in your design but anyway let's get going with it so the first thing I want to do is say if we've pressed any of these keys then we can set controller to be 0 so I'm just going to quickly type F key left or key right for key to jump so if any of those have been pressed if any single one of them is one I think I've shown you what the other thing does before then we've been through that so if that's 0 or if that's one or that one or if that's one this only won't happen if they're all 0 ok controller equals 0 okay so we even set if we press any one of these we know we're using the keyboard of this current time and we don't have to worry later on that's what make any difference until we get to the aiming part but we don't have to worry later on when we're aiming we don't have to worry about what the control is doing if we know we're using the keyboard now now we can go ahead and actually write in our controller stuff so let's say if I'm rack and I'll explain this coroners abs and open a bracket gamepad axis value okay so we're going to use the analog stick so it's different from just getting like a binary 1 or 0 from like the key although you can do that for the buttons and stuff on on the gamepad so there is a gamepad button check that works exactly the same way as keyboard check and so on you just have to put in their own constants for whichever but any one check which will do a valve layer in pan axis value open bracket 0 that's our device lot okay to learn more about what device slots are and how many controls you can have it at once you press f1 go into documentation read them on the gamepad page you can have up to four slots that X input which is things like 360 controllers and I think ps4 and Xbox one controllers and quote me on never I'm pretty sure they're all X info as well and then slots like or through seven or something like that or a direct input which is usually like older different models of gamepad that you might want to use you read all that stuff on your own zero we're just putting zero in here because it represent just the first controller you have so again it's just a good default one if you're just sending an open one a basic system of works so I'm just going to use device zero in every instance that we need a a device and function 'is the number zero and then we need to put in the axis index now there's a bunch of constants and what I mean by constants is these red values like DK space and so on represent a specific thing and always represent map thing and you can access I'm just by typing GP you'll see all the commotions see all labeled constant look and what we want is the constant that represents the left of analog sticks horizontal axis okay and that's GP axis LH okay left analog stick horizontal axis because each analog stick is broke up into like a vertical component like how much you're holding it up and down and how much are holding it left or right that's how they fundamentally work okay and what this function is done is basically returned where our left analog stick is in terms of its horizontal axis which is all we care about moving left and right what we don't want to do is just set left and right to be whatever that analog value is because if it's just tilting like the tiniest bit left just goes out how it hangs by default you'll move slowly to the left there will be really weird when you think you're not pressing anything what you do to solve this it's very old well-known technique is just having what's called a dead zone on the controller okay so you need to have an amount of space that you are just touching just touching the Analects with just a tiniest bit and you ignore that kind of input you only take input if it's above a certain amount of pushing in each direction so we're going to say we're going to say greater van don't point to okay I want as close this around fans as well because how access value works as it returns value between minus one so that will be pushing fully to the left and one pushing fully to the right or like the same foot up and down like minus one to one okay so the reason we put abs here is arms will take a value and will always return the positive version of that value so if it's negative seven by putting that through ABS you get seven okay sorry if it's negative nor point seven you put it through ABS you'll get make a positive naught point seven I hope I said that right and if you puts a positive nor point four you'll get no point four and if you get negative Northpoint board you'll get positive in one point point light it just makes it positive no matter what it is okay which is to say so that it's either saying well like if it's a below negative no point two but above no point to we can just do it in one line by saying it's the positive value of it is greater than or point two that means the we have to push it more than or point two in either direction for essentially get value back okay so I was a lot of explaining but what this line does is just say if our left analog stick is beyond the dead zone so we're using our left analog stick to move then we can do some movement and what we're going to do is if that is the case will overwrite whatever we're doing with our keyboard and we'll just prioritize the gamepad input so if I key left equals abs min he'll explain all these as we go again gamepad access value naught again GP axis LH again comma 0 close bracket close bracket okay and I'm going to write a second one at the same time and I'll come back explaining both equals and we can skip the ABS this time just like Max game card access value not key P axis Elledge first bracket comma 0 closed brackets and cool whoo okay so basically were overwrite in key lap which at the moment is returning a 0 or a 1 based on whether or not it's a OD right and our key right returns a 0 or a 1 and the thing is our system is based on that right it's based on the idea that like bomb down here at key right - key lap that only works if both of these values are between Noir and one so we can't use the negative nor point four or whatever you might get from pushing gamepad axes to left we can set key left to be negative anything because the rest of our code doesn't know how to use that we could rewrite this but we're just going to write this in so it works with what we already have okay let me just the way I do that is by putting abs in front of this which forces us to be positive so forces it to be between 9 1 and 10 what I'm doing is I'm typing min so returns the smallest value between either what the axis is or zero so keep in mind that we're moving left this value say like is not put a negative nor point seven towards the left okay negative nor point seven is smaller than zero so in min returns the smallest value so negative normal point seven gets returned gets turned into positive nor point seven and enough key left saying move is no point seven in the left direction which works with what we have okay key right on the other hand we want to on the other thing as well as if this returns a positive nor point seven positive nor point seven is bigger than zero so zero gets returned and yeah ABS is zero is zero still positive zero is zero and so key latter zero so I started and moving right with key laughter okay and key right is going to get the value again so say this value is nor point seven nor point seven is bigger than zero and max returns the biggest value so naught point seven gets put into key right whereas if it was a negative nor point seven negative nor point seven is smaller than zero so zero gets returned okay so how we make sure we're getting the right values into kilo I think you are in that it's a little bit complicated but that's because we've just had to basically force this to work with how we had it set up originally with the keyboard and stuff too if I were just using gamepad and stuff that I could probably just set key less to just equal the gamepad the axis value and key right that equal the axis value and calculate on your oven just based on that and it would be simpler but we're doing both at the same time and we set it up based on just using a keyboard so it has to be a little bit wrangly to get it to fully work the last thing we want to do in here is just that control equal one okay and that's just for are aiming later just so we know that okay we're moving the birth controller now let's let's do controller aiming instead okay and then the last thing we need to do in the step event here is actually make it so we can jump as watching the time its gamepad but yeah press device zero again and then cheap he brings up the constant list again face one okay so that's the there's face one through form they represent the different face buttons on the the gamepad if I just going to game pilot vision for a moment here you can see the controller GP one is going to represent the button that's here generally okay because you know most controllers have a fairly standardized design at this point you know things might move around but generally there's like four face buttons a d-pad two analog sticks and a couple of couple of things on the back right out of the four thingies on the back so like 4x input which is this type of standardized controller QP face 1 represents this face button I can't remember the audibility of three but there's their GP base two three four and then so on I think four is why I'm not I'm not sure but one is definitely the one of the balm which is the one we're going to use three on okay let's come back into an order and there we go and so now that we've check to see whether or not my bonus press it works exactly the same way as a keyboard check press works but the reason we're not just saying key jump equals this is X we don't want to just always overwrite this because sometimes this will be zero so we only want to overwrite this if this is one okay otherwise we'll just relies we know it's not pressed and then we'll just rely on whatever the keyboard bone wants most of the time we'll get zero so if either one of them is pressed we have a right error century so what I could have done as well as they'd like key gum equals this and then done like an or symbol and put this on but the reason I'm not doing that just on this one line at the top is so that I can do controller equals one here when we're pressing this whether we know we're are using control okay otherwise I yeah I could've just done that one line filter key John equals one so was that we just over I if we happen to be pressing it then that's close that bracket as well I should get rid of all our red stuff yet and now if I run the game hope we're not screwed and up and hopefully you're all still with me so far okay I can now move left and right let's put gamepad moving on again just so you can kind of see it working it's like I'll try and play upside down a bit I guess are you can see yeah that's the right one there we go then that analog stick we move the right and also importantly our system just so happens from the way we've worked it to work in analog values because we multiply our what used to be 1 and 0 with a and D by our new speed we're now multiplying a value between naught and 1 so we can actually do that fraction of our full speed just by only holding down the stick a little bit you see we can actually like kind of walk and then if I hold its Li we move up full speed in each direction and we can just jump by pressing that you can see at any time as well we can kind of return some mouse and keyboard I want to move around in that way just the same ok so now what we need to do is put aiming in okay so when we can only currently aim at the mouse and trying to aim with the mouse well playing with the game having out that's not really going to work as so how much you're kind of playing with two people again so how do we do any of that well all of our aiming and all that kind of stuff is controlled in our gun object right so we can close down our player object now and come into our gun object um the other thing I'll do is go into the create event and controller angle again so that equals zero that's going to be the angle that we want Amen if we're currently using the controller okay which will ignore if we're using the mouse for obvious reasons but we'll use otherwise so you can see has a moment we control everything with just this image angle equals point Direction XY Mouse x + iy line okay which is point two are towards where we want to be unfortunately know if every other thing in here relies on that like the direction we shoot a bullet turn just relies on our current image I'm going so on we've kept that fairly clean which is good because that makes it very quite simple for us to implement our controller aim up here we can just basically manipulate image on Google to be whatever we need to be and then the rest will just sort follow suit very similar in the way how we just did walk like and walk right and make that work slow no type if old player dot just as we did before we're by getting a value from another object controller equals zero okay and then I'm going to cut this line this image angle equals with our mouths our mouths aiming line fundamentally and stick it in here okay so now that we only do this if we are currently using keyboard and mouse because we even keep our Mouse controller will be 0 if we do any kind of input on the the joypad or the gamepad controller b1 and will start ignoring this okay so it's just important because otherwise gun will just aim to wherever the mouse is on the screen and it'll overwrite everything else which we don't want or it'll get overwritten depending which order either way not what we want then we'll talk LS again that's here so this now assuming the controller is one whatever we put in here and we need to do a bunch of things the first thing we need to do is get the axis value of our right analog stick in the horizontal and vertical direction okay I'm gonna type two bars because we're going to use these a couple of times so by putting enjoy watching TV controller H see it I'll turn yellow in a second or probably want to finish the liner now there's excess value open bracket device zero is always in G K axis R for right analog stick H for horizontal okay and that controller H that's going to be our horizontal axis okay let's copy that line paste it proposed controller Z this time for vertical and just change the H from axis are h2 to a V okay this needs to be a state red gamepad axis right analog stick vertical okay so again that's between -1 1 4 how left and right it's being held and minus 1 and 1/4 how up-and-down X being held it has or is feminine so I think you get Y 10 so once again we need to check the dead zone okay we need to make sure we're not like it like not just sort of just hanging loosely by default on it like nor point nor one in a direction or something like that because then they will override our mouse input instead when we don't want to just by having a control type in and we'll do all kinds of things I wanted to do so I'm going to try if abs so again just returning always positive controller H greater than or point two so I think I've fairly straightforward right so whether or not our horizontal axis is greater than or point to just the same the support for our ABS controller v20 so either one okay if either one of these is more open to it means we're using that controller so we're interested in actually aiming the one bad thing I guess is that this is nestled inside already assuming the controller is one I suppose it would be a good idea if you were carrying on one controls to do to do this sort of section outside of this loop somewhere as well and just have it as another way to set controller to one but we're just keeping it simple for now and you will get the general idea in jest at her defense which is what's important so assuming we passed that deadzone I have I can close this bracket properly I think I have yet so assuming all that's the case controller angle which is about is just zero at the moment is going to be set to point direction nor north so I say the top left of the screen but we're not really using it to mean the top level screen we're just using as a basis to work from so that we get an angle based on negative one and one and negative one and one left and right okay by using zero zero and point direction in this way we can get an angle just based from those two values there between nine one which is really really useful controller H can control B so think about it if so you are holding all the way to the right actually that's let's say we're holding all the way up for example case like minus one and we're not holding anything left and right on on the analog stick so just to make it absolutely clear I guess presume that we're doing that on the analog stick a which is holding dead hook what does that mean um that means the control of V is going to be negative one okay because if we fully in the up direction and control H is going to be zero because we're not moving not to write at all so the point direction between zero zero in our in our room but sort of not really and zero and minus one is going to give us the angle of book okay that's going to give us ninety degrees okay there's zero degrees to the right so ninety degrees from that anti-clockwise is up okay so anything the term is ninety so it's a really useful way of just getting the exact direction that we want then once we've got that we stay outside of this image on door equals four angle okay the reason we do this do you think separately is so that we don't overwrite constantly um when it when it's zero okay so when we're zero when we're not holding the analog stick at all we want to just leave ourselves aiming wherever we're going last otherwise we'd have it if we just set image angle that equal this all the time actually that might work out but we have to be careful to make sure we don't ya tell me we don't make it so that the angle just always bases in the direction of our holding so if I run so if it looks like I'm struggling to click on things when I click on the top left is because my webcam so though showing you the controller is counter in the way so I literally can't see my cursor when it's here on the screen anyway but in the game now if I move around you see the moment you see how that snaps like when I start using the keyboard mouse again like so like you see like a goodness following now but now when I move to the right you can see it snapped down like that and it's using whatever my controller Angeles but if I started move around only on stick now I can't really do both these things on the webcam so you just have to trust me that I'm doing it you can see it's not following the mouse rationally you theater cane wherever I point the stick and it stays there importantly okay so it stays there and doesn't just rank on Mouse orders and first crack straight back to zero it stays wherever it should be okay and then if I come back see mouse and keyboard it comes back to and following the mouse around okay so the last very last thing we need to do now is to make is we can actually shoot which is going to be very very straightforward literally just going to add here where this is the section in the good way we fire right this mouse check button and be left and then or firing delay stuff on top I'm going to open a bracket at the start of that and close one at the end of it just so we've wrapped it in some more put some space in there and I'm just going to say it for all I'm going to say it along this it all game card button check twice 0gp stover I'll be okay so if either the left mouse button ping track or just that shoulder bone is being pressed but only enough are be think I'll be would refer to the well are be on okay 365 add reversed only enough to whether or not holding down the trigger funnily enough I know there is a way to get an access out the trigger signal see how much you approach me I don't I don't know all the specifics I normally work with controls a lot but uh yeah that's making my trigger do it so now I'll put the gamepad camera one when I started seeing kind of see like all the stuff in action if you were actually designing this game with controller input in mind one thing you might do I suppose is make it so you can just press Phillip to jump on the the analog stick rather than pressing a just because jumping and aiming is kind of at the same time is a bit impossible now see a piece or swap between the two or Italy's more more difficult which working for a specific advantage - sorry okay that our view per visit would keep that it would be a kind of an advantage to playing on keyboard if you were to design a lineup but only all that stuff up to you that's just as game design you learn as you go right but that's it that's that's adding controller important humans here or comes back and all still works on keyboard and mouse just as well that's that everything that's that's controller input we haven't dived into too much detail a lot of different things a lot of different things you need to watch out for and stuff when giving the controller input but what I would recommend to you is if control panel is important to you go read the documentation it's very very clear and gives you lots of moles of info and all the stuff you need to be dealing with and how to get the most out of your controller input okay so just press f1 go to the gamepad section as a whole section it's written very well and it will teach you how all of that stuff works from this point onwards we're not really going to delve much into controller input and all that kind of stuff we're going to leave this in the game this also you know later on if I release the source code or whatever you can see how it all works but but this will be the last time we're really touching on the Empowered stuff or this particular series okay thank you all very much watching and I'll catch you guys in - no actually in three weeks because I'm going to have that the next week I'm having some time off to spend with my girlfriend who's just got back from a five-week training course thing so we've got a week off together that we're going to have and so I'm going to be back in two weeks with a another video on not platform of stuff but I think it's titled collisions and the week after that we'll be back with the platform series okay so three week waits for the continuation of that boleterĂ­a sorry about that but we all do need a bit of time off now and again and yeah so I'll see you guys ten thanks very much for watching thanks guys thank you as always to my awesome patreon supporters this week we've just hit over $500 a month in order to celebrate that I'm putting this video up completely and free and it's going to stay that way forever because ultimately that's my goal is to be able to make videos completely and free that are just a good quality stuff and to do that I need your support on patreon we'll get closer and closer to that goal every day but in the meanwhile a huge special shout in particular to Dan in a mule Steven Hagen Alex Ray Charles Montgomery Roxanne Harold Guidry Jason McMillan Angel Rodriguez and Owen Morgan and to all very much for your continued support or see you guys in two weeks
Info
Channel: Shaun Spalding
Views: 149,586
Rating: undefined out of 5
Keywords: Game Maker (Video Game Engine), Tutorial, GameMaker Tutorial, GameMaker, Game Development, Indie Games, Tutorial Series, Game Maker Studio, Making Games, How to make games, GameMaker Studio 2, GMS, GMS2, Gamepad, Controller, Joystick, 360 pad, ps4 controller, dualstick, analogue stick, platformer
Id: 5WGesYns4mk
Channel Id: undefined
Length: 29min 26sec (1766 seconds)
Published: Fri Aug 18 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.