UE5 C++ Enhanced Input - 2 - Bind C++ Functions to Input Actions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] welcome back in the last video we went over the documentation for the enhanced input system in Unreal Engine 5 and learned a little bit about how it works and how we can add it to our project now we're going to talk about how we can get a project converted from the old input system to the new enhanced input system so we'll start with a really basic example in this project I have a pawn it's called BP bird I have it here in my content blueprints pawns folder and if I open it I see that it's just a basic Palm it has a capsule component and it has a skeletal mesh component and it's called bird mesh and I don't even have an animation blueprint it's just set to use a single flying animation now this bird is using the old input system currently I just have a single form of input and that's to press a key and log a message to the output log so if I open my output log and dock it in the layout I can press play and possess this Pawn because as you can see in its details panel it has Auto possess player set to player zero so we don't even have a game mode set for this level we're just Auto possessing this bird pawn and if I press play well we don't even have a camera and spring arm so we can't see that we're possessing the bird Pawn but I'm logging a message to the output log and if I click in the viewport and press the W key I see that I'm logging a value of one and if I release it I'm logging a value of zero and if I shift F1 to get my mouse cursor back and detach well then I can move and see that I am indeed possessing this bird Pawn we just don't have any offset because we haven't added a camera or spring on component this is super basic and in fact it doesn't even move forward where only pressing the W to print a message to the output log now in the old way of handling input we would go to edit and project settings and select input and we would add axis and action mappings as you can see I have just a single axis mapping called move forward and if I expand it I have the W key linked up to it this is the old way and I would set it up in C plus plus as follows in my bird header file I would have some sort of callback function that takes a float in this case I have a function called move forward and I would bind it to that move forward axis mapping so in my C plus file I would go into setup player input component don't mind the red squiggles that's just intellisense struggling to keep up with me but as you can see setup player input component has this player input component which has a bind axis function where we would select the axis mapping name and to remind you here in the engine and project settings its name is move forward so we've bound that we have a user object that's this because this bird is the object that we wish to call a function on linked to this axis mapping and we have a callback function we're passing in the address of a bird move forward and as you can see my move forward function is really simple all we're doing is using UE log to log the value of this axis mapping to the screen so this is all the old way of using simple player input to do something in the game in this case we're logging value using a UE log so how would we get this converted so that we're using the new enhanced input system rather than the old way now if you haven't watched the first video where I go over the documentation I highly recommend you watch that video as it explains all of the enhanced input system Concepts and how they work and that'll give you a good grasp on what we're about to do in this video and in that video we learned from the documentation that to use enhanced input we have to add the enhanced input plugin now if I were to convert this project into Unreal Engine 5.1 then that will do the trick for us this is currently a 5.0.3 project so if I convert this to 5.1 then I don't have to enable that plugin it'll be enabled by default but if I were to stay in 5.03 I would go to edit and plugins and search for enhanced input and check that and restart the editor and then the enhanced input features would be added to this project so converting to 5.1 is an option or just adding this to an older version that has enhanced input in the plugins menu is another way now I'm going to convert this project to 5.1 so I'm going to close all I'm not even going to save what I did and I'm going to close the visual studio solution as well and I'm going to take my project it's called slash in my case right click on the U project and select switch Unreal Engine version and I can select 5.1 and click ok now as soon as I do that I'm just going to open my slash dot solution in visual studio and make sure we can successfully compile so we'll go ahead and Ctrl shift B to compile our solution I'll pull up our output log here and I see that I have a successful compile so what I can do is I can now launch the editor so I'm going to double click my DOT U project now as soon as it opens I get some messages that say upgrading default player input class to enhanced player input and another message that disappeared before we could read it but I'll show you what those messages were showing us if we go to edit and project settings and click on input well some things have changed the first thing we see is that access and action mappings are now deprecated please use enhanced input actions and input mapping contexts instead now we talked about what these are in the previous video when we went through the documentation now in 5.1 these are deprecated it doesn't mean they don't work anymore the old way still does work but they're deprecated which means in a future engine version eventually we'll have to use enhanced input now if we scroll all the way down the default classes have changed they used to be player input and input component but now they're the new enhanced versions of these this happened when we converted to 5.1 because now 5.1 has enhanced input enabled by default now the reason the old way still works is because these are child classes of the old classes so they inherit all the capabilities including being able to use these action in axis mappings but of course these are now deprecated and we're going to learn how to do things the new way so now we have access to enhanced input and again if we go to edit and plugins we can search for enhanced input and we see that enhanced input is checked this plugin is added for us as soon as we upgraded to 5.1 so we don't have to add it and restart the editor or anything like that the enhanced input plugin is already here now so what we want to do is convert that basic move forward input system that we created so that we're using enhanced input now I don't see my bird Pawn here that's because this is the minimal default level it's going to reset back because this is a level from engine content we could save it as a new level and put it in a new location if we want to save the changes we make but really I can just drag in my bird pawn and if I search for auto possess I see that it's set to player zero so if I press play I'm possessing it and I'm getting that message logged to the output log so we see that the old way of input still works I can press W and I'm logging one or zero based on whether or not I'm pressing that W key so our goal here is to now use enhanced input instead to get the same behavior and I know this is basic we're going to get into more complicated input later on but for now let's just get this basic mechanic working this logging to the output log so I'm going to close the output log and as we learned in the last video instead of access and action mappings which is the old way as we can see in Project settings and go to input we see we have a move forward axis mapping instead of this we're now going to use an input action that's the enhanced input actions that this yellow message is talking about we need to make an input action for our bird so what we can do in our pawns folder where I have BP bird is I can right click and I can add a new folder and I'm going to call this input we'll put our enhanced input classes in here and here is where we're going to create an input action and we can do so by right-clicking going to input and selecting input action and I'm going to prefix it with IA short for input action so it's clear that this asset is an input action and I'm going to call this simply move so I can double click on IA move and here's my input action and we see that its value type is digital Bool by default which means that it can handle either true or false type of information based on inputs now we're going to keep this nice and simple and leave it at bold just for this example before we move on to handle actual movement we just want to link this up with our character and make sure we can use this I a move and find out how now creating the input action is step one in order to link up this action with a particular player and we say local player because in a multiplayer game there could be multiple players in a game the local player is the one on this machine controlling this Pawn through that player controller so we need to link up this IA move to this local player now in order to link up our IA move input action to the player we have to make an input mapping context and to do that we can simply right click in the content browser go to input and select input mapping context now I'm going to prefix this one with IMC short for input mapping context and I'm going to call this bird context this input mapping context can have input actions added to it and we're going to do that now so we're going to open IMC bird context and up here we have mappings so now that we can add input actions to different input mapping contexts we now no longer have to go to the project settings level and set up inputs for the whole project and then go into code and make checks based on gameplay logic to determine what those key presses do instead we can use separate mapping contexts and activate or deactivate them at runtime during the game to change behaviors linked up to certain Keys now in order to use IA move we have to add it to our input mapping context we do that by clicking the plus next to mappings and if we expand the drop down we see that it says None here but if we click open we can search for our input action called move it's IA move if we select that now we have the option to link up different keys to this input action now we can do so by selecting keys from the drop down but there are a lot of options here we could go to keyboard choose the tab key or the enter key or the number keys but a handy shortcut is to Simply click this icon next to the drop down and pressing a key on the keyboard such as W and now we've Linked UP W to IA move in this input mapping context now input actions can have triggers and modifiers associated with them and we discussed those in the last video for now we're going to leave this input action without any triggers or modifiers as we just want to get this working and we saw from the documentation that we can link up this input mapping context by associating it with a specific player controller we can do that in our Birds blueprint I'm going to go back into my pawns folder and open BP bird here's my bird Pawn blueprint and if I go to the event graph I can use begin play to get our controller so I'm going to get controller and let's see if we can cast this to a player controller as the docs say that to add our mapping context we need to get a player controller I'm going to cast two player controller drag off of begin play and if the cast succeeds I'm going to print a string that says cast succeeded and if I press play there I see cast succeeded so we can get our controller from a pawn and cast it to a player controller because this get controller function returns a controller in C plus it would return a controller pointer but even though its data type is controller it stores an actual player controller and we have to cast to get that player controller so now that we know we have a successful cast the docs say that in order to link up our input mapping context to this controller we have to get something called the enhanced input local player subsystem we get that directly from the controller so from our cast we can search for enhanced input and call this function get enhanced input local player subsystem now this subsystem has the functions for adding input mapping contexts removing them and so on and to add one we call the function add mapping context so we'll call that and hook this up and for this function we'll simply select the mapping context ours is called IMC bird context and we can give it a priority if we give it a priority then we can have multiple mapping contexts added at the same time but the priority will determine which input actions will work and we won't have any collisions between input mapping context and we saw that in the previous video as well so now that we've linked up our mapping context we need to decide what happens if this input action called IA move gets triggered now as soon as we made I a move an event was generated for us and we can access it in BP bird its name is the same as the action mapping so we can search for IA move and here under enhanced action events we have an event called IA move we want the one with the arrow not this one down here that's an actual value of an enhanced input object we want the event that was auto-generated for us as soon as we created that input action and as soon as this action is triggered well what we can do is we can print now we can use print string if we like and notice that our event has an action value and it's a Boolean that's because when we created the input action we let its value type be Bool but as you can see input actions can handle float values by choosing axis 1D they can have two dimensional inputs like a two-dimensional Vector so it would have an X and a y axis and it can have a three-dimensional Vector now we left it at Bool which means when our input action is triggered if we simply drag action value into the print string we'll get a conversion from Bool to string and we should be able to see the value that our input action has once that action is triggered and that value will be true or false based on whether or not our W key is pressed that's because we linked up the W key to IA move so if we go ahead and Press Play click in the viewport and press the W key we're printing true to the screen and if we release we don't see false because we're not triggering that input action it isn't being triggered until we actually press the key and if we hold the key down we see that it's being triggered every single frame and we get that print string now this is a little bit redundant because if I remove that print string if I just unhook that node and I go back and press play we can actually see the value of our input action by using a console debug command we saw this in the last video as well if I hit the tilde key and type show debug enhanced input well now we see some debug on the left and we can see our context is set to IMC bird context I'm actually going to shift F1 to get my mouse cursor so I can point this out we see that our player is player controller zero that's the only player controller in the game right now here's our IMC bird context currently added to the enhanced input local player subsystem and here's the only action that this context has it's called IA move it has a value of none and a duration of zero seconds and here's the key assigned to it now if I click back in the viewport and press W all of a sudden it says triggered and I'm holding W and it's keeping track of how many seconds have passed since IA move has been active and W says true because that's the value that this input action has and if I release now it says 0 seconds with false so this is a really useful way to debug and see if our actions are working so we don't need to resort to print strings or UE logs anymore now let's say we want our own functionality in C plus plus instead so this is the blueprint way to do everything let's do it in C plus plus and we can use a UE log so rather than using this auto-generated event let's say we want our own C plus plus function to be linked up to this iamove input action so let's do that I'm going to go back into my C plus project here's my bird.cpp and my bird.h now the old way of doing things was to create a void function that takes a float value whenever we wanted to bind a function to an axis mapping because axis mappings receive raw input and provide us with a float that'll be zero if the button is pressed and one if the button is released and so on right let's do this the new way now to set up enhanced input from C plus plus we have a few extra steps and before we set that up let's just take a look again at what we did in blueprints we got our controller right we cast it to a player controller and from that player controller we got the enhanced input local player subsystem we're going to learn how to do that in C plus and we called add mapping context and when we added a map in context we needed to provide an input mapping context now the blueprint node lets us hard code one we can select any input mapping context that exists in our project from C plus plus though this function also requires a mapping context so where are we going to get that value in C plus plus well we could create a variable make it edit anywhere for example and set it in the details panel of our bird let's do that so here in bird.h we're going to add a variable for our input mapping context and the C plus plus type for an input mapping context is U input mapping context so this will be a pointer and we're going to call it bird mapping context now we see that it's a undefined type so that means a couple things for one we're going to want to forward declare it here in the header file we've already learned why we should forward declare rather than include headers in other header files if we can help that so up at the top I'm going to forward declare U input mapping context so now we can use this type even though its header isn't included it's now just an incomplete type once we actually use it in the CPP file we'll have to include the header where this is defined now I want to be able to set this input mapping context from the bird blueprint so I'm going to make it a U property and I have this in a protected section so I can make it edit anywhere if I'd like to set it from the blueprint in the details panel and if I did want to access this from the event graph I could use blueprint read write or blueprint read only I'm going to make it blueprint read only since it's not a private variable but a protected one we can use that specifier and I'm also going to place this in a category we'll place it in the category input so after adding this variable I have to remember that this will just be an empty pointer until I give it some value and we'll do that in the blueprint details panel and we can do that thanks to edit anywhere here so we can add this bird mapping context the same way we did in blueprint by first getting our controller casting to a player controller and getting the enhanced input local player subsystem and calling add map mapping context passing in the mapping context and the priority let's find out how we can do that we'll do it in begin playing just like we did in the blueprint so in bird.cpp we can go to begin play and the first thing we can do is get our player controller so we're going to say a player controller we'll make a local pointer of this type and we'll call this player controller and we'll set it equal to the result of a cast now we're casting to a player controller so we're going to take that type and place it in the angle brackets and what we're casting is what we get from our controller now pawns have a controller variable which we can access directly so even though in blueprints we used git controller here in C plus plus we have direct access to that controller variable now it is possible in the future that they may move this into a private section in the Base Class where it's declared in which case we would have to use git controller which is an inherited function that simply returns that controller so we can be preemptive and use git controller just in case they ever make controller private and Unreal Engine does tend to do that with some inherited variables it'll move them into private sections and then in child classes like our bird class they would be inaccessible and we'd have to replace them with the getter anyway so anyway now we have the player controller right and as long as that cast succeeds we'll have a valid pointer that we can use to access that subsystem and we can always check player controller with an if statement to make sure that we don't try to access it if it's null now if you watched the previous video you might have noticed that the third person template project does this a little differently let's look at how the third person template project does it here's the third person template project I just created a project called enhanced input project and it made this character for me and in begin play in the third person template it does this this way it has an if check and it creates a local variable inside of the if check setting it equal to the result of the cast it's casting controller to a player controller now this might look a little bit confusing but really it's only doing one thing it's creating a local variable just like we did in our bird class only it's declaring it in the if statement parentheses and that just means that this local variable only exists in the scope of the if statement if I were to say go outside the if statement and type player controller and try to call a function on it well it doesn't exist anymore it goes out of scope as soon as this if statement body has been ended as soon as we exit the if state body player controller is no longer valid that pointer gets destroyed not what it's pointing to but the pointer itself and we can't access it now the way we did it in bird Pawn we created the player controller local variable outside the if statement therefore it's still in scope until we reach the end of begin play we could type player controller down here and we can access functions on it because we created it outside the scope of the if statement if we declared this variable inside the if statement like the third person template project does then it will only exist in the if statement and go out of scope here as soon as we exit the If instead of here when we exit begin play as is done here so if we wanted to make this a little bit more like the template project we could move this line here into the if statement like this and then player controller will only exist here in the if statement we can access functions on it and use it but we cannot access it outside the if statement as by that point it's gone out of scope so I just thought I would explain that now in our blueprint after casting to player controller we need the enhanced input local player subsystem we can even see how it's done in the third person template project again they're using this trick where they're declaring a new local variable this pointer here inside this second if statement so that means this subsystem local variable is only in scope here inside this second if statement if we tried to use it outside that second if statement say to try to call functions on it like this well it's out of scope by the time we exit that inner if statement now if you're wondering why they did this it's a very very minute form of optimization you see if this pointer is cleaned up or deleted as soon as it's no longer need needed well then we can do other things and begin play and have one pointers worth of memory freed up for the rest of the function but nonetheless all that's happening here is a new local variable of this pointer type is being created and it's you enhanced input local player subsystem they're calling that local variable subsystem and they're initializing that pointer by getting the enhanced input local player subsystem they do that by accessing the u local player static function get subsystem this is a template function which requires you to specify the type of subsystem subsystems are a type of class that can be used in Unreal Engine enhanced input uses this particular class now it's created by default when you start playing the game so if we call git subsystem specifying this type with the enhanced input plug-in active we should get a valid value from this and get subsystem requires a you local player so we typically talk about the player controller or the controller when we're talking about a player but there's also a local player class associated with the player playing the game as well and we can always access the you local player associated with the player controller with get local player so what we're doing is calling git subsystem passing in the local player this is a little different than what we did here when we called ad mapping context as blueprint already takes care of figuring out the local player whereas here in C plus we have to specify the local player we get that from the player controller so we're going to do this same thing in our project so first we need our U enhanced input local player subsystem so we'll say you enhanced input local player subsystem that's a pointer we'll call this subsystem now right away we get red squiggles because well we're using this class and we need to include the header file where this is defined otherwise the compiler doesn't know what this type is so I'm just going to copy this type and we'll go straight to the engine docs in fact I can just type into Google ue5 with this type and the first thing that pops up are the Unreal Engine docs for this type and we can see that not only is this in the enhanced input subsystems header file so we can copy that but it's also in the module enhanced input now I know that we have this plugin active on our project but to access these header files in C plus this module needs to be added to our build file so we'll take care of those things first we're going to comment out this line and if we go up to the top and we try to include enhanced input sub systems.h we get an error here we can't open that file that's because again this is in that module called enhanced input we need to add that module to our project's build file so again let's comment that out we don't want any errors there and in our project we have our build.cs now my project is called slash so the build file is slash Dot build.cs and here are all the modules being used in it now you don't have to understand C sharp that's what language this CS file is in all you have to know is that when you build your project these modules are added to the project by default because we're specifying them here that's why we can access engine classes and header files that exist in these modules because the build file determines which modules to include you see Unreal Engine is a very large engine and it's made up of a whole bunch of different modules you can expand the folders here under engine to see a whole bunch of modules here's the plugins folder all the plugins are modules and the way to include those header files is to include the modules here in the build file we want to include enhanced inputs so we're going to add a comma to this list and type in quotes enhanced input to include that module now let's go ahead and compile after adding enhanced input to the public dependency module names and to compile from Visual Studio because I have live coding enabled I'm going to close the editor first and then compile my visual studio project we have a successful compile and with enhanced input added we should be able to go back to bird.cpp and uncomment this include now if you still see squiggles we may need to generate Visual Studio project files that's because there are automatically generated temporary files that are created when we compile and sometimes they contain things that we need to delete and clear out and make it fresh so what I'm going to do is comment that back save all and close my solution and to generate Visual Studio project files we have to delete our saved that's one of those temporary files our intermediate again a temporary these will be generated back for us and the binaries we'll delete those and we'll right click on you project and select generate Visual Studio project files we already see that the saved and intermediate come back right away and now that that's done we'll double click the you project to open it and it's says the following modules are missing or built with a different engine version would you like to rebuild them now so if you get that click yes and we can open slash dot solution and with our solution back open we can go back to bird and uncomment this include and we see that we have no more errors because our module has been rebuilt we can even go back down and uncomment this line and of course there's our project loading up so that's going to come up too and we're back open to minimal default here which again is a map in engine content so our bird Pawn is gone but that's okay we'll put it back in but for now we can uncomment you enhanced input local player subsystem and see that this type is recognized we successfully included enhanced input subsystems.h and now we can create a pointer of this type so I know that was a lot of work just to create a pointer of this type but now we have access to the enhanced input module on the C plus side and now we can use those classes so looking back at the third person template project we see that we initialize subsystem by getting the you local player and calling git subsystem specifying this type in its template and passing in the local player so we'll do that here we'll set subsystem equal to u local player and we'll call the static function get subsystem and in its angle brackets we'll pass in U enhanced input local player subsystem and in the parentheses we need to pass in the local player which we can get from player controller and the function is get local player so we'll add that semicolon there and now we have the subsystem and we know that we should check pointers before we use them so we can say if subsystem and again if we want to use this trick where we declare this local variable in the if statement therefore it will only exist in the scope of this if statement well we could do what the third person template project does and declare and initialize this variable all inside of the if statement here like this now you might be wondering how does the if statement succeed or fail well this comes down to C plus plus syntax and the mechanics of how the language Works where creating and initializing a new pointer so what does this whole expression return for the if statement well when you declare a new variable such as a pointer then the return value of the entire expression is the pointer itself so this is the same as saying if subsystem and if all this stuff on the right side of the assignment operator returns a valid pointer then if subsystem will succeed and will have a valid pointer but we get the same result if we were to declare this variable outside the if statement and simply check the pointer directly the only difference is this is now only in the scope of the if statement and its body here so we did all that work just to get the enhanced input local player subsystem and we can remind ourselves of what we did in blueprints by opening BP bird again and here we got that subsystem and from that subsystem we called add mapping context we can do this in C plus plus so we'll do that by taking subsystem and calling add mapping con context here it is and if we open the parentheses we see that it takes a u input mapping context it also takes a priority and then we have some modify context options these are optional there's a default value as we can see so we don't have to pass that in all we really need is the input mapping context and a priority which is an integer now we added a variable called bird mapping context this is a u input mapping context we can pass that in for the input mapping context so we'll pass it in here and for the priority we'll just pass in zero just like we did here in the blueprint here we're hard coding it and C plus plus we're passing in a variable and there's the priority and there's those options which again are optional now we made this a variable right if we go to BP bird self and search for the input category here's our variable bird mapping context it's set To None we never actually initialize that pointer but we can set it because it's edit anywhere so we can select IMC bird context so now we'll know that once the game starts we've set this to a valid value and this pointer bird mapping context has a valid value because we set it in the blueprint so all that work just to get the same effect as this logic here in event begin play so now that we're already doing this in C plus plus we don't need to do it in Blueprint anymore now the last thing to do here is to have some sort of function that can be linked to IA move again things are easier in blueprints because this event was created and it's already linked to that input action but we want a C plus plus function linked to IA move instead so let's do that now to do that we're going to add a variable for that input action called IA move now and C plus plus an input action has the Type U input action so U input action it's going to be a pointer and we can call this move action now this is just an empty pointer right just like bird mapping context so we want to set our move action to the input action asset that we created in the editor so we're going to make this edit anywhere as well we'll go ahead and make it blueprint read only and put it in category input also so now that we have a u input action variable we can use it to link up a c plus function which means we'll need a function to call again in Blueprint we didn't have to make a function because this event was created for us as soon as we made that input action but in C plus plus we need to make our own function so we can make a function it's going to be void and we can call it move now in the old way we used a float call called value the new way is a little bit different the new way takes a different type of input and we can see that by again looking at the third person template project it has a move function as well it's right here and look at its input type f input action value this is a struct that contains information about an input action this makes it much more versatile because input actions can hold booleans like our IA move that we created but it can also hold two dimensional vectors three-dimensional vectors or even one-dimensional vectors and so this way if we make a callback to link up to an input action we only have one function signature that we need to worry about one that takes this F input action value and it's passed in by const reference so we're going to use that as the input parameter for our move function so it'll be a const reference of type f input action value there's the reference and it's called value and we can make a function definition and now we have a function so in Blueprint we had an event but in C plus plus we're making a function now at this point let's go ahead and compile our solution which means I'm going to close out the editor clicking save selected and here in Visual Studio I'm going to go ahead and hit Ctrl shift B to compile and we see that we have a failure to compile now why is that well we have a couple of issues here for one missing type specifier int assumed means it doesn't really know what an input action is well we can easily forward declare U input action just like we did you input mapping context so we'll forward declare you input action here so that takes care of that type but we also have some issues with line 35 which is now 36 because we added that full red declaration up there and that's referring to the compiler not knowing about this type now this is a struct and we can't forward declare something that's not a pointer this is not a pointer but structs an Unreal Engine are typically small and it's generally safe to include their headers here in the header file in fact look at the template project for the third person template scroll up to the top of the characters.h file and there's the input action value header so we can see that even in this example epic was okay with including this header here in the character's H file since we now know that this is the include for that struct type we can copy it and we can add that include here to the top of bird.h but we have to be careful if we're including a header here it has to be above the generated H remember that has to be the last include in the header file so now let's go ahead and try to compile and now we see that we compile successfully so for these pointers we can forward declare but for this struct F input action value we need to include the header file because we're not forward declaring we can't forward declare if it's not a pointer so now that we have this move function we want to make sure that it's working right and in the old way we just had a value of float type and we were able to print that to the screen but now we have an F input action value so how do we get the input actions value from this struct well we can take that value and being a struct not a pointer we can use the dot operator and F input action value has a get function and this is a template function because well this system was made versatile to handle multiple types of inputs we can use Bool here if our input action used a 2d Vector we could use f Vector 2D or if it used a three-dimensional Vector we could use f Vector but in this case we know that our IA move has the Bull type so we're going to use Bool so by calling value.get specifying the type Bool we can get this input actions current value and we can store this in a local variable we can make a cost Bool called called current value for example and we can initialize it and we can print to UE log if this value is true let's just say if current value then we'll use a UE log we'll say UE log log temp warning and for the text we'll say I a move triggered so now we have a function that we wish to bind to our I a move input action the only thing that's left is to actually do that binding and we do that and set up player input component just like in the old way of doing things and the old way of doing things we took the player input component which gets passed into this when this function is called it's called automatically and it's called once but now things are different now our class project in the project settings is using a new enhanced input component which is based on this type so we know that this player input component input here points to an object of the new enhanced input component type but it's in the form of a u input component so that means we have to cast it to the new enhanced input component type and again taking a look at the third person template project if we go to set up player input component we see that that's exactly what we're doing we're casting the player input component to the enhanced input component now it's using cast checked which is like cast but it also asserts that the cast will succeed if the cast fails cast checked can actually crash the game for us now you might think that intentionally crashing the game is a bad idea but in this case it's good because if our cast to this enhanced input component type fails we don't want to continue the game we want the game to crash so we can come back and see what's wrong and of course they use that trick again where they declare this pointer in the if statement and initialize it by casting player input component to you enhanced input component now that means we need the header for this and I can just scroll up because here in the third person template example we have enhanced input component here so I can copy that include and up here I can paste in enhanced input component so we need to cast our player input component to an enhanced input component and since we're no longer using the old system I can either remove this line or just comment it out and move it out of the way so let's cast player input component to an enhanced input component so we can say if and we can declare the variable here U enhanced input component foreign we can call it enhanced input component and we can initialize it and we'll use a cast but I'd like to use cast checked I think that's a good idea we'll crash the game if the cast fails now we're casting to you enhanced input component and the value we're casting is simply the player input component we already have access to in this function as an input parameter so we'll pass that in and now our if statement has this local variable inside of it now in the old way we would call bind axis on the player input component but the enhanced input component has its own function that we can use to link up our move function to our move action remember we created a variable move action here it's a u input action right so here's how it's done we take our enhanced input component pointer and we call bind action on it in the old way we had access and action mappings and in the new way there's only bind action now the first input to bind action has to be a u input action we have that it's called move action and we still need to remember to set this in the blueprint so we'll pass in move action and we're still getting that red squiggle so there's something that we need here and if we look at the third person template project we see that it includes component slash input component.h if I go ahead and include that here at the top we'll put it right there then we can now see the next input parameters and the next one we see is e-trigger event now this is an enum we can tell because it's prefixed by e and if we Type e trigger event with the double colons we have all the options these are all states that our action can be in it can be canceled completed none ongoing started or triggered now we want our move function to be called when this action is triggered so we're going to choose triggered and the next input is just a u object called object much like we did in bind axis we can use this as this is the object we're concerned with calling a function on and next we see that we can pass in a function name we can also pass in the address of a function that's what these T method pointers are they're wrappers for function addresses so we can use the address of operator and we can use our new move function a bird move we need to use the class name and the scope resolution operator that fully qualifies that function name so this function is going to bind our move callback to our move action so this wasn't something we needed to do in Blueprint because that auto-generated event IA move and blueprints is just automatically linked but if we want our own custom C plus plus function linked up to our input action we have to do it this way and set up player input component so that's the last piece of the puzzle we should be able to compile and test this out so I have my editor closed I'm going to go ahead and compile and we have a successful compile so now I can open up my project and I'll go ahead and open my blueprint and we see that nothing's linked up here so none of this matters anymore I'm going to go ahead and remove all those notes and again we have to remember to select BP bird self and we have our bird mapping context we already set that and we have our move action this is an input action and we can choose our I a move that way that pointer in C plus plus will be valid and it needs to be valid because we're calling bind action specifying that move action variable and this line will link up our move function to it so if everything works we should see our UE log printed to the screen so we can test this by once again dragging in a bird Palm we know that auto possess player is set to player zero so we should automatically possess it and if we show the output log I'm going to dock it in the layout here and press play then I can click in the viewport and press W and there's our message so now we're using enhanced input and we're calling our move function in C plus thanks to binding it to our input action and that only works because we added our bird input mapping context through the enhanced input local player subsystem I know it's kind of complicated but this really allows us to expand and make our game better now again I could use the console command show debug enhanced input and as soon as I do that now when I press W I can see that action is triggered I can see how many seconds it's been triggered and we see a true or false value based on whether or not we're triggering that input action so this is a lot to set up but now that we've set it up we can easily expand this we can add more input actions we can add modifiers or triggers to them and our input mapping contacts can take any number of input actions and all we need to do is go through the steps to link up our c plus functions to those actions and in the next video we're going to add some more complex Behavior to a pawn so we'll learn more complex things that we can do with our input actions in our input mapping context so great job thank you for watching and I'll see you in the next video
Info
Channel: Druid Mechanics
Views: 42,957
Rating: undefined out of 5
Keywords:
Id: fW1pXOAIviw
Channel Id: undefined
Length: 53min 16sec (3196 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.