UE5 C++ Enhanced Input - 1 - Core Concepts and Documentation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] in this video we're going to talk about unreal engines enhanced input system now I have the enhanced input documentation up and we're going to be discussing how to take a project and convert it to use enhanced input now before we start doing that I'd like to read through the documentation which explains the enhanced input system and discuss what everything means here so that we're sure that we understand what it is we're doing as we start using this new system for handling inputs and we'll compare it to the old system and get a bit of understanding about how the new system is improved and has more capabilities so as we're reading through the documentation I'd like to take a look at a Unreal Engine 5.1 third person template project now this very much behaves the same way as a third person template project in an earlier Unreal Engine 5 version we have the same behaviors but this project is using the new enhanced input system if you create a new third person template project it'll be set up to use enhanced input by default now you don't have to create a third person template project to follow along as we're reading the documentation if you're short on disk space you can just watch but if you have the space you might as well go ahead and create a third person template project just so we can open the assets and see what we're talking about and of course if you'd like to do that all you need to do is open your epic games launcher and launch Unreal Engine 5.1 or whichever version that you have you may have a later version by the time you watch this video now from the unreal project browser you're going to select games and then you can select third person and as we're reading through the examples we're going to choose C plus plus and you can go ahead and create a new project I call this one enhanced input project so if you're going to follow along make sure to select C plus plus and create this project now if you don't see these options here these example template projects that just means you need to go to your epic games launcher to your version of Unreal Engine and select the drop down right here and choose options and make sure that you have templates and feature packs checked that's going to take an additional 3.53 gigabytes in this case and then you should be able to create those template projects so I have the third person template project here and as I mentioned in 5.1 this template project is already set up to use enhanced input so that's going to allow us to take a look at the settings and the assets that exist for this project as we're reading through the documentation so let's go back to this documentation on enhanced input and see what it has to say and what we can learn from it so we'll go ahead and read through this it says that for Unreal Engine 5 projects that require require more advanced input features like complex input handling or runtime control remapping we'll learn what those mean the enhanced input plugin provides developers with an upgrade path and backward compatibility we'll talk about that as well from the default input system from Unreal Engine 4. this plugin implements features like radial dead zones now that just means if you're using say a thumbstick and your thumbstick is very close to Center you're not pressing it to the side very much then we won't be firing off events or functions that can be triggered from the input from that thumbstick so A very slight change in the thumbstick isn't going to do anything that's what a dead zone is we have corded actions and corded actions refer to actions that require multiple inputs at the same time such as pressing multiple keys or buttons on the GamePad to combine to trigger some kind of action chord comes from guitar chords where you're holding down multiple strings at different Frets and strumming a chord contextual input and prioritization so input that depends on the context and we can have checks performed on that input to filter certain actions from happening prioritization as in we can have multiple sets of input and we'll learn what those are called and how they work and they can each have their own priorities as well as the ability to extend your own filtering and processing of raw input data in an asset-based environment so this is a new system that allows us to have more complex input and we can have a better control over that input without over complicating our own gameplay code now we have a getting started section and it says to configure your project to use enhanced input enable the enhanced input plugin you can do this by opening the edit drop down menu in the editor and selecting plugins under the input section of the plugin list find and enable the enhanced input plugin then restart the editor so this process is for projects that don't already have enhanced input enabled by default again our third person template project already does so if I go back into this project I can go to edit and plugins and search for enhanced input and I'll see that it's already checked here we already have it enabled but if we didn't have it enabled yet the documentation says to check that and that's going to cause us to restart the editor so once we restart it says you can set your project to use enhanced input plug-in classes instead of the default ue5 input handlers so in enabling the plugin just adds the enhanced input system to our project but then we have to configure our project to use the enhanced input classes and for that it says to go to the edit drop down and choose project settings from there locate the input section under the engine heading and find the default classes settings these will initially contain the standard player input and input component classes so before enhanced input these were the classes used for the default player input class and the default input component class and these are classes used for us to handle input but now we need to use the enhanced input classes so in Project settings if we go down to the input section under the engine heading we can scroll down to default classes and again this third person template project already uses the enhanced versions of these now an interesting thing to note if you're using the old way a and you change these to the new enhanced input classes your code would still work because these new classes are based on the old ones they're child classes of those and therefore they inherit all of the capabilities of the old input system so you could take a project that uses the old way of handling inputs and change these to the new classes and it would still work now we see at the top axis and action mappings are now deprecated please use enhanced input actions and input mapping contexts instead so even though we still have backward compatibility with the old system that's likely to change in future versions which is why it's so important to learn the new way to handle input with the enhanced input plugin so back to the documentation we now know that to convert an older project we're going to need to change to the new enhanced versions of these classes so here we see the new enhanced input classes set in this screenshot now we see a note here it says from the editor press the tilde key to open the console command window and enter looks like there's a typo there the following command show debug enhanced input which will provide you with the next steps until everything is configured as required so we have this debug console command that we can use if our project is configured to use enhanced input so back to this third person example project if I press play and I'm controlling the character in the pi I can hit tilde on my keyboard we get this little console command box here at the bottom and I can type show debug enhanced input and as soon as I do that all of a sudden I see all kinds of debug information for one we have this box around the character and over to the left we can see a bunch of different actions which become green when those actions are triggered so as I'm moving my mouse we see action I a look IA stands for input action and when it's triggered we're seeing two values X and Y and that's because when I move a mouse we have two sets of information the input in the x-axis when I'm moving my mouse left and right and the y-axis when I'm moving up and down and if I press W you can see that this other input action called IA move is now being triggered and it's even showing me how many seconds I've been holding that button down and we can see that it too is a two-dimensional input with X and Y values y being positive one and if I press the S key to run backwards we see that I a move is triggered again but now y has a value of negative one pressing the a key is triggering I a move yet again but this time look at the x value it's negative one and of course pressing D triggers I a move yet again but its x value is one so there's something set up here where the wasd keys are all somehow linked to this thing called an input action called I a move and we're going to learn exactly how that works now I can press the space bar and we see in green action IA jump is triggered and if I hold the space bar down we can see that it's counting the seconds that have been holding that down and this one doesn't have X and Y values it looks like a Boolean value it says true and if I release the space bar it says false so these input action things will learn more about them as we go on can have different value types we see that look and move have two dimensional input types X and Y values and this input action jump is a Boolean either true or false so this show debug command enhanced input is really useful for debugging and seeing whether or not any of our given input actions are active so let's learn what those input actions are I held shift and hit escape to exit the pi session and we're going to head back over over to the enhanced input documentation so now we have a core concept section and this is going to explain to us all about the enhanced input system and how it works it says the enhanced input system has four main Concepts input actions are the communication link between the enhanced input system and your Project's code so we need to make something called an input action to link our code to the enhanced input system an input action can be anything that an interactive character might do like jumping or opening a door it can also be used to indicate user input States like holding a button that changes a character's walking movement to running input actions are separate from raw input and input action is not concerned with the specific input that triggered it but does know its current state and can report an input value on up to three independent floating Point axes and we saw that when we started showing the debug we saw that our look and move input actions are two-dimensional they had two floating Point axes and acts and a y Ai and we'll learn how to take advantage of those we also saw that our jump input action was one dimensional it just had a single Boolean value so we were able to see the state of those input actions dynamically with that debug it says as an example a pickup item action might need only an on or off state right Boolean indicating whether or not the user wants the character to pick something up while a walk action might require two axes to describe the direction and speed at which the user wants the character to walk so it's just giving an example of when you'd want to use a one-dimensional versus a two-dimensional input action so we know that we'll need something called an input action to handle raw input the next concept is input mapping contexts input mapping contexts map user inputs to input actions and can be dynamically added removed or prioritized for each user so even though we'll need input actions it's this input mapping context that allows us to link an input action to specific types of input so we'll need an input mapping context to link a particular key to an input action for example and we see that we can dynamically add or remove them at runtime or prioritize them we can actually have multiple input mapping contexts running at the same time for a given character it says you can apply one or more of these contexts to a local player through its enhanced input local player subsystem so there's something called an enhanced input local player subsystem in this class is what we have to access to add a particular input mapping context to a given character and prioritize them to resolve collisions between multiple actions trying to consume the same input so we might have multiple sets of input mapping contexts in fact it gives an example it says consider a single button that can either open doors while the character is walking around in the world or select an item while the character is looking through a backpack whenever the character is near a door you add the open door contacts talking about these input mapping contexts if the character opens the backpack you add a select item input mapping context that out prioritizes the open door context so that characters looking through a backpack while standing near a door can still select backpack items they wouldn't be opening doors when the character closes the backpack you remove that select item context enabling the open door context to take effect so you'll basically have two sets of input mapping contexts and each can handle what happens when you press a particular button or key but because these can be prioritized when both are active at the same time you only have one actually working and that's the one that will have the higher priority so this is great for more complicated types of projects where you might have the same button do different things depending on what's going on in the game it says this ensures that the user's inputs will be interpreted correctly based on the character's situation and prevents the need to program code at the input handling level with awareness of the door and backpack systems so this is making our code much cleaner and easier to maintain if we just had a single function to handle say pressing the E key then then without the enhanced input system we would have to go into our code and check the situation for the character perhaps check to see if it's close to a door or check to see if its backpack is open we may have variables in our game that we can check to see those things but with enhanced input all we'd have to do is add these input mapping contexts and give them priorities and then our code doesn't have to know about the door or the backpack so we know about input actions and input mapping contexts the next two concepts are modifiers and triggers modifiers adjust the value of raw input coming from the user's devices an input mapping context can have any number of modifiers associated with each raw input for an input action common modifiers include dead zones input smoothing over multiple frames conversion of input vectors from local to World space along with several others that are included in the plugin and it goes on to say that developers can create their own modifiers so in our input mapping context we can actually take raw input from the user as they're pressing buttons or moving thumbsticks or a mouse and we can filter it with modifiers that can change those values for any given action and for any given input now triggers use post modifier input values so the modifiers will modify that input first and then hand it off to a trigger so they use post modifier input values or the output magnitudes of other input actions to determine whether or not an input action should activate so triggers perform checks to decide whether or not some event or function should be fired off we have some criteria that triggers can check it says any input action within an input mapping context can have one or more triggers for each input for example taking a photograph might require that the user hold down the left Mouse button for a quarter of a second while a separate input action for aiming the camera is active so we have two different things right you have to be aiming the camera and then you have to hold the left Mouse button for at least a quarter second before you can trigger some sort of action in this case it would likely be taking a photo in the game by combining these Concepts developers can quickly set up input systems ranging from simple to complex and adjust these systems without having to change project code so this is huge this means that we can just go through the enhanced input system and change our enhanced input assets that we'll be making rather than digging into the code and changing it in order to change the features of our game so that's an overview of these Concepts but we'll be diving deeper and looking at all the moving parts so the documentation now begins to talk about input actions and how to make them it says input action are the connection between the system and your Project's code that's the enhanced input system you can create an input action by right-clicking in the context browser expanding the input option and choosing input action to trigger an input action you must include it in an input mapping context so that's another type of asset that we have to create and add that input mapping context to the local players enhanced input local player subsystem so it mentioned these things earlier now it's showing how to do it so it shows how to make an input action and we can go back into the editor and in the third person template under third person we have three folders of course the character blueprint is in the blueprints folder but it also has an input folder and if we double click that we see IMC default IMC stands for input mapping context so this is that input mapping context we were talking about and then we have an actions folder and inside of that here are the three actions that we saw when we were running that debug command we have IA jump which stands for input action jump I a look and I a move and of course we can always create new ones by right clicking going to input and choosing input action and now we'll have a new input action and we can double click it to open it up and here is where we can set up its defaults now there isn't a whole lot here I'm going to point out value type here and it says digital Bool but the drop down allows us to choose a axis 1D that's a single float value axis 2D two float values like our move and look input actions they had those X and Y axes or we can have a three-dimensional value type a 3D Vector so we know how to create an input action the documentation says to make your Pawn class respond to a triggered input action you must add an event to your blueprint graph So it's talking about how to set this up in Blueprint each input action generates an event with its own asset name for example if you created an input action and named the asset my action then the event name will be my action you can right click any open space in your blueprint graph and choose the appropriate event from the enhanced action events category in the context menu you can enter the asset name to narrow down the context menu's results so back here in the editor we created an input action just now called new input action now if I go into my character blueprint in the blueprints folder BP third person character and open the full blueprint editor I can right click and search for new input action and we see that there's an event here it's called new input action it has the same name as that input action and that's because this was automatically generated as soon as we created this new input action here and of course we can access it here so the documentation is showing us that with the example of creating an input action called my action now it warns you that if you have multiple input action Assets in different folders with the same name you will see multiple events with that name in this list you can hover over them with the mouse to reveal their tooltip which will reveal the full asset name including the folder path to make sure you select the one you want when you add the event to your blueprint it will be collapsed you can expand the node to see other execution pins and additional data so back to the project we can expand that we can see that there is more data here than before we expanded it and it was talking about having different input actions with the same name in different folders so why don't we go ahead and just demonstrate that for a second if we go back into our input folder and go into actions here's our new input action but if I go back out one level to this folder and make a new input action and let it simply be called the same name new input action well now if I go back into the event graph and type new input action I see two of them that's because two of them exist in my project so this can be a source of confusion and perhaps error but we can always hover over it and see its path we see that this one is in the actions folder this one is one level out so that's something to be aware of if you're creating different input actions with the same name I personally think that that would make things a little confusing so I would prefer all my input actions have different names but that would be a decision made on a project by project basis so I'm going to delete this new input action here that I put out here in the input folder and in my actions folder I still will have the first one I created called new input action so we can see that this event created once we made an input action has multiple output execution pins and multiple values that it can return so if a given action is actually triggered then this is the blueprint node that we could hook up some logic to we would hook it up to the triggered execution pin and then if that action becomes triggered we can fire off some blueprint logic we also have started ongoing canceled and completed we can also get the action value in this case I created an input action whose value type was Bool remember here in our new input action the value type is set to digital Bool but let's say I change that to say axis 2D and save that well now if I go into BP third person character we have a different type of action value it says vector 2D structure so changing the value type of that input action changes the action value that we can get from this node and a vector 2D can be broken if we choose break Vector 2D now we can see that we can get that X and the Y value so that's how we can use it to say move a character for example it says this is the fully expanded event node this will execute the appropriate pin or pins on each tick based on the state of my action so these are on a per tick basis so we know how to make an action let's talk about input mapping contexts input mapping contexts describe the rules for triggering one or more input actions the basic structure of an input mapping context is a hierarchy with a list of input actions at the top level so let's take a look at the input mapping context in the third person example project we can go back out out to the input folder and here's IMC default an input mapping context if we double click on it we see that here's that hierarchy we were talking about and take a look we have mappings with three different input actions IA jump IA move and IA look so that's that hierarchy with a list of input actions at the top level under the input Action level is a list of user inputs that can trigger each input action such as Keys buttons and movement axes so here's the top level of the hierarchy with input actions but if we expand IA jump we see that we have three different types of input linked to IA jump we've seen that we can press the spacebar to jump so the space bar is linked to IA jump but we see that also the GamePad face button bottom that would be for a console controller is also linked to this input action and in fact Touch 1 is linked to this action as well so if this example project were packaged for mobile and deployed to a mobile device touch one which is the first finger touching the screen as opposed to touching the screen with multiple fingers that's going to be linked to the Ia jump input action we can expand IA move and see that well we have quite a few Keys mapped to IA move we see wsad also the up down right and left keyboard keys and the GamePad left thumbstick to the axis so all of these different types of inputs are linked to the Ia move input action and if we expand IA look we see that the mouse X Y 2D axis is linked to this IA look input action as well as the right thumbstick on a Gamepad so it says under the input Action level is a list of user inputs so that's all those inputs we just saw wasd and so on it then says the bottom level of this hierarchy contains a list of input triggers and input modifiers for each user input which you can use to determine how an input's raw value is filtered or processed and what restrictions it must meet in order to drive the input action at the top of its hierarchy so filtering or processing that has to do with modifying the the input using modifiers and restrictions that it must meet in order to drive the input action is referring to those triggers which determine whether or not we meet some criteria so for example we can expand spacebar and we see triggers and modifiers and both of these have zero array elements these are arrays and you can click plus to add triggers or add modifiers but the spacebar has neither of those in other words if we press the space bar the Ia jump input action will be triggered we don't have any really restrictive requirements and we don't need to modify any raw data basically true or false whether or not we're pressing that key but if we go down to move and expand the W we'll see that it doesn't have any triggers added so we're not checking for any requirements before triggering off this input action but we do have a modifier and if we expand it we see that this modifiers array has this single element and the modifier is called Swizzle input axis values we're going to learn what that means but if we expand the drop down we'll see that the enhanced input system has multiple types of modifiers just built in and the documentation mentions that you can make your own custom modifiers as well but these are at our disposal if we need them they're going to be some of the most commonly used modifiers so Swizzle input axis values is a modifier that we're using when we press the W key we're taking the raw input and changing it with this modifier so we're going to learn very shortly what this modifier does and why we need it for this particular input so the the docs say any input can have multiple input modifiers and input triggers these will be evaluated in the order in which they appear in the list you create this is particularly important for input modifiers which use the output of each step as the input for the next what this means is that if our W key had multiple modifiers they would be evaluated from top to bottom and each time that input is modified by a modifier the result of that modification is passed on to the next modifier to perform its own modification so if we expand the S key now and expand modifiers under it we'll see that the S key has two different modifiers while the W key only does this Swizzle input axis values the S key does the same modifier again we'll learn about this in a moment but then it applies a second modifier to that input so when we press the S key this first modifier acts on that raw input data and then passes it on to the next modifier which is called negate now you can imagine what a negate does it'll take a positive value and turn it into a negative or take a negative value and turn it into a positive so when we press the S key we're first swizzling it then the result of that modification is passed to the negate and if these were in a different order say we had negate first and then Swizzle well then the raw input data would be negated first and then passed on to the Swizzle if these were reversed now we saw that iamove was a two-dimensional input we had X and Y values and that's why if our negate is expanded if we expand index one here we see X Y and Z so negate can negate up to three axes of input if our input has multiple Dimensions to it X Y and Z we could check or uncheck any any of these if we wanted to say negate only one of them or some of them now the doc's saying to create an input mapping context right click in the context browser and expand the input option and choose input mapping context so pretty simple so back in the editor if we wanted our own input mapping context we can simply right click go to input and choose input mapping context now it'll be called new input mapping context by default and if we double click on it we see that it's empty but we can add input actions by clicking plus and as soon as we do that we have the option to select from the drop down any of the input actions that our project contains for example we could choose new input action and here is where we would link up keys or buttons that we wish to be associated with that action we could select them from a drop down and choose any type of input here and of course there's a shortcut by clicking on this keyboard icon and say pressing a key on the keyboard that would automatically link up the a key to our new input action in this input mapping context and of course new input action can be linked to multiple keys if we wanted we can click plus right next to the input action and choose say another key maybe the D key so pressing the a or the D Keys as long as this input mapping context is currently active would trigger off this new input action and we can modify the data for example let's say we want to press the D key to move right and the a key to move left well we could simply use a modifier and negate the raw input value for a so that it would become negative we'll see more how to do this very soon so the docs say you can populate your input mapping context with all of the relevant input actions we just added our new one to our new input mapping contacts for a simple project you can put all of your actions into a single input mapping context more complex projects work best with multiple input mapping contexts since a local player can have more than one active input mapping context at a time as an example you can give a character who can swim walk and drive vehicles multiple input mapping contexts one for common actions that are always available and always mapped to the same user inputs and another one for each individual mode of travel developers can then place the vehicle-related input actions into a separate input mapping context which are added to the local player when entering a vehicle and removed from the local player when exiting the vehicle so in our case we know that the template project already has these basic movement input actions to handle jumping moving and looking right now we created another input mapping context and we may wish these to trigger different gameplay logic perhaps when opening an inventory menu or doing something else and our character can still have this default input mapping context but in some situations we may wish to add this input mapping context so that certain keys will do different things because they're associated with different input actions and the docs say that doing this helps optimize and prevent bugs by ensuring that inappropriate input actions cannot run so whenever we have any given input mapping context active then only those input actions in that context will be able to be triggered additionally using mutually exclusive input mapping contexts will help avoid input collisions therefore when a user input is used for different input actions the input will never accidentally trigger the wrong action so we just talked about that we can have separate input mapping contexts where only one or the other is active at a time it says see the sections on modifiers and triggers for more details so we'll get to those very shortly but here's an example of an input mapping context that has the Run action on it where it's linked the GamePad left thumbstick to this run action and the Run action has a trigger this particular trigger is called a hold so in this case when we move the GamePad left thumbstick this hold trigger is going to make sure that our input action doesn't get triggered off until we have the actuation threshold of 0.5 seconds if we move that thumbstick in a shorter period of time the action won't be triggered off so this allows us to make sure that that thumbstick is being held at least for 0.5 seconds we see that this particular run action has a modifier as well it has a dead Zone and you can set some parameters here so when moving that thumbstick here are the thresholds that determine whether or not we even try to trigger this input action and if we're not moving that thumbstick away from the center enough well then nothing will happen and here's the description it says this input mapping context shows an input action for running this can be activated by multiple inputs including a gamepad's left thumbstick deflection here we're only seeing that Gamepad left thumbstick but we could add other inputs as well and it says it combines both axes into a single input so in other words how far left or right we're moving the thumbstick and how far up and down those are combined into a single 2D Vector it says that inputs raw value will go through a dead zone input modifier That's The Dead Zone we just talked about and the resulting value will be sent to the hold input trigger to drive the Run action input action so the modify fire changes the data right it'll return zero if we're not deflecting that thumbstick enough and then after all modifiers that data is passed to the trigger and the trigger will check its own criteria such as have we been holding that button long enough and here we're showing how you can add multiple types of input to a given action and that shortcut key that we discussed it says there are a large number of input bindings available in the drop down menu right so you can select spacebar or Gamepad face button but to select your input binding more quickly click the small button to the left of the drop down and then press the key or button you want to bind so we saw how that worked and here now we're seeing an example of binding multiple inputs to Any Given action and we've seen that in the example template project it says this simple input mapping context supports input actions for running and jumping now once you have populated an input mapping contact text you can add it to the local player associated with the pawns player controller so in any given game if you're controlling a pawn or a character which is also a pawn you have a player controller you need to access that player controller first to add an input mapping context so that that player can provide input to control the pawn it says you can achieve this by casting the Pawn's controller to a player controller getting its enhanced input local player subsystem and adding the input mapping context to it so again there's this enhanced input local player subsystem this is a class that will already exist in the game as long as you have the enhanced input plugin added to your project and you add the input mapping context to it providing an integer priority value and this is because again we can have multiple contexts going on at the same time and the priority determines which one will be active at any given point it says the enhanced input local player's subsystem also supports querying or removing specific input mapping contexts or clearing all input mapping contexts so it's through this subsystem the enhanced input local player subsystem that we can add any given input mapping context to any player and we have to associate this with that player controller so let's see how this works now our project we created is a c plus template project so this is all done in C plus and we're going to find out how that works and how to do it in C plus plus later on but if this were a blueprint project and we wanted to add that input mapping context from blueprint we can do that but as the docs say we have to access the controller now we could right click in the event graph and type get player controller because this is a character class and not just a pawn class it has a get player controller node but we can also get the controller with Git controller and the docs are saying that you can call git controller and cast it to a player controller so we would say cast to player controller for example and that would get our player controller as well so either of these methods will get the player controller since the docs do it this way we'll just show this example by doing it this way calling git controller and casting it now begin play is a good place to do this by begin play the game has started and we should have a valid controller by now now according to the docs it's showing that from the player controller we can get the enhanced input local player subsystem so as player controller we can type in enhanced input local player subsystem and look at that we have a getter function so if we click that here we're now getting that enhanced input local player subsystem so this is how we access that subsystem in order to add a given input mapping context and associated with that given controller now once we have this subsystem we can then call functions on it to add or remove or query any input mapping contexts to add one we use ad mapping context so we would drag off of the subsystem and type add mapping context and select that function and we would call it here now to add any given mapping context we have to provide an input mapping context so if we had a variable we could plug that in or we can simply hard code it by selecting one from the drop down now I mentioned in this template project since it's a c plus project this IMC default input mapping contacts the one with all those different Keys already mapped in here this is already added to this character that's why we can currently play and run around that's done in the C plus plus class for this character but this is the way to do it in blueprints we can add say our new input mapping contacts that we just added and we can give it a priority and so on so getting the enhanced input local player subsystem is how we add input mapping contexts to a given player so it's a little bit different than the old way of doing it and the docs say each input mapping context that you add makes it possible to trigger the included input actions those input actions that were added to that input mapping context running the appropriate events in the Pawn's blueprint graph you can change the set of input mapping contacts at any time during gameplay so since we've now added this new input mapping context and Associated it with this particular controller we can now use any actions that we add such as our new input action so we've linked up the A and D keys to this action if we go back remember we have this event that was created once we made that new input action and once it's triggered we can do something such as say doing a print string node if we use a print string node and we take our action value and plug it in well our input action new input action is two dimensional it's a vector 2D and if we hook that into print string well we get an automatic conversion from Vector 2D to print string so now if we play test this we would expect to see a print string node let's see if we do let's press play a and right away I click in the viewport and move my mouse which tells me that that default input mapping context the one that was already here when we first created the project is still active and if I press A and D well we're getting that input action in that one the one that was already in our project not our new one so as we can see we can't have two running at the same time but here's where priority comes in I'm going to shift escape and come back up to our add mapping context and change the priority here to one now that this input mapping context is going to be added with a priority of one now we should see that we'll get our new input mapping context activated and we should see the print string now so let's press play and I'll click in the viewport and we see that I'm still able to control the character and run back and forth but if I press a now look at what's happening we get a print string showing us data we're getting that value let's go back here we're getting this action value and printing it to the screen it's in the form of a vector 2D now we'll learn how to use this data very shortly and we can use that one that's already active that IMC default as soon as we trigger an action from our new input mapping context the one we're adding here we get behavior from this input mapping context because it has a priority of one now if you're wondering what the priority is of this IMC default that's already working I mentioned that it was added in C plus for this class because this is a C plus plus template after all so if you're curious we can see what that looks like in begin play in C plus plus as well now we'll get into how to do this but as you can see we're doing basically the same thing that we did in Blueprint here we're getting the player controller and C plus plus there's just a controller variable that we can use and casting it to an a player controller we'll talk about this more later but we're also getting the subsystem this is how to do it in C plus plus we'll talk about that later as well and once we get that enhanced input local player subsystem we're calling add mapping context just like we're doing here so this is the same way to do it and take a look we're providing the context as an input in this case the character has a variable called default mapping context and again we'll get to that later but then we also have priority and it's zero so in C plus plus we're adding this mapping context that default one with a priority of zero and in Blueprint when we add one with the same priority zero they both have the same priority but the C plus plus context was added first and we'll get that one instead of our new one but as soon as we give it a value of one now this takes priority and any actions that we trigger from this will take effect essentially pushing out that default input mapping context and we can't move left or right so the docs say you can change the set of input mapping contacts at any time during gameplay they can be added removed we can clear all of them this makes things very Dynamic essentially behaving like a state machine would behave where you have different situations and only specific input mapping contexts will take place depending on their priority and whether or not they've been added to to the enhanced input local player subsystem now the docs go down to talk about the modifiers and the triggers so we're almost through with the documentation and ready to actually convert a project to use enhanced input but let's run through this real quick just so we can get more information about how these things work so we see that input modifiers are preprocessors that alter the raw input values that ue5 receives before sending them to input triggers so we've seen that already modifiers just change the raw input in some way the enhanced input plugin ships with a variety of input modifiers to perform tasks like changing the order of axes implementing dead zones converting axial input to World space and several others so we have several input modifiers already built in for us and we'll learn later why we would want to change the order of axes it says each input associated with an input action inside an input map in context goes through a user-defined series of input modifiers before proceeding to the input trigger we've talked about that and we have in parentheses or input triggers because you can have multiple triggers for an input input modifiers are applied in the order they are listed we've seen that and the output value from each modifier becomes the input value for the next we've seen that as well now it goes on to say that you can create your own input modifiers unreal has several already built in but you can create custom modifiers and we'll go ahead and just gloss over this part but come back to this if you get to a point where you need a custom modifier and this will show you how to create a new modifier class and how to modify the input essentially by overwriting a function in the modifier called modify raw and then returning it so essentially you will be able to take the current input value modify it in some way whether you want to perform a calculation in return earn some result and then that goes to the return node and because modifiers are applied one at a time in order whenever you add this modifier to Any Given input it will take the input from the previous modifier or the raw data if this is the first modifier and return it either to the trigger if there are no modifiers after it or to the modifier after it if there exists one so that's how you would go about creating a custom modifier now let's move on to this little section here which is really helpful on directional input so we know that we can create an input action choose its value type and we know how to add the input mapping context and add these input actions to the mapping context so that we can make input do something we've only printed a string so far but what about directional input how would we use that and how would we modify it we've already seen that IMC default here here in the default project maps all these keys wasd and the arrow keys to the move input action and the reason we can have only one input action but multiple keys that move us in different directions is because these all have modifiers that change the input based on the key we see that the W key uses Swizzle input axis values and the S key uses both Swizzle and negate so why is that and what are these modifiers doing to the input well the docs are about to tell us it says a good example of input modifier usage is two-dimensional directional input using a single input action that's what we're doing here with IA move it's a single input action and it's a two-dimensional one if we go to the actions folder and check out iamove we see that its value type is Axis 2D so I a move is an input action capable of handling two float values it essentially has a 2d Vector inside of it and an IMC default we're mapping different keys and a single key just returns a single value the docs say with a mouse or a gamepad's analog stick reading two-dimensional movement is a simple matter of creating an input action that supports at least two axes and adding the appropriate input to the input mapping context So it's talking about Mouse and Gamepad thumbsticks that would be our IA look right here remember IA look takes a mouse XY to the axis so in this case it makes sense to have a two-dimensional input because when we're moving the mouse up and down or left and right we'll have raw data values for the X and Y associated with that movement now let's expand modifiers here and we see that we have a negate hmm I wonder why we have a negate modifier well if we expand that notice that this negate is only affecting the y-axis now the negate modifier has X Y and Z but the Z doesn't matter here because I a look is two dimensional it only has X and Y and this negate modifier will take that y value and negate it let me show you what happens if we uncheck that and go back and press play I'm going to click in the viewport and move the mouse up and down when I move the mouse up I start looking down when I move the mouse down I start looking up but if I go back to IMC default to the negate for IA look and check that y again and go back and press play Then when I move the mouse up I look up and when I move the mouse down I look down so that's a preference some people like to move the mouse up and look up others like to move the mouse up and have the camera look down that's a configuration known as inverted but that's how our default input mapping context is modifying our Mouse input it's negating the y-axis for that and our IA move has its own modifiers negating some of the values and swizzling some of the values and again we haven't talked about what that means but we're about to now it goes on to say enhanced input supports input from one-dimensional sources such as a keyboard's directional arrows or the popular wasd key configuration so it's saying that pressing a key is one dimensional right you're either pressing it or you're not pressing it it says you can achieve this control scheme by applying the correct input modifiers specifically by using negate to make some keys register as negative and using Swizzle input axis values to make some keys register as y-axis instead of the default x-axis so that's what swizzling is doing by default if you have an input action that's two dimensional like move right move is value type axis 2D it's two-dimensional capable of taking in two float values an X and A Y but if we just link up a single key to IA move well a single key is only capable of providing one-dimensional input either it's on or off being pressed or not pressed so what happens when you link up a key to a two-dimensional input action is that the raw data from pressing the key or not pressing it goes directly into the x-axis of this two-dimensional input action in other words the y-axis in IA move and its 2D Vector that it has inside of it will always be zero but when we press the key the x-axis value will change it'll be a one if we're holding the key down and zero if we're not holding the key down in other words we can't really change the Y value of our I a moves 2D Vector so that's where swizzling comes in if we expand the Swizzle modifier we have some options y x z z y x x z y y z x and zxy in other words we're changing the order that that raw data will fill in the individual axes it's changing which axis is populated first setting this to y x z means that when we press the W key instead of the default Behavior which is to pass that raw data into the x-axis for IIM move we're now going to pass that data in to the y-axis for IA move in other words when we press the W key we want to move forward right so we're going to take I a move and pass in that data to its y-axis instead of the default x-axis and that way we know that the y-axis in the Ia move to the vector will be 1 if we're pressing W and 0 if we're not now the S key is for moving backwards right so we do want data corresponding to whether we're pressing the S key or not to go into the y-axis right we're going to use y for forward and backward movement in the input action and for that reason we have the Swizzle input axis values with Y X Z so again any input coming in when we're pressing or not pressing the S key will fill in the y-axis for IA move but if we want to move back words we're going to want that negated right so pressing s with the negate modifier means that I a moves y-axis will get a negative 1 when we're holding that key down and if we're not holding the key down well it'll just stay at zero so this is why we would want to Swizzle and put axis values if we want that raw data to affect our I a move or whatever input action It Is by filling in a different axis if it's a 2d or 3D axis value type now if we go down to the a key we see that it has one modifier and it's simply negate that's because as we mentioned a single key when linked up to a two-dimensional input action is going to affect the x-axis by default so if we press the a key without any modifiers the x-axis of IA move would become one but if we add the negate well now now it'll be negative one that allows us to move to the left and we don't need to Swizzle again because moving left and right we can just use the x-axis for that information and then we have the D key which has no modifiers that's because well pressing the D key without any modifiers is going to provide a value of 1 in the x-axis for IA move and we don't need to negate it because if we're going right we just want that value to be positive so D doesn't have any modifiers so with these modifiers we can link multiple keys to the same input action and the modifiers will change not only which axis in IA moves 2D Vector is affected by pressing that key but also whether or not the input value is negated or not and again negate can accommodate for three axes even if you're not using all three axes as is the case here so you can take a look at the arrow key Keys as well and you'll see that those same directions have the same modifiers as the wasd keys that correspond to those directions so check that out if you're interested so the docs have a nice little table that show you if you want wasd movement and you want it linked up to a single move action what modifiers you would want to add to those keys so for w we saw that we wanted the desired input interpretation to be in the positive y-axis in our input action which is a 2d Vector so we use Swizzle to make sure that that input goes into the Y instead of the default x-axis and for the a key which is going left we want that in the x-axis and we want it to be negative so we simply add negate and the S key is going down which means we want it both in the y-axis so we Swizzle and we also want it negated so we negate as well so that's why it has two modifiers and then the D is simply going right that's the positive x-axis well inputs go into the x axis of 2D value type input actions already so we don't need to Swizzle and we don't want to negate it either we want it going in the positive X so it has no modifiers and here's a nice screenshot of all the things we just looked at in the engine and it also explains that since each key reports a positive one-dimensional value this value will always occupy the x-axis so we talked about that and we'll have a value of either 0 or 1 on any given tick that's based on whether you're pressing or not pressing the key it says that by negating the value for left and down inputs and switching the axis order right or swiveling so that the inputs x-axis value move to the y-axis for up and down movements you can use input modifiers to interpret a set of one-dimensional inputs as a single two-dimensional input value in other words our input action can have a 2d Vector that can have either 0 for X and Y when we're not pressing any keys or it can have a y value of positive one when we're pressing the W key it can have a y value of negative one when we're pressing the S key thanks to the modifiers we've added and an x value of positive one when we're pressing D we didn't need any modifiers for that and when we press a the x value will be negative one and that allows us to use this 2D Vector to interpret how we should move the character and we'll get into how to do that a little bit later so we now know how we can use modifiers to handle directional movement next comes triggers and we'll kind of briefly gloss over this in this video it says input triggers can determine whether or not a user input after passing through an optional list of input modifiers should activate the corresponding input action within its input mapping context so here we're having some criteria added most input triggers analyze the input itself checking for minimum actuation values and validating patterns like short Taps prolonged holds or the typical press or release events this is really nice because if we want to require our player to hold a button for a short period of time before the action is activated well traditionally we'd have to use things like timers or delays but the enhanced input system just has this Behavior built in in the form of triggers and it's automatically taking into account how much time is passing for any given input so this makes it really easy and you don't have to go and make those timers or delays now it says there are three types of input triggers explicit types cause the input to succeed if the input trigger succeeds so remember a trigger can be an input to another trigger implicit types cause the input to succeed only if the input trigger and all other implicit type input triggers succeed so you can have multiple implicit types and all of them have to succeed in order for an implicit trigger to succeed think about multiple keys at the same time blocker types cause the input to fail if the input trigger succeeds so it's kind of like a negation if an input trigger succeeds a blocker input trigger is going to fail so this gives us ways to make more complex input behavior and this is a logical example of how these will work and we won't be getting into that too deep in this lecture now after processing user input input triggers can return one of three states none indicates that the input triggers conditions have not been met so the trigger fails pretty simple ongoing indicates that the input triggers conditions are part officially met and the input trigger is processing but is not yet succeeding and triggered indicates that all the input triggers conditions have been met and the input trigger succeeds so that's what these states mean you can create your own input trigger by extending the base input trigger class or input trigger timed base and then we see that this is how you can create your own custom input trigger and how to use it and we won't be making custom input triggers in this video but the docs here are showing you that you can so that's the documentation for the enhanced input system and so now we see that we have several different pieces and things we need to do in order for enhanced input to work for our project and we also have the C plus code in our template project if you created it if not that's fine showing us how we can add a mapping context by accessing the enhanced input local player subsystem here we'll be doing this when we convert a c plus project to use enhanced input we also see that setup player input component in this character is setting up enhanced input rather than doing it the old way we're going to see how to do that shortly as well and we also see that we have functions being bound to different input actions and those take a certain data type that allows us to get those axis values in that given input action we'll see how to get that working as well and the best way to show you how to set all this up I think is in the form of converting a project from the old input system to the new input system so we're going to get started doing that right now
Info
Channel: Druid Mechanics
Views: 74,159
Rating: undefined out of 5
Keywords:
Id: bIo97TLsXkY
Channel Id: undefined
Length: 66min 29sec (3989 seconds)
Published: Wed Nov 23 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.