Unreal Engine 5 Tutorial - Convert blueprint to c++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone uh in this video we're going to take a look at uh a project where I move a function from blueprint over into C++ uh so you can see here that uh we have this actor in the world that's spawning these bombs at random locations at a radius uh we're going to take a look at how we make this uh the logic that does this in blueprints uh you can take a look at here uh we have a blueprint function uh that does this and then we move the logic of the blueprint function over to C++ uh it's not a lot of code but uh the the purpose of this video is to show the process uh rather than than actually uh explain in detail what what we're doing uh and we end the video of on showing uh how we can extend the C++ implementation and do some interesting things for debuging for example here uh we're adding we have this deug show debug points option on the uh the bomb dispatcher actor we have a distance from center Val value that we can change so if we put that to 2,000 we can see that the the bombs are being spawned uh anywhere between uh yeah the the location and the 1,000 uh uh 2,000 uh range uh so let's just uh jump into it and get started so we'll start by making a new Unreal project this is a C++ project uh that uh that I created it includes the the starter content so uh let's begin by making some of the these uh the actors that we need for this to set up so I'm starting out just making an actor I'm going to call it BP uh bomb dispatcher uh and I'm going to make another one and calling this BP bomb BP bomb and the blueprint logic that I'm going to make is I'm going to go here into the bomb dispatcher I open it up here let's see like so okay uh I am going to start by making it spawn the uh spawn these bombs that that were we're going to have uh and the way I'm going to do it is I'm going to do set timer uh by event so every let's say 2 seconds uh this is going to call a custom event called uh spawn uh it's going to call a function that we're going to make and I'm going to do the function is going to be called get uh complex bomb location and this is the function that we're going to be moving over to C++ later uh so starting out we're going to do get actor location we are going to create a random unit vector and we are going to uh multiply the random unit Vector with a number let's say 500 on all axes here uh we're going to add this together uh let's actually do it a little bit better so let's make now this is fine so we'll do it like this and then we're going to return here the location so now we're going to get a random unit Vector uh in that is 500 units away from the from the unit and let's just start by calling uh Spawn from glass uh we're going to spawn our bomb here uh and we are going to call our G complex bomb location and we're going to use the location that we got from that in here like so and uh yeah so the logic is now going to be we start on event begin in play uh for so every 2 seconds uh it's going to loop it's going to call the event spawn bomb the first thing it does it calls the Gat complex bomb location logic and uh this is going to be the the heavy logic that that we want to move over to C++ so if we just take a look at that so I double clicked on it it gets the actor location it generates a random unit Vector uh multiplies the random unit director with uh a value 500 on all axes add this together and it returns it and then we go back here to the uh to the graph we use this this gets cast into a transform and we spawn the bomb actor so just to finish the setup uh we're going to go into the bomb actor and here we're going to go in and on begin play let's say spawn uh emitter attached and let's attach the emitter to the scene root so that means that the this emitter is going to get spawned at the at the root of the seam uh we're going to spawn the explosion I have this explosion here because the uh uh because I'm using the starter content and uh and that's it so let's see what happens if we if we play this now uh so I've created a new level I'm just going to add my bomp dispatcher here in the uh in the world and now we can see that uh we're getting a a sphere of bombs but I don't I I only I don't want this to I I only want the bombs to be I want the bombs to be level with the uh uh with this here yeah level with the actor so I'm going to take the uh I'm going to split the vector so we're going to get the x value and the Y value and then we are going to do a break we're going to get let's do it prettier uh let's break the actor location here and we're going to just use the zv value of the of the actor location so all of the bombs are going to be now spawned uh at the same location as the uh we can even go in and if we add a plane let add a add a tiny Cube so we had a cube here just so that we see where it is let's call this 01 01 01 just so that we see it in the in the world and now that we play we can see that it's it's here and all of these bombs are are being spawned level uh level to it so I can even go into simulate mode and I can move it around a bit so if we put it here on the ground now all of the explosions are happening on the on the ground right so uh so this is the setup so let's move over to the uh to moving this over to z+ so we're going to imagine that our function uh the the calculate get complex bomb location function is a really uh really heavy function and we want to we want to move it over to C++ for for whatever reason so let's take a look at the steps that are required to do that so first uh we go to tools here at the top and we do new C++ class uh we're going to add a a parent Class A parent class in C++ where we can Implement uh Logic for our bomb dispatcher so I'm going to pick actor because this is an actor uh I'm going to give it the name bomb dispatcher dispatcher so the name this is just going to be the name of the file uh it is also going to give us the name of the class uh but uh but this doesn't really have to be bomb dispatcher the names don't have to match but it's nice to have them matching if it's uh and one of the conventions that I try to keep is that you see that I have bpor bomb dispatcher and then I have the the base class in C is a bomb dispatcher uh one okay so do create and we added to the project um so while this is created let's take a look at it should open up visual studio for us here we go okay so the new class has been created so let's open up visual studio yep we're going to reload it uh so this is what we should be seeing uh so now you can see that we have a new class it's called Uh bomb dispatcher and it's actually two files if I if I spread this out a little bit uh in visual studio uh so we have the CPP file this is where the logic is implemented and then we have the header file or the H file and this is where we we put our definitions and and and things like that uh okay so uh now let's go back to Unreal and let's take a look at uh just basically the first step how do we associate the this blueprint how do we make the blueprint kind of be connected to our new newly created cclass so we do that by opening up the blueprint here uh we go to class settings here at the top and here you can see that the parent class is set to actor uh if we if we go back to uh visual studio uh we can see that our uh the the actor that we we already have uh the actor that we made in C++ it inherits from actor so uh the definition of the uh uh of the actor of the our class here is is is here and this line here means that this this inherits from actor so uh the point of that is that if we go back to the blueprints we can easily go and we can reparent it so uh the current parent of the is uh actor and we can just say uh we want your parent to be bomb dispatcher uh you can see there was a little bit of a of a refresh uh but nothing uh nothing really happened uh so now we're actually ready to start uh migrating the con uh the uh kind of the logic over so first thing that I'm going to do is I'm going to rename the uh the function here to BP get complex bomb location and I'm going to do that so that we don't run into our naming conflict when we recreate the function in uh in C++ so I'm also going to copy the name here and let's go over to this here oh I wasn't in the right spot here we go all right so the first thing that we want to do is we want to make the like we want to make the function so just like in in the blueprint uh when we create press the button here uh create new function and we get a new function here uh we can do this in C++ as well uh so do it like this here and the way that we do that is here in the definitions into the the header file uh under public you can see that when I created the class we got a couple of things uh for free we got the uh we got the Constructor uh which is kind of logic that runs before uh before everything else it's something that assembles the object uh we have the begin play function so just like in blueprints we can add logic here to begin play and we have the tick function uh which is the same as in in Blueprint we we can do stuff on on tick but we want to make a new one uh so here uh we add it just at the bottom of the file but it's important uh that it's under this kind of this public section here so uh we're going to say that our function is going to return the first thing that we need to decide is what does our function return and we're going to say it's an F Vector so the F Vector is the same as the yellow uh line or the yellow variable in in unreal so we say our function is going to be returning an F Vector so we first say what it's going to be return then we say it's the name of the function and then we do open and close parenthesis and uh the kind of this symbol here that I I can never remember the name of semicolon or yeah uh all right so now we've kind of defined our function but and normally in C++ if you were just working in a regular C++ uh program this would be enough but uh because we're an unreal we need to add something on top of it as well we need to mark it as a u function function uh and we need to add a couple of the couple of kind of settings to it or or parameters and the one that we're going to add is blueprint callable uh and this means that the function is going to be available and callable in blueprints if we don't add blueprint callable to this uh we're not going to be able to use use the function and then uh we need to so now we've just added the definition to the function and if we we were to com uh compile our project uh we're not going to uh we're not going to see anything and we're probably going to get an error uh so we need to add an implementation for for this function and we do that we can just copy if we just look at the pattern here uh we have to add the return uh kind of type so F Vector we add the name of the class it's a bomb dispatcher and the a means that it's an actor and bomb dispatcher is the class name we do double semicolons or colons sure uh and we add the name of the function open close these are the input parameters if we had any and then we do open and close curly brackets and now the only thing that we have to do is it expects us to return a function uh it expects us to turn a return a vector so we'll do F Vector uh result so it's going to return this so now we've just created an effect with no with nothing in it and we'll do return result and that's it so now if we go back to Unreal Engine uh and we compile uh nothing is going to happen we don't have access to this yet because uh now that we've added C++ code uh we need to compile uh we need to compile our code uh and the way we do that is just want to make sure that you can see it so if I move a little bit uh there 's a button here that's really nice to use uh that's not good uh called a recompile and reload C++ code on the Fly it's it's a little button here on the on the bottom left you can do this through Visual Studio if you want but uh uh this is pretty pretty powerful so you press the button uh now it's going to go in and it's going to compile our code and reload the reload the editor and uh we can see that uh we got a little green box here saying that this was uh uh this was correct uh now now we can go in here uh into our bomb dispatcher blueprint we can see here at the top right that the uh that it is inherited from our bomb dispatcher and even if I uh yeah if I'd run this through Visual Studio I can click on it and get go into it uh but now if we go to our begin play here and we have the BP get complex bomb location I can right click here and do get bomb get complex bomb location and now you can see that I have a function here uh uh available G complex bomb location and this is the function that's coming from C++ uh right now it doesn't do anything uh we haven't moved any of the logic over to it yet so uh so let's start moving the logic over so looking at the logic that we need to move over to C++ uh it's a it's not a very not a very complex uh not very complex but I just want to show how uh how we uh actually move some of this stuff over and then uh we can take a look at more things that we can do so uh if I just minimize this and take a look so the first thing that we we want to do is we want to get an actual location and we want to get a random unit Vector so uh let's just start filling out our G complex bomb location function uh with this logic so the first thing that we want to do is we need to we need to get the random unit vector and by Googling that uh the way that this is done is uh let's see uh we need to create uh a random stream so you can see here that uh the random stream here like this is uh an implementation to get random numbers and this I I created an inst uh an instance of this called random I called rant and then what I can do is I can call uh rant. get unit Vector here so this is going to give me a random unit Vector so if I do F Vector F Vector uh random unit Vector equals now uh this is the code that is needed for me to get the random unit Vector so now I've uh I've created the same logic as we have here random unit vector and it gives me the uh a random Vector with a length of one like this is uh this is how that's done at C++ uh we are going to take a look so we didn't we have the actor location and we here we go so we multiply the random unit Vector with uh a constant so we did 500 here uh now it would be kind of cool uh to have this as a variable so uh what we want to do is we want to create something called uh uh the random location at a distance so we're going to get now we have a vector with 0 to one uh and in the blueprint what we did we multiplied the this by 500 so we're going to do the same thing we're going to save this into a variable we're going to get the random unit vector and we're going to multiply it by we can do we can do 500 here so now we uh now we've gotten the same logic uh but another cool thing would be to have this as a variable for the user so uh the way to do that is here in the header file is uh we can create the uh distance variable so this could be a this actually be a float that I think it's done like this so uh let's create a float and let's call it uh distance from Center and let's give it a default value of 500 and here uh we need to do the same thing as we did with the function we do U uh uh property because this is a a property we do blueprint read write when we do edit anywhere so this means that uh the blueprints can read and write into this this property or this variable and this can be edited anywhere you can do edit this in um on an instance of the object or in the in an editor or something like that so now since we added this here into our uh into the header f file we can we can use this variable in our implementation here so now I can just say the random location at a distance here is the random unit Vector times distance from the center and uh we still we're still just res returning the uh the the zero results or the non result uh because we haven't started using this yet but uh but now we've moved this logic here over to C++ and now the only thing that's left is we want to zero out the the the Z uh property so we go in here and we say we want result to be the random location at a distance and we can say result do Z which is the Z value equals uh oh now one thing that we uh we forgot to do uh we want to take the random location at a distance and we want to do the result equals uh get after location so we get the location of the actor and we multiply no we sorry we add the location of the actor to the random location at a distance so this is the uh this is the multiply that we we're doing here this note basically here all right so uh and just to finish it off uh we wanted to return the result uh with the Z value of our result being the value of the actor instead of the value of the uh uh uh of the unit Vector so and the way we do that is we do result. Z equals get Vector location do c and we need to type spell this correctly and this one here as well all right so now you can see that I've uh I've modified the result value here so the Z value of the result is the same as the Z value of the U of the actual location and then uh we return the result so this should if we did everything correctly this should result in the same logic as we had before so if we go back here into our uh set event timer event let's try it with the blueprint first and see what happens okay so this is working the way that we expected let's switch our function out take the blueprint function out and uh we move it here we can reuse this and this should give us the uh same yeah right right so uh right now one of the things that's happening is we forgot to compile so here at the bottom uh under my uh body uh we need to press recompile and that's going to recompile our C++ code uh just so that it's uh it's usable there is a way to turn on auto autoc compile anytime that the the function change anytime the code changes but I like to do it uh explicitly okay so there's something wrong um and because we're just getting the uh this information like it's just being spawned in in a single location and that's fine uh so here I'm going to debug this it's probably something very very simple uh I'm going to show you what you can do uh one of the cooler things that you can do with C++ you can't do this with uh with blueprints as well but the way that I would I would work now is I would start adding uh kind of debug information into into my actor here so let's start by uh adding another property here so the goal here is uh debugging our uh debugging this bug uh and try to visualize a little bit what we're doing so I'm going to add a new U property I'm just going to copy uh this here this is going to be the same and I'm going to the one other thing I'm going to do a cat category debug and I'm going to do Aon and this is going to be a show debug uh point and I'm going to initialize this as false the beginning here we go all right so uh so we have some logic here uh that's not doing what we want uh the basically the first thing that we want to see is so we're generating this random unit Vector uh we want to let's try to see where uh where that is so if we do if show debug point here we can do uh there's a function called Draw debug Point here and this takes in a whole bunch of uh kind of function uh a whole bunch of parameters uh and I can just go and uh and start filling them out so it takes in a world so I can call G World it takes in a position so now I'm going to use the random unit vector position because I want to I want to see where that is uh uh what's happening there uh it takes in a size so let's just give it a 10 it takes in a color and color can be uh you so you can see that it's asking for an F color which means that this is a special uh kind of type of kind of color structure so we do F color and do double semicolons and we can do blue this gives us the color blue and we say uh persisted lines we say true and everything else we can just leave an a default uh so to make this a little bit more readable I'm going to do it like this move so it need to be like this yep like this and then like this and like this and then we add a uh this colon thing here at the bottom all right so if uh we say show deug Point uh if the flag show debug points is Set uh we're going to draw a debug Point here and uh maybe that's going to help us see what's wrong and we compile so the thing to notice here is uh we added uh if I go into my scene here you can see I have the BP bomb dispatcher uh actor selected here now here in the details panel if I move the details panel over a bit uh we should see a new uh we can see here that we have the bump dispatcher properties here this is distance from the center 500 but we also have this deug one now and we can turn the debug rendering on and off uh any way we like so I'm just going to start we can see that it's it's exploding here I have the bump dispatcher uh kind of on okay so I'm just notied that the bombs are not disappearing so that's something that I forgot to do so I'm going into the bomb blueprint uh I'm going to go and look at Lifetime and I'm just going to say that the uh the bombs are only supposed to live for one second so uh we don't if we run this for too long okay so back to the uh the bomb dispatcher here so if I turn on show deug points you can see that uh everything is so it's drawing these points all of them just here at the uh yeah at the center of the world at 0 0 0 which means that our function here uh get unit Vector is not returning anything other than zero uh every time we can also uh we can also let's just do that we can also print the the lot uh we can do a a print and that's done by let me see quickly I'm adding a yeah so I always have to Google this uh really quickly add in the the default locking command here here we go so uh we are drawing the debug points here uh but it all would also be nice to know the uh the location of the point is semicolon and then we do uh percentage s and we do random move this below like so so I usually just Google Google the ulog uh uh command or function um and we're going to say random unit Vector to string so a lot of these uh a lot of these things that I'm doing like the star here and uh all of this this is just something that uh you just need to pick up and and learn and it takes a little bit of time uh but I just wanted to show the process a little bit uh on how I would do this okay so now we have a log statement here it's going to give us a warning and it's going to tell us the location of each point uh here okay cool so let's go back into unreal and uh see if this plays uh we're going to go ahead and compile our code and that takes only a few seconds so now that I play this I'm going to turn on the uh I don't have to turn on the show debug points because we are getting uh uh yeah so here we can see that our location of the point is the same thing every time so there's something wrong with the the random Generation Um and what we can do is we can go in here and we can call generate at new seat uh and this is actually something that I uh I forgot to do earlier so generate new seat basically initializes the random uh generation objects if I understand it correctly so let's recompile and let's see what happens now okay so now we're getting something cool something cool is happening uh the bombs are appearing in the right location uh we can see here on the right that the lifetime that I added is doing it's correct thing and uh the yeah the values that we're seeing here are just really really small because this is the the logic of the this is the unit Vector uh these are the points of the unit Vector they haven't been scaled up so let's go back into unreal um so let's move we should have done this earlier let's move the the log uh into the debug points here so now now we have draw debug points here uh and we can continue while we're just we're just building a tool around this uh that uh yeah it's just going to help us visualize and and make the function exactly work the way that we want it so I'm going to go and I'm going to take the if show debug points here as well uh and I'm going to just add the draw debar points logic in here again let's change the color to red and instead of drawing the uh the unit Point uh we're going to draw the result here we go so now if we go in and this is not supposed to be here now if we go in and recompile uh we have now uh moved if I clear this here so you can see that the logic is working the way that we wanted it uh but we also have this really really nice button here or this flag here that lets us see the debug points that are being spot so now we get a red dot every time there anytime there is an explosion and we're also rendering out the the unit uh the unit graph here uh so yeah you can see the uh kind of the distribution of the points here uh and we've also taken taking a look at how uh yeah I guess the a good way to end this would be to go ahead and delete the blueprint implementation of the of the function uh because now we've uh we've actually gone ahead and we've created the uh yeah we've moved our function over to C++ and uh We've added some more uh We've extended the function to be uh yeah kind of more flexible and and and things like that and one the only thing that I wanted to double check here is if I if we run it here we turn on the debug points and we change the distance from Center to 2,500 uh now you can see that the points are being generated here further away and um and yeah so uh so that kind of concludes it uh if you enjoyed this I um love for you to subscribe or leave a comment if if there's something that uh could have done better or if you know if it's called a semicolon or a colon or or whatever it's called but anyway uh thank you it's been fun
Info
Channel: Gisli's game development channel
Views: 17,533
Rating: undefined out of 5
Keywords:
Id: S8VFVt8p1cY
Channel Id: undefined
Length: 33min 24sec (2004 seconds)
Published: Sat Oct 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.