Attaching function calls to Slate events

While creating buttons is all well and fine, at the moment, any UI element you add to the player's screen just sits there without anything happening even if a user clicks on it. We don't have any event handlers attached to the Slate elements at the moment, so events such as mouse clicks don't actually cause anything to happen.

Getting ready

This recipe shows you how to attach functions to these events so that we can run custom code when they occur.

How to do it...

  1. Create a new GameMode subclass called AClickEventGameMode.
  2. Add the following private members to the class:
    private:
    TSharedPtr<SVerticalBox> Widget;
    TSharedPtr<STextBlock> ButtonLabel;
  3. Add the following public functions, noting the override for BeginPlay() ...

Get Unreal Engine 4 Scripting with C++ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.