Converting Blueprints To C++ Code! | Unreal Engine 4 & Unreal Engine 5 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what's up guys Sean the Bro here and in today's episode we are going to be going over converting blueprints to code a few episodes part of this one we made an episode called converting code to blueprint so in case you had seen code in either my tutorials or somewhere else and you decided that you wanted to convert that logic to blueprints for your project I think that's a very valuable skill and I think it's even more valuable to be able to convert blueprints into code because sometimes you'll see a blueprint tutorial or you'll have logic that you have in your blueprint that you want to advance and make more efficient that case we want to convert that to code to C++ and so that's what we're going to be doing today now I have a project on the screen this is my platform fighter and Super Smash Brothers tutorial series that I have here it doesn't matter if you watched that series or if you have any of the content from here I'm just using it as a base because I already have some logic in the blueprints and I'd like to convert some of that to the code so it ends up working out nicely for us my goal is to use this episode as a general example for anyone who wants to convert blueprints to code and it doesn't matter if you have any experience or not I'm going to try and talk about what you should do when you see blueprints that you want to write in your code since the previous episode did so well on that topic converting the code to the blueprints I'm going to do more of these in the future but we're just going to go over the basics today we're going to take a tick function and convert that from the blueprint and put it in the code file that way we can do all the same logic in there so to get started this is my platform fighter tutorial and in here I have a base item BP so I have a code class already called base item and it has a lot of stuff in here don't get me wrong a lot of it was already done in the code however the base item BP is what will have the mesh and what will actually be spawning in the world it has a tick function that determines the rotation of the weapon now the function we're looking at isn't super important this is the function I'm going to use to convert my main goal in converting this event tick is to show you guys my thought process because this is not one I've done before in the code so we're going to be going through it live converting it and any problems that come up will solve anything we need to include will include and that's how it's going to go it's going to be a very free form episode this event tick is the actual event tick for this base item BP I do technically already have the tick in the code here I've already overridden it but I don't do anything inside of it you can see it down here so I'm going to show you how to get the tick function in your code to begin with because any events that you have in the blueprint can be functions in your code now of course you can make a custom event just like making a a function in code and in this case you would just make a function with the same name however there are certain ones that we have to override any events like begin play or on component hit or things that we will need to override in the code to actually be able to access them so since we are focusing on event tick today in the blueprint you just type event tick and you can grab it here it's super simple but it's not as simple in the C++ going into the code now if I go go to the file where I want to include the tick my base item. I can scroll down and this is how I can override and actually grab the tick function in this file we use the keywords virtual and override when talking about the tick function or really any function and as long as the name and the parameter list match exactly we can override this function and put it in this file the reason we are overwriting it instead of just using a new function is because tick is a function that exists in actors this base item is a child of the actor class the actor already has a tick we want to override the logic from the actor's tick and instead make our own function of tick we can still do everything that the actor's tick does inside this function but then we can add our own logic as well which is where it comes in handy so this will work for anything that you want to override just to show you an example you could do virtual void begin play override and this will also work it's giving me errors right now because I'm currently running Visual Studio but if I were to close it this would work and that's because begin play is a function on the actor so I am able to overwrite it with these keywords and the name of the function begin play doesn't have any argument so I didn't pass any in and since that all matches I can now override begin play don't need that today I'm going to keep my tick but I just wanted to make that clear since I'd already had that logic in here from before now once you override your function you can go to your CPP file in here I want to actually create my function and to do that you just type the return type and then the class that you're calling on a base item colum colon the name of the function and then the parameter list you don't have to have anything with the virtual the override in here it will know at this point that this is our tick function we're just overriding from the actor's class lastly I have super colon colon tick passing in Delta time super refers to the parent class so we're calling the parents tick function within our tick function and the parent again is the actor class so we're basically calling the actor's tick but now we have the ability to do all sorts of other logic in here let's go into the blueprint and let's take a look to give you a little bit of background we essentially throw an item and it rotates freely until it hits the ground or collides with some sort of object then we want to rotate it to be upright this is just to make it a little bit more visible if you played Super Smash Brothers and throw an item you'll see that they always kind of land and reposition themselves in a nice way so that you can see exactly where they are and go pick them up again that's what we're going for here so I have a Boolean should rotate to upright and I have a branch now one other thing that was done in code part of this episode is this Boolean is actually a code Boolean because my base item has that but if you didn't have it you just go into your base item. any variables that you have in the blueprint you can convert to code very easily and essentially you click on your variable or look at your variable and you'll see this is clearly a Boolean says so in the details panel but I can also tell because it's this red color the variable name it should rotate to Upright so when I go into my code I want to put bull should rotate to upright and that's essentially already converted the variable from blueprint to code I have added a U property line in here so that I can access this variable in the blueprint that may be necessary even if you're converting some parts of your logic from the blueprint to the code you may not want to convert all of them so if you do want to access this Boolean in the blueprint make sure you put U property and then edit anywhere blueprint read write will allow you to access it and edit it within the blueprint the category isn't required it is just to organize things a little bit better at this point I've made my variable so we can go back to the base item. CPP and actually start transferring this logic over so should rotate to operate Branch really a branch is an if statement so we can just check to see if this condition is true and if it is we'll go and do more logic so if should rotate to Upright that is essentially what we saw in the blueprint okay okay so this is the same thing that we have in the blueprint we're just going to check and see if we should rotate to Upright if we should then we are going to go into this if statement so basically if this Boolean is true we will go into these brackets right here now if it is true we have a static mesh we're getting its World rotation and we're doing rotational interpolation to so we're going to rotate it to a specific point and then set that to be the actual World rotation of the static m mesh basically we're going to rotate the mesh to the angles or degrees that we want now to do this we have our static mesh component I can come into the code and I can type something like get static mesh but I'm not going to get anything that's because the static mesh is actually a component on my blueprint it's not one that I created in the code so if I want to actually be able to access this in the code a simple way to do that is to create your components in the code not in the blueprint print so this is a static mesh component you can tell because if you hover over this it'll tell you the information about it you can see it's a static mesh component used to create an instance of you static mesh so basically this was just a component in the list here static mesh here we go static mesh right here and that's what type it is so we need to add a static mesh into the code if we want to do this now the easiest way to do this is to go into your base item. go to where you want to create the component I usually put it under my variables but doesn't really matter where you put it so I will come here to blow my function with my variables and we'll make a U static mesh component pointer and we'll call it static mesh all right that'll be good so this is our static mesh component that we have now at this point we are going to want to close the editor just so that we can build and make sure everything's working we'll come back back to this in a minute but I'm going to close it for now and stop visual studio now when we're in here we have a pointer to a static mesh but that is not going to actually allow us to create a component on the blueprint so what we're going to want to do is add the class keyword this will just know that we're referencing a specific class of the US static mesh component now that we have our static mesh variable we need to actually make it a component on our character or on our actor so in this case we're going to go to our base item. CPP and we're going to go to the Constructor Constructor is usually here by default when you make a class but of course this is where you assign all the default values so we're going to go in here and we are going to assign our static mesh component so we're going to say static mesh equals create default subobject and we need to choose the type of sub object that we're creating we want to create a UST static mesh component because that's what it was in the blueprint print and then we can give it a name as well so we're going to give it text of and we'll call it static mesh in code I don't want to call it static mesh because I do have the blueprint one in there and there'll be a little bit of overlap with the name so I'm calling it static mesh in code and now our static mesh will be related to this object that we create on the blueprint object really you don't need a blueprint object to use the sub object but that's how we're going to visualize it for this episode so that you can see that it worked and that we created it properly now we have our static mesh reference so we can go in here and use it in the tick just like we need I'm going to launch the editor again all right the editor is back open and now you will see that there is a static mesh in code static mesh component right here so what we added in the code did actually work going back to the tick function we can now actually do what we came to do and that is use our static mesh to get the world World rotation and perform rotation interpolation to set the proper World rotation so going into the code we use our static mesh and we want to get World rotation and nothing's coming up so sometimes the names of functions and even variables are different from code to blueprint it is not g World rotation the blueprint it is get component rotation and this will return the rotation in World space which is the same as git World rotation that's what git World rotation does but it is not called the same thing so sometimes there's a little bit of looking that you have to do putting in functions reading the comments or looking up documentation to see what is a match so get component rotation and get World rotation here are doing the same thing now after that we want to use that to do something called R interp 2 now for this one you can see it says the target is Kismet math Library this is really helpful because if you're trying to find R interp 2 you might have a heck of a time so let's look up here R interp 2 well that doesn't tell me anything rotation interpolation doesn't tell me anything but we had seen that the Kismet math library was being used so we can actually use this I have closed the editor that way I can actually get the syntax and you can see it without a bunch of Errors so that you know we are doing it correctly so we're going to want to include that type that it said in the blueprint which is Kismet SL Kismet maath library. now all we have to do is find our R interp 2 function and we're going to find it this time right and we don't see anything so when we include something such as this math Library there's a chance that it is a a static function if this is the case we're going to want to use that library that we included so that is the U Kismet math library and we can do colon colon R enter two and you see there is the function that's the node that we had now I'm going to comment out these things so that we can get it to build because I'm going to launch the editor again the editor is back open so here we go we have our world rotation that we were able to get in the code and now we have our r interp 2 so in the blueprint we are using current Target Delta time and interp speed with these values so let's go to the code and make sure that these line up we have R interp 2 here which uses the current as the static mesh G World rotation which we already have so I can take this line that I made earlier put it as the current comma we need the target Delta time and inter speed the target we left is all zeros because that is my base rotation I'm trying to rotate upright and my items are all positioned upright which is at 0 0 0 in the blueprint so I can make an FR Rotator of zero 0 0 that's my target for Delta time we were using the Delta seconds that came from event tick and we can do that here as well so Delta time is passed into tick we're going to just pass that along and then there is the interp speed which I have as 10.0 so I can go to 10.0 F and now we have copied that over properly going back to the blueprint we were also calling set World rotation on the static mesh static mesh set World rotation so after we have done this let's do static mesh set World rotation this one is the same name as before and the rotation we're setting is this result see the new rotation is the result of the R interp 2 so we can actually take everything that we did in here we don't need the semicolon there and pass it into set World rotation just like this and that becomes a pretty long line but that's now doing the same thing we're setting the world rotation to the value in the blueprint additionally we did some other logic to check to see if the current value of R interp 2 was equivalent to 0.0 for the X Y and Z and specifically this is a tolerance thing so we have the ability to check within a given radius and essentially I'm checking to see if the result of the rotation interpolation is within 0.00001 of three zos if it's close enough to that then we can ignore this process and we no longer want to rotate it to be that position so it's basically going to stop rotating if it gets close enough to that value now you might not know what this note is called but you can see that the target is the Kismet math Library yet again so if I go back into here I can assume that there is something within the Kismet math library that will allow us to do this so we're going to do U Kismet math Library colon colon and we can type equals and take a look in here and now we just want to match the type so we have equal equal Rotator Rotator and that sounds pretty accurate because we are comparing the return value of R interp 2 to a custom Rotator value and it takes in this tolerance let's see if the equal equal Rotator Rotator does that as well so we can do the same result that we had here that we passed than to set World rotation because notice the r inter two has a return value going to set World rotation and this node comma F Rotator of 0.0 0.0 0.0f comma and the tolerance it defaults here to 1.0e to the -4 and that is actually what we have in there for the tolerance but we can put our own value in there as well so we do 0.1f and put a semicolon there to wrap up that line and this returns a Boolean if we go into the blueprint you can see it returns a Boolean and it goes into a branch so we want to add this to an if statement so we can do if and then put this all in here actually want to take away that semicolon brackets and if this is true we want to set should rotate upright to be false so we can say should rotate upright equals false now you can do a few things to clean this up if you want to such as taking the U Kismet math Library R inter to line and setting equal to a local variable so you could say current rot for current rotation and we're going to make this an F Rotator because that's what the kism math where returns and then instead of using this huge line every time we can just pass in the current rot so static mes set World rotation would be current rot make sure to add your semicolon here if you do follow this line so those would look like that and then in the equal equal Rotator Rotator we don't need to do that line either we can just use current rot so I'm going to take out all this stuff up to the first parameter so now we can have if ukis math Library colon equal equal Rotator Rotator current rot the Rotator we're checking against and the tolerance so there we go we've successfully converted the blueprint tick to the code tick function there is one other thing I would like to teach you about this in the introductory episode and that is how you can find nodes that you may not know about if you don't know their code counterparts such as R inter turp 2 now I looked at the Target and saw that it was Kismet math library and of course that works we were able to find it based off that but if you right click on the Node you can also do go to definition which is alt G for the shortcut and it says jumps to the definition of the selected node if available as an example it list C++ code for Native function or the graph for a blueprint function so if we hit go to definition it's going to say reading C++ symbols and it will open it up here if it's possible it doesn't always work it depends on where it's located and your version of unreal so if you have a source build you'll be able to do this for everything if you don't you'll be limited to some things and now if I take away my blueprint tick relaunch this I should be able to do the exact same thing I was able to before but now it's all done in the code instead of the blueprint the only thing that won't work here is because we are using the static mesh that we made here and not the static mesh that exists in the blueprint so when we relaunch we will need to actually set and use that static mesh that we made in the code not the one that already exists in the blueprint but once we do that we will be good to go if you do want to do this and actually use this in your project especially if you're someone who's been following the Super Smash Brothers tutorial you can there's one other thing we need to do and that is say static mesh error setup attachment and we want to use our root component root component is essentially just the root component of the actor simple as that it's basically the component that you can't change when you're in the blueprint you might have a default scene route that's been the root component or you might start out with just a mesh or a capsule collider those are your root components and so we just want to attach this mesh that we made to the root compon component now if we go back into the actor that we made that on as our base item BP it is right here you can click on it you won't be able to set all the default values on it like you should be able to and that is because we haven't done everything we need to make it visible in the blueprint so if we go back to the code and go to the base item. H above where we made this static mesh component we need to make make sure it is visible in the blueprints just like how we make other variables visible in the blueprints so we can do U property and I'm going to close this again because we're going to need to relaunch and for this component we're going to make it visible anywhere blueprint read only and we can either keep this in protected and add a tag like allow private access or we can move it to public I'll go with this one just so I can hopefully teach you guys a little bit more so if you have a variable that is private or protected and you want to allow access to it in the blueprint you can just do meta equals allow private access equals true and the true in here I'm going to put in quotes just like that allow private access equals true now it is open and you can see there's a few issues the issues come from the fact that there's already a static mesh object here so you may or may not have these depending on if you're following it exactly and you already have a static mesh but if you do you can delete the old static mesh then you could get this here where you have events in use on them and they're going to become invalid if you delete it so I hit no and I have to find out what events are using them and those are going to be my on component hit events so if I click on my new static m I can scroll down to the bottom and just move these over so on component hit view you'll want to make sure that before deleting your static mesh you have everything transferred over for your class default so I look at the one that I'm going to delete I want to make sure that everything on the standard static mesh also matches this and so few things in particular the lock position lock rotation I know for sure are not set up by default so I want to make them match so on the one that I already have I'm locking X position and locking rotation on every axis we will want to do that as well and just really go through and be thorough here so I can see that my physics look pretty much the same here on my Collision settings make sure they match and right now they don't you can see that they should be set to Custom Collision Collision enabled query and physics with World static so I will want to change this to Custom Collision enabled query and physics and world static additionally everything is overlap except oneway platform and Stage so let's go ahead and do that oneway platform and Stage are block so now those are all the same scrolling down more in this and there we go now since these are the exact same name it's giving me a a little bit of trouble so I'm going to delete it I'm going to hit yes and it's going to give me a little bit of trouble I can try refreshing the node and compiling and that should work if not you can go back down to here on component hit and add it and you'll get a new one in there now the other issues are with the setting Collision response to channel so it had used the static mesh as the target here and we're going to just do the same thing right here and we want to go to the next there and we're just going to do this we're definitely using the static mesh here as well just want to use those as the target eventually this should compile we will want to go to any children so like the sword weapon BP and make sure it still looks good so in my case it does not because the static mesh does not have the actual mesh that it should but I can click on the static mesh and go to this area and I can pick it again so I forget exactly what I was using using but you can use really whatever you want so I have this pillar and something like that will work there's my sword the other items like base weapon BP they do exist they do use the static mesh but the base classes are just wrapper classes they don't actually have meshes on them may need to do this for your other items so like my health pickup I could set my static mesh so for this I just used a cone so now we have this health pick up is looks like Stone and lastly we have this Mystery Box BP which I can set up again as whatever it was a cube before so we're going to use that again that's fine anyway guys thank you so much for watching I hope you enjoyed and I hope this taught you a little bit about converting blueprints to code if it did Please Subscribe and consider joining the patreon for more benefits and supporting the channel if you ran into any issues with this episode feel free to message me on Discord or leave a comment on this video I'm Sean the bro and I'll see you in the next one goodbye [Music] [Music] guys
Info
Channel: Shawnthebro
Views: 6,706
Rating: undefined out of 5
Keywords: Shawnthebro, Shawn, the, bro, Shawn the bro, video, game, video game, Shawn thebro, stb, ue5, ue4, unreal engine 5, unreal engine 4, unreal, engine, ue, unreal tutorial, ue4 tutorial, ue5 tutorial, guide, converting c++ to blueprints, c++, cpp, convert c++ to blueprint, blueprints, blueprint, visual scripting, visual, scripting, convert project, c++ to bp, bp, convert, converting, how to, how, how to write code, how to make, convert blueprint to c++, convert blueprints to code, bp to c++, c++ code
Id: Ke936YiTGpE
Channel Id: undefined
Length: 28min 22sec (1702 seconds)
Published: Fri Nov 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.