In this lecture, you'll learn about single delegates in Unreal. For our purposes, single delegates are analogous to the higher-order functions we learned about in the previous lecture, where we pass in the address of the callback function and the higher-order function can callback that function at some other time. The terminology and the way we do it is slightly different in Unreal, but the bigger idea is the same. In the interests of full disclosure our single delegates are actually more like what are called the function objects or funk tours in C++. But the way we're using them, you can just think of them like the higher-order functions we've already learned about. Before we look at the code for our example, I wanted to show you here in the Unreal Engine documentation, if you search on delegates, you'll get to the documentation that explains how to do the things that we're going to do today. The big idea is that the delegate is essentially a higher-order function that we can bind a pointer to, a callback function to, and when we decided to execute the delegate, which is essentially executing the higher-order function, it will call the callback function that has been bound to that delegate. In this lecture, we're going to explore single delegates, and in the next lecture we'll explore multicast delegate including event. We have some options when we declare a delegate. We can declare delegates for functions that return a value, we can declare delegates for functions that are const, we can have up to four payload variables which we're not going to deal with in the examples that I provide, and we can have up to eight function parameters for that pointer to a function that we're going to bind to the delegate. I will let you explore the rest of this documentation on your own so we can just get to our code example. First, I'll show you how the code works. I have a talker actor and a listener actor. If I run the code, the talker actor every three seconds sends a message with a number in it, and these log messages you're seeing appear are the listener receiving that message with the appropriate number in it. The listener has provided a callback function to our talker delegate, and when our talker delegate is executed, it calls that function back. We'll look at the talker actor first. Here's how we declare a delegate. We use the macro declared delegate. I happen to be declaring a delegate that has one parameter, so I include underscore one param in my macro name. Over here on the right I provide two things. I provide the name of my new delegate. This is defining a type. I'm defining a type called F message delegate, and I say, what is the type of that one parameter for the delegate? I happen to be passing enough string when I execute this higher-order function to call whatever callback function has been bound to this delegate. I'm keeping track of the message number so that I can increase that each time I send a message, I have a timer that goes off every three seconds to execute my delegate. This is the function that will actually execute that delegate, and this variable right here is my field or a member variable that holds the actual delegate. This is the delegate that will bind a callback function to and execute when we decide we want to execute it. We have the constructor beginning play intake as usual. In my implementation file, I'm just starting the talker timer on Begin Play and that does the usual starting a timer thing. Here in my talk function I'm taking that message delegate object, which remember is essentially a higher-order function, and I'm telling it to execute if bound. Then I provide that single f string argument that my delegate type requires and that my callback function is expecting me to provide when I execute the callback function. There's actually an execute function that I can call instead of execute if bound, but that's a bad idea. We should only execute message delegate if in fact there has been a call function bound to the message delegate. We'll use execute if bound when we're executing single delegates that we're using in our code. This is the talking side. This is the message delegate calling a callback function with this particular argument. In my listener actor, I'm going to keep a reference to the talker actor and I'm going to add two additional functions beyond the usual functions. This receive message function is my callback function and as you can see it's got a return type of void, and it expects one f string argument, so this matches the required function signature for me to bind this function to my delegate. I'm overriding the end play function so that when this listener actor gets removed from the level or the map, it will unbind itself from the delegate. We don't want to remove this and have my delegate still holding on to a reference to it in memory that's like our dangling pointer problem that we've talked about previously. We want to unbind our receive message function from that delegate when we get removed from the level. Finally, in the implementation for our listener actor, on begin play, we get a pointer to the talker actor object that's in the map and save it right here if we find it. Then on that pointer to a talker object, we access the message delegate field that we declared to be public in my talker actor, and we call the bind you object function on that delegate. There are a number of different ways we can bind a function to a delegate and you can read the documentation to see that variety of ways we can do that, but I'm going to use the bind you object function to bind this listener object with this address of my received message function to that delegate. This is me providing the callback function to that delegate. When I receive a message, I just print out the message I received. Then finally on end play, first I check to make sure I have a pointer to that talker actor object before I try to access a field in the object I think I'm pointing to. As long as this isn't a null pointer, I access the message delegate field for that talker actor object and then I unbind myself from that delegate. If this idea of binding of function to a delegate looks eerily familiar to you, you should realize that we've been doing this ever since we started handling Unreal input. As a reminder, we've called the bind to access function on an input component to bind a particular function. In this case, the address of the fish player controller's move horizontally function to a particular axis, and we've called the bind action function on the input component to bind a particular function. In this case, the fish player controller shoot fish function to a particular action. We've actually been providing addresses of functions to bind them ever since we started processing Unreal input. To recap, in this lecture you learned how to use a single delegate and Unreal, and you also saw that the bigger ideas are analogous to our higher-order function and callback functions in pure C++.