I spent a few hours digging through the Engine code and pausing the video at key frames and finally got something working. I already have a game plugin for doing some custom stuff so I will make reference to that in a few places. 's a high level overview of what I had to do:
- Create a new TCommands class
- Add a TSharedPtr<FUICommandList> member to my plugin module
- Write a bunch of boilerplate code to set up the delegate so the button calls the function
- Hook up the extender to add the button
- And of course, the function that I wanted to call
- Also, had to add dependencies to the build script for my plugin
's some details…I am copying from Visual Studio so let me know if I make some mistake:
–Create TCommands class–
class FNCommands : public TCommands<FNCommands>
{
public:
FNCommands()
: TCommands<FNCommands>(
TEXT("FNCommands"),
NSLOCTEXT("Context","FNCommands", "FN Commands"),
NAME_None,
FEditorStyle::GetStyleSetName()
)
{
}
// TCommand<> interface
void RegisterCommands()
{
UI_COMMAND(TestCommand, "Test Command", "Test Command", EUserInterfaceActionType::Button, FInputGesture());
}
// End of TCommand<> interface
public:
TSharedPtr<FUICommandInfo> TestCommand;
};
–Add a FUICommandList property to the game plugin–
class FNPlugin : public IModuleInterface
{
// other stuff
public:
void TestFunction();
private:
TSharedPtr<FUICommandList> CommandList;
}
–Boilerplatecode added in StartupModule to hook up the extender–
void FNPlugin::StartupModule()
{
FNCommands::Register();
CommandList = MakeShareable(new FUICommandList);
CommandList->MapAction(
FNCommands::Get().TestCommand,
FExecuteAction::CreateRaw(this, &FNPlugin::TestFunction));
struct Local
{
static void AddToolbarCommands(FToolBarBuilder& ToolbarBuilder)
{
ToolbarBuilder.AddToolBarButton(FNCommands::Get().TestCommand);
}
};
FLevelEditorModule& LevelEditorModule = FModuleManager::LoadModuleChecked<FLevelEditorModule>("LevelEditor");
TSharedRef<FExtender> ToolbarExtender(new FExtender());
ToolbarExtender->AddToolBarExtension(
"Game",
EExtensionHook::After,
CommandList,
FToolBarExtensionDelegate::CreateStatic(&Local::AddToolbarCommands));
LevelEditorModule.GetToolBarExtensibilityManager()->AddExtender(ToolbarExtender);
}
–The function you want to call–
void FNPlugin::TestFunction()
{
UE_LOG( LogTemp, Log, TEXT("Button pressed") );
}
–Add dependencies–
PublicDependencyModuleNames.AddRange(
new string]
{
"Core",
"CoreUObject",
"Slate",
"UnrealEd",
"EditorStyle",
// ... add other public dependencies that you statically link with ...
}
);