UE5 C++ Enhanced Input - 5 - Directional Input to Move a Character

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] welcome back now the last few videos we talked about enhanced input and how to convert input mechanics from the old way to the new enhanced input system in this video we're going to convert another project to the enhanced input system and this one contains a few more types of input this character is from my new Unreal Engine 5 ultimate game developer course and the project I have here has several things going on the character can move around they can press a key to equip a weapon and pick it up they can put the weapon on their back they can take it back off they can attack with the left Mouse button and the right Mouse button is a Dodge ability which uses up stamina now these actions also result in other types of gameplay mechanics such as combat and eliminating enemies in the world and things like that so we have quite a few inputs linked up to this character and all done with axis and action mapping so we're going to learn how to get directional input like this into the enhanced input system as well as other actions like attacks equipping weapons and so on so the first thing we need is to make sure we have enhanced input enabled and this is a 5.1 project we see Unreal Engine version 5.1 which means enhanced input is already enabled if we search plugins for enhanced input we'll see that it's checked we have access to the enhanced input system but all of the input so far in this project is done the old way if I open my project settings and go to input I see that I have some action mappings and some axis mappings now depending on where you are in the course you won't have all of these yet but we'll go through all of them one at a time to show you how we can get this converted into using enhanced input so we'll start with movement notice I have some axis mappings I have moved forward and I have a move right now move forward is mapped to W and S as well as the up and down keys and S and down both have a scale of negative one to scale that input so we can move backward and move right has d and a for left and right and it looks like this should be left and right actually so we might as well go ahead and fix that by selecting right for the one with a scale of one and left for the one with a scale of negative one so now D and right would result in right movement and a and left would result in left movement now this isn't that important because we're about to ditch all this and go into the advanced input system for our inputs but before we do that we'll first go over how these work so I'm going to open up my character class it's called slash character now you're going to see more things here than you might have depending on where you are in the course or if you're just watching this video straight from YouTube you might have a complete completely different class with its own movement input but movement in this class works with the move forward and move right functions and these are bound to those axis mappings with the same name so if I go to set up player input component I can see that move forward and move right are both bound to their respective axis mappings so we'll first work on converting these into using enhanced input in the new way and we've seen in the previous videos that we need to add enhanced input to our build file so we have access to that module so I'm going to go into Source slash that's my project name and find my build.cs file and in addition to all the other modules I have in here I'm going to add enhanced input and I'll go ahead and save and close out of both the editor and visual studio and regenerate project files deleting saved intermediate and binaries so deleting those folders I can generate Visual Studio project files and open up my you project so I can rebuild the slash module and I can also open my visual studio solution and now that I have access to enhanced input I can use those classes so I'm going to close my build.cs file and go back to slash character so now that we're ready to start using enhanced input we're going to need some input actions and an input mapping context for my character and since we're going to work on movement I'm going to make a move input action similar to how we made a move input action for the bird in previous videos so in my characters folder I'm going to make a new folder and call this input and I'll open that and make a new input action so I'm going to right click select input choose input action and I'm going to name this IA underscore and we'll just call this movement since we already made a move input action for the bird now that's okay if we have different input actions with the same name in different folders but I'm going to call this movement just to be different and we can open movement and its value type is bull by default but we're going to want at least an axis 2D I'm going to use axis 2D for movement so we can move in two Dimensions forward and backward and left and right now we also need an input mapping context and we can add our new IA movement to that context so we can right click go to input and select input mapping context and I'm going to call this IMC underscore slash because that's the character I plan on using this with so I can open this up and we can add a new mapping to this by clicking plus expanding mappings and I can search for my I a movement I'm going to go ahead and select that now we have to remember that IA movement is a two-dimensional input action it takes an X and A Y and if we add a single key then by default that key is going to fill in the X for that two-dimensional Vector that means we're going to need to at least modify it if we want to press a key and fill in the Y for the two-dimensional Vector I'd like to use the X for left and right movement and the Y for forward and back awkward movement so left and right is simple I'd like to map my d and a keys and perhaps even the left and right arrow keys so I can click this little keyboard icon and press D and I don't really need any modifiers for D because if I press the D key then I want the X component of my IA movement input action to me one and if I'm not pressing it I want it to be zero and that's the default Behavior now I can add another key to this input action and click the icon and press a now this will be for when I'm moving to the left and I'm going to want that X component to provide me with a value of negative 1 which means I'm going to need a negation modifier so I'm going to click plus under modifiers and select negate and we know that negate will negate X Y and Z but in this case it doesn't matter that we leave these all checked because a single key is only a single value it's not two dimensional and as we've learned by default a single key will fill in the x of a two dimensional input action so the negate is all we need for the a so that takes care of d and a now what about moving forward and backward the W and S keys well let's click plus and we'll click the icon and press W and for the W key I want to fill in the Y component of our two-dimensional Vector in this input action to do that we have to Swizzle that's a type of modifier so we can add a new modifier open the drop down and here it is it's called Swizzle input axis values and if I click on that and expand the index we see the order changes here we're changing this input action key mapping so that it fills in y first so we can leave that at its default and the result of this is when we press W triggering this input action means we'll get a value of positive one in the Y component of the 2D vector here in this input action now we want to move backward with the S key so I'm going to add another key and click the icon and hit s and for this one we need to do two things we want to Swizzle so the S input data goes into the Y component of the 2D vector and we also want to negate so it's negative so pressing s provides us with an input data value of negative one so we'll go ahead and add a modifier and choose Swizzle input axis values this will ensure that pressing the S key will give us a value of 1 and the Y component of this 2D input action but we also want to negate so we'll add a second modifier and choose negate now we could do the same exact thing for the left right up and down keys but it's the same process so I'll leave that to you as an optional challenge if you want to use the arrow keys as well as wasd I'm okay with just wasd so now that we have an input action and it's added to our input mapping context we need to set this up in C plus plus so that my character can use this input mapping context Now setting up some of this is review if you've watched the previous videos we know that we're going to need a variable for our input mapping context as well as variables for each input action that we want to add to our local player so I'm going to do everything up here under begin play in the protected section I first need a u input mapping contact variable and for that reason I'm going to forward declare that type up here with my other forward declarations so forward declaring U input mapping context and I'm also going to need variables for each of my input actions and those are of Type U input action so I'm going to go ahead and forward declare U input action as well and now I can add my input mapping context variable so it's a u input mapping context and I'm going to call this slash context and we're going to set this from the blueprint so I'm going to give it a U property with edit anywhere now I don't see an immediate need to expose it to the event graph so I'm not going to give it blueprint read only or anything but I will give it a category of input now I have a new input action so I'm going to add a variable for this and we'll set that in blueprints as well to our new asset we created so it's going to be a u input action and this is for movement I'm going to call it movement action and it'll get the same U property because we plan on setting this in the details panel now we need to add our input mapping context to the character and we're going to do that and begin play so we'll go to the character's CPP file and search for beginplay and we'll just do it right here now again if you're taking the course and you're early on in the course don't worry about any of the other code you'll add that code later in the course when you get to that point we're just talking about movement for now so to add the input mapping context we need to access the enhanced input local player subsystem and we've seen in previous videos when we took a look at the third person template that we need these three includes components slash input component enhanced input component and enhance input subsystems so I'm going to add those includes right here in my character class at the top so right here we'll go ahead and separate those with a line there so back to begin play we first need our player controller and we need our enhanced input local player subsystem we did all this already a couple times here's the third person template project where we're doing it here I'm going to Simply copy these this is review at this point so I'm going to paste it here the only difference is I'm calling add mapping contacts to add our input mapping context using the subsystem but our mapping context is called slash context so that's what I'm going to pass in here and just to remind you we're declaring a local variable in the if statement so it's only in this statement's scope same with our player controller it's only in this outer if statements scope and we're calling get subsystem from the u local player class specifying the enhanced input local player subsystem and we have to pass in the local player which we got from player controller from there we can add the mapping context specifying the context we want to add and we do need to set this in the blueprint so we can't forget that before play testing but after these lines we now have our input mapping context added and all we need to do is bind some functions to our input actions we only have one input action so far it's called movement action so we're going to want a function to bind to that so we can make that function here it's going to be a void function and we'll call it move now we've seen that this move function needs to take an input if it wants to receive input action values and that input is of type const f input action value reference and we'll call it value and we've also seen in previous videos that we need to include the type input action value dot h if we want to use that struct as we can't forward declare a non-pointer variable so we're going to include that up at the top above the generated.h file and now we have a function callback that we can bind to our input action so we'll create a function definition for it and then we can go about binding this to our movement action and we do that in setup player input component so we'll find that and we'll be replacing move forward and move right with our new move function so the first thing we need to do is take our player input component and cast it to an enhanced input component we've seen that we needed to do that in the previous videos and we do it this way by using cast or in this case cast checked to U enhanced input component so I'm going to copy be the interior of this if statement and make a new if statement declaring an enhanced input component local variable and we can use this to bind our move function to our new input action which we called movement action so let's do it we'll take enhanced input component and call bind action first passing in our input action which is called movement action we also choose an e-trigger event we want to call this function when the input action is triggered we have a user object which will be this and we have a callback function which will be the address of the fully qualified move function so it'll be a slash character in my case move so now we have our function bound and if that input action is triggered then our move function will work so we just need to decide what to do in move let's go ahead and find that function and the first thing we can do is get the value of the input action value so we can take value and call get using the template type corresponding to this input action and this is a two-dimensional Vector so we'll use f Vector 2D and we can store this in a const local F Vector 2D so we're going to say const f Vector 2D and we'll call this simply movement vector and initialize it and we can now get the X and Y values and the X and Y values will depend on what keys we're pressing we set it up with the right modifiers so that if we press W then the Y component of this F Vector 2D will be one if we press s it'll be negative 1 if we press D the X component will be 1 and if we press a the X component will be negative one now it doesn't really matter what we put inside this function at this point we can choose whatever we want earlier in the course we simply get the forward Vector of the character and move the character forward later on we do something more complex when we learn about rotation matrices we get the forward direction of the controller and move in the direction we're facing so depending on where you are in the course you can just use the logic that we added to move forward which would be basically to make a constant filter called forward and initialize it with Git actor forward vector and then simply call add movement input passing in that forward vector and then a scale value in this case because we're moving forward we want to scale this by the Y component of the movement Vector because that's how we set it up in the input mapping context pressing W and S will fill in that y component and it'll be positive or negative depending on whether it's w or S so add movement input can be scaled by movement Vector dot y now here's the cool thing we can also take care of moving to the right if we want to do that by making a const f vector and calling it right and we can call git actor write vector and from here we can call add movement input passing in right and scaling by movement vector and this time we want the x value because that will be positive or negative 1 depending on whether we're pressing D or a so this will move us according to our actual characters forward and right vectors it won't matter which direction we're looking in all that matters is which direction the character is facing so this is the first implementation that we do in the course now later in the course we get a little more complex by finding out our controller's Direction so once you get to that point in the course you can come back to this video and see how we do it here we would make a const f Rotator called rotation and we'll get our controller and call git control rotation and you learn all about what controllers are and how they have rotation in the course we also want to only use the yaw for the controller's rotation we don't want the pitch or the roll so we can make a const f rotator called yaw rotation and initialize it with 0 for the pitch rotation dot yaw for the yaw and zero for the roll so we're zeroing out pitch and roll we just have the yaw now we want the forward Direction corresponding to the controller's rotation and in the course we learn about what F rotation Matrix is how rotation matrices can find a vector corresponding to a rotation and we do it this way we make a const f vector called forward Direction and we initialize it using F rotation Matrix so we create one of those passing in yaw rotation so we have a rotation Matrix corresponding to yaw rotation and from the rotation Matrix we call git unit axis not axes but axis passing in e axis X so the result of this is the forward Vector corresponding to the direction our controller is pointing at and from there we can simply call add movement input and we'll pass in forward Direction and we want to scale this by our movement vector and in this case we want the Y component of that as we'll be pressing W and S to move forward and we'll do the same thing to move right we can make a const f Vector called right direction and this will be the same thing F rotation Matrix yaw rotation we'll use that called get unit axis but this time we want e-axis y that'll get us the right vector for our controller and we can call add movement input passing in the right direction vector and we'll scale this by our movement Vector but this time we want the X as we'll be pressing A and D and those will fill in the X component of our input action so this is how we can have directional movement for our character now let's go back to project settings and input and take a look at those axis mappings again we took care of forward and move right and we only needed a single input action and a single callback function to handle both of them now we can handle turn and look up and this will be handled the same way that we handled it for the bird in previous videos so we'll need a look input action I'll go ahead and make a new input action right click input input action and I'll call this I a and I'll just call it looking so it's not the same as the other IA look we made in earlier videos and I'll go ahead and open it up and this one can be value type access to D just like we did before we need to add this to the IMC slash input mapping context so we'll add a new mapping we'll select from the drop down IA looking and for the input type will go to Mouse and choose Mouse XY 2D axis and if we don't want inverted Behavior we can negate the Y component so we can add a modifier click the drop down and add negate and expand that and only have y checked so that way we're not negating the X but we will negate the Y and if we want to link this up to a C plus plus function we're going to need an input action variable in our character so we'll go back to slash character and along with the movement action We'll add a new input action and we'll call this look action and we'll also need a new callback we'll make this a void function called look and it'll have the same input a const f input action value reference so we'll go ahead and make a function definition for look and we'll implement this in a second but first we have to take care of binding it to our input action and that's in setup player input component so we'll do that in here so it's basically the same thing as this line the only difference is that instead of movement action we're now using our new look action input action so we'll go ahead and replace it with look action and instead of move we're going to call our new function called look and now we don't need turn or look up as these were handling the rotation behavior of the camera in response to the mouse we now have this function that can handle it now if we look at our turn and look up functions we saw that all we did was call add controller yaw input passing in the value and add controller pitch input passing in the value that's all we need to do in our look function as long as we can get those values from our input action so from look we need the value from the F input action value well we can use value.get with an F vector2d so I'm going to make a const f Vector 2D and this one can be called look axis vector and we'll initialize it just like we did in move we'll get value.get specifying a vector 2D and all we need to do is call add controller pitch input and for the pitch we're looking up and down we want that y-axis value from our input action so we're going to get look axis vector dot y and we can call add controller yaw input and for this we want the look axis vector dot X as we're moving the mouse left and right if we want to change the yaw and that's it now if your game has some sort of checks in place in the code you'll want to make those checks here for example later in the course we'll check the action State and make sure that it's unoccupied in other words if it's not set to unoccupied this enum constant then we return so we can't move you can always add those types of checks in your callbacks for example my new move function can check the action State see if it's not unoccupied if it's not return and that will prevent us from moving when we shouldn't be so that takes care of move and look now we can go ahead and compile this and set our variables slash contacts movement action and look action in The Blueprint and test it out so I'll go ahead and close out of the editor saving all and we'll compile and launch from here in visual studio so launching the editor we can open our asset editors I don't have my character blueprint open so I'm going to open it and set the values we put them all in category input so we have slash context that's our input mapping context called IMC slash so we're going to set that to IMC slash for movement action we have IA movement and for look action we have I a looking so setting those we should now be able to press play and I should be able to click in the viewport and move and I can rotate and look around and if I press W I move forward I can move back left and right now if you're earlier on in the course and you haven't added animations yet don't worry we'll get to that but we now have movement and this is the most complicated part of the enhanced input system for the character now later in the course we add even more inputs aside from the move and turn axis mappings so once you get to those points in the course you're going to come back to this video at this point in time now the rest of these inputs are action mappings we see that jump is mapped to the space bar equip is mapped to the E key attack is mapped to the left Mouse button and Dodge is mapped to the right Mouse button now these are all one-offs we click a button or press a key and something happens once we can make input actions for each of these and they can all be simple they can just be Boolean input action types so let's make jump equip attack and Dodge so back to input we'll make a new input action called I a jump and we can open it and see that it's already set to Bool so we don't need to actually change this at all now we're going to want a variable for this in C plus plus and we'll want to bind a function to it now here's the cool thing about these simple Boolean input actions we only want to do something when this input action is triggered and we don't really care about the value of the bull if it's being triggered we want to jump so back in our character header file we can add a new input action variable and this can be jump action now my character already has a jump function it's overwriting the jump function inherited from the character class it's up here it's a virtual override and it takes zero input parameters well we can bind a function to an input action that takes no inputs unlike these move and look functions that take the F input action value we don't actually have to have a function take any inputs to bind to an input action so all we need to do is come into setup player input component and bind the action so once again copying the line we're going to change this to jump action and we'll bind our jump function which has zero parameters and that's it so we can close the editor again and we can compile and launch but first we might as well go ahead and comment out the binding for the jump action as we don't need it anymore and back in the editor we can open our asset editors and we want our character because searching the details panel for input we want to fill in that jump action with our new IA jump input action that we just made now we also need to add this to our input mapping context so we'll go ahead and add a new mapping selecting our IA jump and we need to select a key I'm just going to use the space bar and I'm not going to add any modifiers so we can test this by pressing play and pressing the space bar in fact I can use the console command show debug enhanced input and we now see that IA looking is triggered when I move the mouse IA movement is triggered when I use wasd and we can see which of those values is true at any given point in time and we can jump and we see that spacebar triggers the jump input action so now that we know that we can have a simple input action that takes a Bool and we can bind a callback to it that doesn't even take any inputs then we can go ahead and add any inputs that we want to be fired off that don't really require any information jump doesn't require info and the rest of the action mappings here in this project don't require information we have an equip attack and a Dodge and these function callbacks that are bound to these action mappings are not actually functions that take inputs they are void in fact let's go and look at them foreign is a parameter list function and so is attack and Dodge so it doesn't even matter what's in these functions all we need to do is create input actions for each one and bind the functions to them and then it's up to us to decide what happens in those functions and you'll learn in this course how to implement these behaviors but we're just going to bind these to some input actions let's make the input actions first so back to my input folder we'll make three more input actions I'm going to go to input input action IA and this one will be e key pressed I'll have another one and this one will be IA attack and we'll have another one and this will be I a Dodge now I just need to add these to my input mapping contacts and choose keys for them so we'll add another one this one can be e key pressed and we're going to map this to the E key we'll have another mapping this one will be attack and we can map this to the left Mouse button and finally we'll have another mapping called Dodge and I'll map this to the right Mouse button now we just need variables for these and the character class and to bind function callbacks to these input actions so I'm going to search for input action find my other input actions and simply make more for each new input action that we created so this one can be e key action this next one can be attack action and the next one can be Dodge action and since I already have callbacks for these binding those callbacks to these actions will be quite simple we'll just use the same line we've been copying and change the action we're passing in for this one we can do e key action and our function callback is called E key pressed next we can do our attack action and the Callback is called attack and next we can do the Dodge action and the function is called Dodge now we don't need any of this old code we can remove all of it and now all that's left is to compile and set our action variables in the blueprint and test it out so once again closing down the editor saving all I'll go ahead and compile and back here in the editor we can set those new input actions on the character blueprint so searching for input here they are we have e key action we'll select our new IA e key pressed for attack action we have IA attack for Dodge action we have I a Dodge now we can save and if we Press Play I can left click we already have movement and looking around but for E key pressed we have some functionality in that function for picking up items so I can press e and we put the weapon on our back right away if I press it again I'll pull it out attack Works Dodge works and of course we know that jump works now why did we put the weapon on our back right away well we can know if we use tilde show debug enhanced input and we press the E key we'll see that as long as we're holding that e key down it's triggered in fact if we continue holding it down we'll keep putting that sword back on our back and pulling it out because every time we press the E key it's going to trigger that function call and our function call is designed to first pick up a weapon but if we call it again right after well then we'll put it on our back so that can be taken care of by perhaps disabling input for a short period of time with a delay or a timer we could even restrict it here in our input mapping context with triggers for example if we add a trigger we can expand the drop down and see that we have quite a few different ways to approve the triggering of this action if we look at Pulse it says trigger that fires at an interval in seconds while input is actuated this will kind of limit us from spamming the Callback if we select pulse and expand it we have the option to trigger on start which means we'll get our callback fired off right away but the interval is set to one second so we have to wait a whole second in between firing off even if we're holding that key down so let's see what happens if we go and try to pick up a weapon it doesn't actually spam that callback we have to wait a whole second before we can use that callback again so if I hold it down you'll see that it waits a second before actually triggering it off again and you can obviously change that from one second for the interval to maybe 0.5 seconds so these triggers are extremely useful for controlling this Behavior you don't have to go and make a timer in C plus plus or use a delay node in Blueprint you can just add one of these triggers and I encourage you as you're making your projects to just kind of look at all the triggers and the modifiers and see how you can use them to make your life easier when you're making your games so now we've converted this project to use enhanced input instead of the old input system which means we can actually go to Project settings into input and remove these action mappings we can hit the trash can icon and remove all of them and it doesn't matter because we're not using them anymore we're using the new enhanced input system and all of our inputs are working and it looks great so hopefully these videos have demystified enhanced input for you as you can see it's not that complicated and we have lots of new capabilities and this can really expand what we can do as we're making games so thank you so much for joining me for this and I hope you have a lot of fun using enhanced input in your Unreal Engine projects I'll see you soon
Info
Channel: Druid Mechanics
Views: 52,818
Rating: undefined out of 5
Keywords:
Id: n3n8bqu7F9Q
Channel Id: undefined
Length: 39min 41sec (2381 seconds)
Published: Thu Nov 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.