Unreal Engine 5 Tutorial - C++ Part 2: UProperty & UFunction

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] foreign C plus plus tutorial Series in the first episode last time we talked about how we create a c plus class and how to expose variables from the C plus source file into the editor for designers to use now the other side of that coin is functions and events so in this episode we're going to go through how to set up functions and events and explain how that works and exposing those from the class into your blueprint editor so let's get started foreign actor that we started working on uh in the first episode and we set it up and showed you how to do properties now let's talk about uh functions and how they get called and they're very similar so in your header file you need to find what they are so we're going to go to public here and you do you function so you property for verbals you function for functions and again in Brackets we have a few arguments that we could pass through to this um and we want it to be callable by blueprints so we're going to do blueprint callable and give it a category as well and if you give it a category that means it will show up in the right click context menu so do get used to adding categories to these things so go test functions we'll call this and in here we'll Define our function now with function just to first of all Define the return type if it's not going to return anything you just return it as void if you can return a value then you put in what type of value it's going to be so for our example here we're going to uh a void here we'll do void and we'll do uh calculate value and I'm gonna give it another variable so your property here we're going to change this to edit anywhere and blueprint read write uh read right and get rid of transient and leave us Academy there so that'd be value I'm going to do value a and then I'm going to copy this and put another one in for Value B there we go okay so we've got value a value b u function booping callable category test functions void calculate value oh and you have to give it um some brackets on it like that to indicate that it's a function okay now when you get a green underline Visual Studio it just means that you've got a function but you haven't actually done anything with it like there's no no source code for it so it's not going to actually do anything um so it's just giving you a little hey you haven't yeah implemented this so let's go ahead and implement it so calculate value we're going to go into uh thing here we'll do it you could do it before after these events let's do it before and in here we'll do void calculate uh calculate value and I'm gonna put in code brackets to indicate that you're defining the function and in here we're going to look at Value a and value B so we get value a and we're going to add value A and B together so what I'm going to do here actually yes um let's do this as an INT return I'm going to change this to a void to in 32. okay there you go and just change this to match in 32. um and we're going to do return value a plus value B and we'll get rid of these guys here um we have to Define what these values actually are to begin with so we're going to go into here and do value a equals zero value P equals zero as well um and I did not Define the type did I yeah I did I don't know if I can I'll click value done all right oh yeah it's not appearing because I didn't actually put this in we have to put in a my test actor and then two colons there that is basically saying you're going to find this function and all the relevant stuff it's gonna be part of this class so you're just saying this is part of this class here um okay it's actually return those two values there so if I hit um save awareness okay so because it's a u function is blueprint callable we should be able to access this in our blueprint let's go back into our project and hit the hot reload button that should go through hopefully it was a green success there you go and we'll go into our child blueprint class here and you can see you've got value a and value B up here working like this and what we can do we do begin play and we can search for our function calculate value and you can see there that's right value put that in you can see I'd have to give it any parameters because this is just going to read from these two values here so let's do a print string like this print string that value and on the cross defaults I could put in whatever one in here so let's do 10 35. okay so we should expect to see 45 being printed out in the top corner I hope we don't actually put in the scene and push play 45 okay so that means we can access that function inside our blueprint and likewise we can also access it elsewhere too so if I was in my say level blueprint I can get the reference to my test actor and then from there I can do calculate value as well okay so I can access it from anywhere we'll look at reference to it I can access it and that's because it's public okay so let's talk about then how do we actually make it work as an event so how do I custom events work for our classes so events in C plus are just functions but with a slight bit of different definition so if I go into my class here um it would go up to blueprint callable we'll just do um another function here go U function blueprint portable uh sorry not recordable uh you want uh implementable event whatever yeah uh implementable event and then a category that you want to belong to category which would also done a typo I'm going to be test functions not text functions let's fix that here there we go uh test functions and that's not going to have any return okay so we're just going to call it void and say um on value calculated okay so it looks exactly the same as a normal function that you put in except it doesn't have a return because events don't return anything and we want to be defined it as an implementable event now there's also native events we'll go through a difference of that in a second but first of all let's just make this output a test here so we're going to go and put this in here and we'll do void and it's not going to have anything in it so we do void um a my test actor and do on calculate uh value calculates there you go on and then just put in that there okay leave it like that hit save all that should all be okay and then we're going to go compile let's go back to our project copyload uh build failed what I've missed I've spelled something wrong very easy to do uh category there we go so with events we don't want to actually Define this in our source file we're just going to leave it undefined because what you're going to do is say you're going to let the blueprint class Define what that event should do so this works much like how a begin play would kind of work essentially um so to Define it as an implementable event uh category and the name of it now it would be green underlined that's totally okay and void as well because events have to be put in so once you've done that save it all and then go back to your project and we'll do a quick hot reload there we go and wait for it to go green so accessibility linked patch and here we go so now if I go back to my custom class here I go to overrides you'll find in my function overrides our new one on value calculator hit it right on value calculate there we go and now so this is an event that gets cooled um and I believe that we can make this event do stuff and so forth and because this is inside the C plus code you can make other things cool this thing here let's make our calculate value function here call this so if I put print string in here uh we'll make it say hello and compile and then I'm going to go back into my source file here and go to my source CPP file and when I call calculate value I'm doing return value A and B um I want to just call my on value calculator like so okay and then if I save that and then compile the project again uh hot reload there we go finish patching and a second yeah Mom you can do it there we are so now my test actor what should happen is on begin play it's going to do this function here which is going to trigger the C plus code to run this event so it should say 45 because of this and then hello now if I go push play hello okay so really cool useful thing to have um so what about if I want to pass through a value in here okay well what I'm gonna do it's going to go back to my header and source file and inside my U function for on value calculate I'll just put in here in the besides brackets the parameters I want it to have so here I'm going to Output in 32 and we'll give it a name of um uh value okay and if I go to my test actor source file this is now going to give me an error message because I'm not outputting a value here and what I'm going to do is I'm going to Output value a and value B inside of that and we'll return um to do that and then we would turn value as well we'll keep it there there you go and semicolon always underlying with semicolon and the command so this is going to do on value calculate and then pass through value a plus value B so if I do now save all go back to the project and do hot reload there we are and if we go into our test actor now our event is now outputting a value which means I can just plug the value into there straight away and I can just remove this pinstring here so that's going to call that and then that's going to Output this event file close push play and it's just print 45 there we go okay so that's how we get our events callable in our blueprint but what if we want it to have some default behavior that we can override if we so wish in our blueprint well that's a different type of event you've got implemental event and this one we're talking about now is a native event I'm going to show you just the native and this is going to be a void uh a function and we're going to have no parameters in it so we're going to take out the parameters like so and on value calculate would do that now the thing that's different between this and the previous one is that this one actually does need to have a definition for the function so in here you can do int sorry void I ate my test Hector close your column and on value in 32 value okay and that's going to be a little bit different now because it's now going to be doing a bit of code for us before we override it or extend it in that blueprint so the thing that's going to be different here is uh what's this up to edited Apple oh yeah because we ain't got um but we just took that out okay so it's gonna call this one now on here you're gonna see as well we're gonna get an error on the value calculator here but now we're just gonna take that out for a second and just leave it uh with no parameters um some value calculator here but if we want it to call this automatically for us uh we're gonna add a suffix to this which is underscore implementation and and that's what you've got to do now it will um we'll now do the bit of code that we do in here I'll do a print string so do a pinstring we'll go into this more detail next time but um you go engine G engine toy access modifier um no access modified the um Arrow denotion and we're gonna do add on screen debug message um the key for this would be minus one the time to display will do 10.0 float seconds uh color we'll do F color uh blue and we'll do some text here we're going to do um hello okay and we'll just put that on there okay so what's gonna happen is when we call the uh um our calculate value function that's going to call this event and this event is going to run some default code so I'll show you how this all works inside the engine so at the moment the only thing that's changed on our header is just a native event you will get an error saying that we're not calling this event that's fine that's correct um we just want this implementation here instead so with that saved we can go back to the engine pop reload and wait for it to do its job there we go and now when I push play it says hello um so what I've done in my test here all it's doing is just begin play calculate values and you can see we've got no other event going on it's just doing that and that's triggering the underlying event for calculating values which I'm gonna get hello from it but I can extend or override it as well so if I wanted to go to functions here I can go to override and choose on value calculate and now I've overridden it so now it won't do hello it will do nothing yeah but if I want to extend it it works exactly the same as if you've done this before with blueprint just right click on it and go add call depend function so now you're extending it because it's now going to do hello and then whatever else we want to put in after the buds on that blueprint and there we go we've now can access our variables and our functions and our events from a custom class in the next episode we're going to go through some common functions that you guys are probably used to using in blueprints such as the print string node and showcase how to actually accomplish these inside of a c plus class itself you can watch the next episode right now over on patreon.com forward slash Ryan laylee we found all my videos early before anyone else from just one dollar a month Master thank you to all my patrons and YouTube members for the continued support and thank you for watching and I'll see you next time bye everyone [Music] thank you [Music] foreign
Info
Channel: Ryan Laley
Views: 17,700
Rating: undefined out of 5
Keywords:
Id: B9czUTN23SQ
Channel Id: undefined
Length: 19min 45sec (1185 seconds)
Published: Tue May 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.