‫Hello and welcome.
‫In this lecture, we're going to be learning about functions that live not in the level blueprint,
‫but on a particular blueprint class.
‫These are called member functions and are absolutely crucial to object oriented programming.
‫Let's dive in and see how they work.
‫So in our spawn projectile function, in the level blueprint, we have got a load of nodes here relating
‫to adding the impulse, getting the direction of the impulse, and getting the static mesh component.
‫All of these nodes use the BP projectile instance in depth.
‫Wouldn't it be great if we could take all of this code and move it in to the BP projectile so that the
‫code that deals with that data lives with the data itself?
‫Well, that's the entire point of object oriented programming.
‫So we can define object oriented programming as functions living with the data that they manipulate.
‫So how does this work?
‫How can we make functions on particular instances of things?
‫Well, one way we can do this will go to our be open up our BP projectile class and make sure we click
‫open full blueprint editor is you can see that in this full blueprint editor there is a my blueprint
‫pane and a function section so we can hit the plus arrow here to create a new function on the BP project
‫class.
‫And I'm going to name this function, print your name and this print your name.
‫Function should do just that.
‫You can see we've got a node that starts the execution of this function so we can pull off its execution
‫pin and type print string.
‫So that's going to print out.
‫Hello.
‫Not exactly the name of this function, is it?
‫But we'll sort that out in just a second.
‫So what we've got here is known as a member function.
‫A member function is a function on a class that always gets called on a particular instance of a class.
‫So you don't say, I'm going to call print your name on all BP projectiles.
‫You have to say, I want to call, print your name on BP projectile number one, or BP projectile that
‫has just been spawned.
‫So if we go to our level blueprint, what you would have to do is take the reference that came out of
‫spawn BP projectile.
‫And from that reference you could call print your name and you can see it automatically connects to
‫a special input node called Target.
‫And obviously you've got to hook up your execution pins as well.
‫I'm going to hook it up to happen after ad impulse.
‫But what's interesting about this special target input node is that if you go over to the BP projectile,
‫you'll see that print.
‫Your name has no such input node.
‫So what's going on here?
‫Well, implicitly, any member function has a parameter which is its current target or the current instance.
‫And you can get hold of this by right clicking in the blueprint event graph and typing self.
‫And you can see there is a node called get a reference to self and it just creates a self node like
‫this.
‫And then you can use this self object reference to get hold of any of the properties on this particular
‫object.
‫So if we wanted to get hold of the name of our BP projectile, you can pull off of the self reference
‫and type in get display name, which we've done before.
‫And then you can hook up the display name into our print string.
‫So now when we play and we fire off a new projectile, you can see it's printing the name of the projectile
‫and you can see for every new projectile I create, the name is a different one because the self that's
‫being referred to by the member function is a different self is a different projectile.
‫So we've learnt that self is a node available inside of member functions that always points to the specific
‫current instance that we are using.
‫So the whole point of looking at member functions in the first place, the reason we started was because
‫of these nodes in the level blueprint, the where we get the static mesh, we get the forward vector
‫and then we add an impulse to these.
‫These feel like they should live on the BP projectile.
‫And so that's what I would like to challenge you to do.
‫So this is a bit of an evolved challenge, but we're getting towards the end of the section.
‫So I will take you through step by step.
‫So we're going to make a launch function.
‫Shin on the BP projectile.
‫So create it on the BP projectile.
‫Go to that plus button.
‫Add a new one, name it, launch.
‫Then cut and paste the nodes over from the level blueprint into this newly created function of yours
‫and make sure you hook up execution pins and etc..
‫Now you're going to try and compile this that we haven't done this very often in blueprints, but compiling
‫just allows you to essentially spell check your code and make sure that there aren't any errors that
‫the Blueprint compiler can foresee.
‫So the way you do this is you go to the button in the top left and you hit compile and if there are
‫any errors, they will show up at the bottom in the little pane called compiler results.
‫You can see I've got compile successful, but any errors will show up here and sometimes you'll get
‫a little bit of red on a node to indicate where the error is.
‫And I'd like to see if you can try and understand what that error is saying and try and fix the error
‫using the new self node that we have learnt about.
‫And then you need to go ahead and call that function from the level blueprint by dragging off of the
‫instance like we did for our print.
‫Your self node drag off of the instance of the new projectile created from the spawn node and call the
‫launch function.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So I'm going to go over to our BP projectile, hit the plus button next to functions in the my blueprint
‫pane and type in launch to name the function that let's go back to our level blueprint, cut out the
‫nodes and go into the BP projectile and paste them in.
‫Now I've got to hook up the execution from launch to add impulse and now I'm going to hit, compile
‫and you see I get an error on the static mesh or get static mesh node.
‫And if you look in the compiler results, it says it uses an invalid target.
‫So target this input here is not correct.
‫Now as I recommended you do is you can right click and type in self to get a reference to self and then
‫bring that self into the target instead.
‫Now if you hit compile again you can see the error has gone away and it's compiled successfully because
‫what's going on is that it's now able to get the static mesh component from the current projectile that
‫this launch function is being called on.
‫So if we go back to our level blueprint now, we can take the output pin from our spawn actor and we
‫can type in launch to call our launch function.
‫You can see the execution pin was automatically hooked up for me and then out after the launch function
‫we can hook up our print your name function if we want to, although that was just for debugging and
‫demonstration purposes and if you test it out, you can see that it's still launching successfully.
‫Except now the code that deals with launching lives on the projectile itself.
‫Great stuff.
‫We've learnt about one of the core concepts of object oriented programming in this lecture member functions.
‫I will see you in the next one.