Gamemaker DND Platformer Tutorial - #17 Controller Support

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you're interested in making a detailed platformer just like this one with in game maker using drag-and-drop then keep watching g'day gamers and welcome back to our drag-and-drop platformer series one thing that has been missing from our game has been controller support but even if you haven't done the rest of the course you could still follow along on this one and use a similar method to add controller support to any of your drag-and-drop projects so to add a controller we need to modify how we are capturing our current inputs I added them in a simple way back at the start of the series but to offer dual controller or keyboard support we need to improve them instead of applying the key presses directly to our HSP let's capture the keys being pressed and then use the Cal command script to apply them the first thing I'm going to do is change the get input so it captures the key being pressed instead of applying it directly to our HSP so for pressing right I want to set the value of right to be true and for pressing left will set left to be true just like we do for up now I'm going to take a copy of the up both of those ctrl-c ctrl-v and make sure I drag them underneath the up and we'll set this four down and we'll set down to be true as well now because I'm using down and I'm using left and right we need to declare those variables so up here we're assigning our variables let's create some new ones so let's have a write and I'm going to set that to false going to have left false and then we also created a down so I'll set that to false as well I'm going to add two more here which you're going to use when we capture a gamepad so I'm going to have a H move standing for horizontal move I'm just going to set that to zero and then a V move and I'll set that to zero as well now this resetting of the variables is something that we can use elsewhere so I'm going to do my favorite thing and that is move it all into a script but let's grab that and let's cut that out let's create a new script I'm going to call this reset input and let's just paste that in now when we go back to our get input we need to call that script here so drag the execute script block across and let's select that so when we run our get import we're resetting our variables first if we have control then we're capturing the input but before we capture the input for the keyboard let's capture the input for the gamepad I'm going to do that just here so let's create a new script to do that and we're going to call this get underscore input underscore gamepad so just here we can actually execute the script and let's call the get input gamepad right there so back in that script this is where we want to capture the input from the gamepad now game maker can detect up to 12 controllers and each one is allocated the port number from 0 to 11 but what we can do is loop through those values and see if any of them have a controller attached now an Xbox controller will be found on port 0 while a ps4 controller will actually be found on port 4 so to make it easier as soon as the controller has been found we will use that one and not look for any others also since modern controllers are analog the sticks have a dead zone which means how far they need to move until they are recognized the analog sticks are not perfectly centered and this stops unwanted movement so let's set that value first then create our loop I'm just going to set a temporary variable and I'll call it dead underscore zone and I'll set it to zero point three but the analog input is captured between zero and one so zero point three means you have to move the analog stick a little bit before it starts to recognize that it has some movement now let's create the loop that will loop through the ports so I'm going to just search for a for loop and drag that in and we're going to run it from 0 up to 12 which means it'll go from 0 to 11 and this is temporary we don't require for this variable afterwards so if we type in gamepad and see there's a bunch of gamepad code blocks I'm going to drag in the get gamepad connected code block and the gamepad port there we want to look for is I because that's our loop and we're going to store it in a variable I'll just call it connected and this will be set to true if this port has a gamepad connected to it so if it does have a gamepad connected then we need to do something with it so we can say if this variable connected it's equal to true and the first thing we want to do is use our dead zone so we go back to our game pads we can set our dead zone here and we want to set it for the gamepad port which is I and the dead zone is our variable created it's called dead underscore zone so the next thing we can do is capture the axes input so that's whether the controller stick is being moved there's one here called get gamepad access so let's drag that across and once again the port we're looking at is I now the axis we want to get is the gamepad axis left horizontal and that's what this stands for you can choose different options here we want the left stick the horizontal of the left stick now I'm going to store that in a variable called H move and that's what we created earlier standing for the horizontal movement so this will actually return a value between minus 1 and 1 and that value is a real number meaning it's a decimal so minus 1 means you are pushing all the way to the left on the analog stick one means you're pushing all the way to the right now we can also store the vertical movement so I'm going to do that I'm going to take a copy of this and paste it and this time I'm going to look at the left stick vertical and I'm going to store that in now V move now make sure you don't take temp we want to keep these and use them in calculator now the other thing we want to capture is if the player is jumping so we can grab a gamepad button pressed which means it was only pressed once and we're looking at the port of I and the button we're looking at is the action button so on a controller top normally as your Y on an Xbox controller you've got X the left a for bottom and B for right but for our jump I'm just going to use the action bottom button now if that is true we need to do something we need to set our variable and the variable we set is that jump variable we want to set jump to be true now as I mentioned I don't want to process any more controllers if we've captured this input then I'm just going to use that one so we can do a break and we can place that down the bottom and what that means is that we will jump out of this for loop not run anymore once we get to the end there but now that we've captured the input let's use it inside our cow move meant since we will be using input variables here and our enemy and coin use the Calculon script - we need to give them their own script but then we're free to make changes on this one so let's duplicate the Calculon script and I'm going to call the new one Calculon underscore other and if we go into our objects and go to our enemy we see that at one point they are using calc movement so we need to just change that and ensure they use the other script and let's see any time they use it and that coin also uses calc movement and that's in the object parent here it is here so let's change that to other as well so that lets us use Calment now for the player only so when we introduce analog movement meaning it's not just stop and go but instead it allows decimals like acceleration we can use that to give our player as smoother movement we can have slower walking speeds for example so let's add a variable to control that acceleration go back under our player let's go down to the bottom of our variable definitions and let's add an acceleration I'll just call it a CC and I'm going to set it to 1.1 and this is a real so now in our movement we can set our HSP based on our H move variable and our acceleration so if we go here and we assign variable H SP to be whatever heat move is times by acceleration and that's why I set it to one point one because this value times one point one will increase over time if we take relative so HSP will get larger over time now if we run that you'll see it will continue to accelerate as there is no limiting of speed you can run that and test it yourself but we can add a clamp function to ensure it never goes above the walk speed value but let's add another assigned variable and this time we want to make sure our HSP is clamped and there's a function called clamp so I'm going to say clamp and you'll see that it takes the value and then what you want to set it as the minimum and the maximum but we're going to take heat as P and I'm going to set the minimum to be minus walk speed which is our variable we used before to calculate our walk speed and positive walk speed so therefore HSP will never be smaller than minus walk speed or larger then a walk speed now let's run this and try it out press play now make sure you have a controller plugged in and we can move and jump and we can actually move slower so that works great we have a nice slow gradual movement speed and also a slight acceleration when starting to move now you notice one thing and that's the keyboard no longer works so let's fix that up now when we were back on our kid input you remember that if we are pressing right or pressing left then our right assets are true or our left to set to true so if that's the case we can just set our H move to one or minus one but just above up the top of calc movement we can ask a question and say if our right is equal to true well then let's just set our H move to be one and I'll take a copy and paste to that make sure you drag it back under and we want to say if left is equal to true then H move is minus one and that will just ensure that we can use H move down here with our HSP assigned variable now we are using H move here but we're not using V move but let's drag across and if check and let's look at our V move variable now if V move is less than zero now with game maker if the V move is less than zero that actually means that we're pushing the analog stick up and if we're pushing the analog stick up then we want to set our up variable to be true and then we can take a copy and paste to that make sure it's back under and will ensure that if our V move is greater than zero it means we're pushing the analog stick down and therefore our down is true now remember we're capturing this V move if we go back here and look at our gamepad input we're capturing it right here and that's how we're determining whether V move is positive or negative so if you want to test this you can just change the up to jump and run that and that will just allow you to see if the V movie input is actually working so now if I push up I'll get the player jumping now you might notice something that happens here and that's when I run into the enemy I still just charged straight forward to them I don't actually get knocked back anymore so our knocked back is not actually working but first autos changed that jump back to up and let's go have a look at why that's happening so in our player in a step event if we go down and look at our knock-back script we're actually using calc movement here so because we're not capturing any input though how H move stays at what it was when we moved into this state and we continue to move forward so we could either 0 H move or we could just use our reset script to do it now let's just use our set script and that will work so you now have a dual control method meaning you can switch between keyboard and gamepad so let's test that out now we can bump into the enemies and we should get knocked back which we do and we can also at the same time use keyboard or gamepad either one will work so we have dual controller support which is really useful in your game now I want to end with a huge shout-out to my patreon supporters they helped to build the channel and allow me to continue producing content for you this month I want to appreciate the generosity of my legendary supporters Dylan Jackson Fox and Raven ant and Chris thanks as well to my epic supporters sky devil pom Sheldon intp Salvatori Kappa Leno and claypot as well as my other supporters if you want to get access to source code special videos early access content and even udemy courses the link is in the description there are links in the description to my udemy game course thanks for joining me I'll talk to you in the next one [Music]
Info
Channel: Slyddar
Views: 2,223
Rating: undefined out of 5
Keywords: gamemaker, platform, platformer, drag, drop, dnd, dragndrop, drag n drop, tutorial, peter morgan, one way, oneway, fall, make games, shaun spalding, heartbeast, spalding, yoyo, gms2, gms, advanced, learn, drag and drop, collision, make game, jump on head, enemy ai, horizontal, collisions, coins, collectible, items, objects, slyddar, peter, morgan, 1.4, 2.3, slyddar studios
Id: uC8DZGEAke4
Channel Id: undefined
Length: 15min 52sec (952 seconds)
Published: Sun Jun 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.