‫Welcome back.
‫Now in this video, we're going to talk about hit events.
‫We would like to detect when the projectile hits something and that way we can do something in response,
‫such as destroy the projectile.
‫And later we'll handle doing damage and causing explosion effects and things like that.
‫So let's talk a little bit about hit events now.
‫Our projectile has a mesh component.
‫It's type is you static mesh component and you static mesh component is one class in a long hierarchy
‫of classes and the unreal engine system one of its parents is you primitive component.
‫Here is the documentation page for you primitive component.
‫It says you primitive components are seen components that contain or generate some sort of geometry
‫generally to be rendered or used as collision data.
‫So they're basically scene components with some added features.
‫And if you scroll down and look at the hierarchy, you'll see that we have you seen component and derived
‫from that you primitive component and you can keep scrolling down and see all of the other classes that
‫derive from you.
‫Primitive component.
‫Now the reason we're pointing this out is because you static mesh component inherits these features
‫from you primitive component and you primitive component has the ability to generate hit events.
‫Now, what do we mean by generate hit events?
‫Well, let's say we have some geometry in our world represented by the rectangle to the right, and
‫we have an object with a static mesh component represented by the circle to the left.
‫Now, if this object that has a static mesh component is moving towards the block, then if this component
‫is set up to handle hit events, then as soon as it hits that block, the hit event will be generated
‫upon collision.
‫Now a hit event will generate some data pertaining to that collision, including the component that
‫was hit.
‫So this block here may have a static mesh component.
‫We can access that component upon colliding.
‫We can also access the actor that was hit and the head event also generates an EF hit result.
‫We've learned a little bit about the F hit result and we know that it contains lots of variables about
‫the hit event, such as the hit location, which is an F vector representing a location in the world.
‫So we have our projectile mesh.
‫This is a variable of type you static mesh component pointer.
‫Now whenever this static mesh component hits something in the world, we want to have some code that
‫responds to that.
‫Now being a static mesh component, it inherits things from the you primitive component class.
‫One of the things it inherits is a variable called on component hit.
‫Now here's the documentation for on component hit.
‫It's described as an event called When a Component Hits or is hit by something solid.
‫Now scrolling down, we see that on component hit has the type F component hit signature.
‫And if we click on this, we see that F component hit signature is a struct.
‫Now going back, we can see that the documentation says an event called when a component hits or is
‫hit by something solid.
‫This could happen due to things like character movement using set location with sweep enabled or physics
‫simulation for events when objects overlap such as walking into a trigger, see the overlap event.
‫So there are hit events and there are overlap events.
‫We're concerned with hit events.
‫So we know that on component hit is described in the documentation as a hit event and we know that on
‫component hit is a struct of type F component hit signature.
‫So far so good.
‫Now this hit event on component hit is also something we refer to as a multicast delegate.
‫A multicast delegate can have multiple functions bound to it.
‫There can be functions that exist on multiple classes.
‫And if those functions get bound to this multicast delegate, then they're added to something called
‫an invocation list.
‫This is a list of functions to be called when this hit event tells them all to be called.
‫We say that this hit event or multicast delegate broadcasts when something happens.
‫So in the event that our projectile mesh hits something, then the delegate will broadcast to all classes
‫that have a function bound to this delegate.
‫As soon as that broadcast is sent, then all of these bound functions are called in response to the
‫broadcast.
‫This is a very powerful feature of Unreal Engine.
‫It allows us to have this a thing called a delegate, which waits for an event such as a collision.
‫And then.
‫And the delegate will be broadcast, resulting in all bound functions called in response to the broadcast.
‫Since multiple functions can be bound to this delicate, we call it multicast.
‫It's broadcasting to multiple classes that have their functions bound to it.
‫Now, how do we bind a function to this delegate?
‫Well, on component hit has a number of functions that allow us to do this, one of which is called
‫add dynamic.
‫So when we call the ADD dynamic function, all we need to do is pass in two parameters the user object
‫which we can use this for, and the callback function, which is the address of some function on our
‫class.
‫So once we access this on component hit delegate and call the add dynamic function, we can pass in
‫the user object and the callback.
‫And this will result in adding that callback to the hit events invocation list.
‫Now on component hit, we'll broadcast this delegate resulting in any functions bound to it to be called.
‫So in order to bind to this delegate, we need to create a callback function.
‫So here's projectile and we'd like a callback function for the hit event.
‫So let's create a private function here and it's going to be void.
‫And I'm going to just simply call this on hit.
‫We could call this anything we want, but on seems descriptive enough.
‫Now this callback function has to have the correct signature for hit events, which means its input
‫parameter list needs to be very specific.
‫The first input parameter has to be of type you primitive component pointer and it's called hit comp.
‫This is the component doing the hitting.
‫In our case, we're going to have our projectile mesh hitting things.
‫The next parameter is an a actor pointer called Other Actor.
‫This is going to be the actor that got hit.
‫The next input parameter is another you primitive component pointer called other comp.
‫This is the other component that was hit.
‫So if we hit the static mesh component of some other actor, then this would be that static mesh component.
‫Next is an F vector called normal impulse.
‫If we're simulating physics, the physics engine will apply an impulse in response to the collision.
‫And this f vector represents the direction and the magnitude of that impulse.
‫And finally, we have an input parameter of type const F hit result and this is a const reference and
‫it's called hit.
‫So when on hit gets called, we'll have access to a hit result that contains even more information about
‫the hit.
‫Now, since we're binding this to a delegate, it has to be a U function.
‫So we're going to mark this with you function.
‫Now, you should be familiar with you function.
‫It's just like you property only it's for functions rather than variables.
‫So very important.
‫Our callback has to be a u function for this to work.
‫Now we've declared the function.
‫Let's go ahead and define the function.
‫So back in projectiles.
‫CP Just under our tick function, let's define this function.
‫We'll give it a function body and put the class name just before the function name, and now we have
‫a callback function.
‫So we're ready to bind this on hit function to our delegate and we need to do this in begin play.
‫Doing it in the constructor is to early and sometimes you can run into issues like your delegate not
‫being bound.
‫So we're going to do it here and begin play.
‫So we have our projectile mesh and we'll use the arrow operator to access the variable inherited from
‫you.
‫Primitive component called on component hit and on component hit has the function add dynamic.
‫Now strangely, autocomplete is not going to work for add dynamic, so you're going to have to type
‫the entire function name.
‫This is the function we'll use to bind our callback to on component hit and add that function to its
‫invocation list.
‫Now first, we need the user object that's going to be this because of this is the class where binding
‫the function for and next we need the address to the function that's going to be the address of on hit.
‫So we'll use the address of operator and the function a projectile on hit.
‫And with this we have our function bound and we're ready to generate hit events.
‫Now we can test this out with a UEE log and we can use log temp warning.
‫And for the text we can simply say on hit.
‫And if we see this in the output log, we know that our on hit function has been called let's compile
‫this.
‫So I have the output log open here.
‫I'm going to press play and fire a projectile and there's my log message on hit is being called.
‫So this is working.
‫Now just a side note, if we go into BP projectile and select projectile mesh in the details panel,
‫we can scroll down to the collision section.
‫And here we see that the collision presets is set to block all dynamic and that collision enabled is
‫set to this setting collision enabled query and physics.
‫This is necessary for hit events.
‫We need to be able to query for a hit event.
‫And of course, physics allows the physics engine to act on the projectile mesh.
‫So now that we've bound to the on component hit delegate with our on hit function and we've placed a
‫huge log to make sure that it's working, I'd like you to explore this function a little bit more so
‫that we can understand some of these input parameters in our on hit function.
‫So in the on hit function, add some more YOUI logs.
‫I'd like you to print the names of some of these input parameters using the git name function.
‫I'd like you to use a YUI log to print the name of hit comp, other actor and other comp so that we
‫could see the names of these objects we're hitting.
‫You're going to use the percent se format, specify the in the E log text macro.
‫And just as a reminder, whenever we call get name, we have to use the asterisk when passing that string
‫into the YUI log macro, for example, to get the name of other actor we would call other actor, get
‫name and place that asterisk at the beginning just to return that C style string, which is what you
‫log expects when you format it with a string.
‫So pause the video and use Yui log in our on hit function to print the names of hit comp, other actor
‫and other comp now.
‫Okay.
‫So here in projectile CP, I'm going to use you log here.
‫So I'm going to copy this line where I'm using Yui log and for the string I'm going to say hit comp
‫percent s and just after the text I'm going to call hit comp get name and I need this asterisk to convert
‫from an F string to a C style string.
‫Now I'm going to use this same line, only I'm going to change the text to say other actor and we're
‫calling it name this time on other actor.
‫And finally, I'm going to print other comp calling, get name on other comp.
‫So let's compile and back here in the editor, I'm going to go ahead and hit play and shoot a projectile
‫and check this out.
‫We see that hit comp is projectile mesh.
‫Remember we said that this is the component doing the hitting, which is our projectile mash and you'll
‫see that it's using the name that we assigned to this component in create default sub object.
‫So this is one of those situations where that name is being used.
‫Now you'll see that it says other actor flaw underscore zero.
‫And that's because this static mesh actor that is the flaw has the name flaw underscore zero.
‫Sometimes the engine will place a number at the end of its internal name for actors and components,
‫and you'll see that we have that same thing happening when we print out other comp.
‫The flaw static mesh actor has a static mesh component and its name is static mesh component zero.
‫So this gives us a little bit more information about those input parameters in our on hit callback function.
‫And this is useful because we have access to those objects in C++ if we'd like to do something in response
‫to the hit event.
‫So in summary, we have learned about hit events and how these are implemented as multicast delegates
‫and we learned how those work.
‫We created a function called On Hit and Bound, that function to the on component hit delegate that
‫our projectile mesh has inherited from the you primitive component class.
‫Now we haven't destroyed the projectile on hit, but we now know that we can easily do so because we
‫have this callback function and we can place logic in this function such as destroying the projectile.
‫So we'll handle that in a future video.
‫For now, pat yourself on the back because this is one of the more complex areas of Unreal Engine C++
‫that you've learned so far.
‫I'll see you in the next video.