Hey again! ❤️
Welcome to the final part of covering Notifications in Unreal Engine. In this part, we will cover Notifications with delegates, or to put it simply a Notification with buttons.
Before we get started, as always. This tutorial works from the basis created in both parts one and two. You can find those here, follow along with them and then come back here if you haven’t done so already.
Notifications in Unreal Engine Part 1
Notifications in Unreal Engine Part 2
Setting up
This tutorial will perhaps be a little more complex than the others before, so I would say an understanding of shared/weak and pointers in general would be really useful to look into first.
So let’s start! As before, let’s go ahead and add to our SNotificationButtonsWidget header and cpp files and then I’ll break it down and describe what’s going on.
.h (We add everything under Part 3, be sure to add the NotificationPtr too!)
.cpp
A note before we continue, you’ll see in the CPP file I’ve removed the first two parts methods for the sake of brevity and making this gist easier to understand. So just be careful copy pasting if you are!
The first thing, just as before is in our Construct method, adding another SHorizontalBox slot with a button which links via the OnClicked() delegate to our FReply Method just the same as before. This should be some what intutive now.
We then define three methods, one FReply for the button – the other two for the Accept and Dismiss buttons.
A lot of the code here is already concepts we have covered in the first and second parts of this tutorial series - namely, FNotificationInfo struct and it’s capabilities. The new part here is ButtonDetails and creating a shared pointer to a notification so that we can access it later.
Pointers!
Let’s start at the beginning of SpawnChoiceNotification(), the first thing that we do is take our pointer that we defined in our header and retrieve a shared pointer to it. We’ve defined this as a weak pointer initially because the notification itself may not exist anymore, we need to validate it.
The .Pin() method converts a weak pointer to a shared pointer if it hasn’t expired. We then validate it on Line 109 before moving on. Otherwise, we leave the method with FReply::Handled();
Next is the usual creation of an FNotificationInfo so we can actually set up our notification.
FNotificationButtonInfo
Now for the fun part! ButtonDetails is an Array of FNotificationButtonInfo structs. This struct type takes in localized strings for the button name, the tooltip and a callback to another method that takes on the behavior of that button - in this case, it is our AcceptClicked() and DismissClicked() methods respectively for handling both those states.
Another requirement is line 100, FireAndForget. This parameter ensures that our notification remains on screen until one of the buttons have been pressed so the user has time to make that choice.
We do some more styling at line 102–108, as covered in previous tutorial parts, and then we move onto handling our notifications states.
We need to set the NotificationPtr variable we have set in our header to the result of our created Notification on line 116. This is so that we can access that same notification later, should we decide to change existing text on the notification or do something else.
Buttons
For both button callbacks, we will be ensuring first that we are able to .Pin() as we will be modifying them.
Within the IsValid() if statement for each of these, you can call and do whatever logic you need when a button is pressed.
For AcceptClicked() we first validate .Pin() and then call SetText() directly on our NotificationPin. If you need to change your notifications text at any point, this is how you do it - just ensure you pin first!
We then Set the completion state, just to inform Unreal and anything else listening how this notification has ended, either with success, failiure etc.
And then lastly, we can call ExpireAndFadeout() which does exactly as it says! It informs Unreal that the notification is done with, and begins a fade out animation. Parameters for these can be customized, these are ExpireDuration and FadeDuration, and are passed in to the FNotificationInfo when we create the notification.
Our DismissClicked() is nearly exactly the same thing - except we use a Completion State of CS_None to inform Unreal that we just canceled out of this notification.
Thank you!
Thank you for reading this far! And I hope that you learned a thing or two about notifications. I often find that these are over-looked in plugin development as they really can open an opportunity for User Experience and feedback from developers who will be working with your tools. They’re not too complicated!