How to create a menu bar using FMenuBarBuilder?

I am trying to create a menu bar using FMenuBarBuilder.

I only could find the following piece of codes at Slate UI Widget Examples for Unreal Engine | Unreal Engine 5.1 Documentation,
but it is incorrect.

FUICommandList CommandList;
FMenuBarBuilder MenuBarBuilder(CommandList);
{
MenuBarBuilder.AddPullDownMenu(TEXT(“Menu 1”), TEXT(“Opens Menu 1”), FNewMenuDelegate::CreateRaw(&FMenus::FillMenu1Entries));

MenuBarBuilder.AddPullDownMenu(TEXT("Menu 2"), TEXT("Opens Menu 2"), FNewMenuDelegate::CreateRaw(&FMenus::FillMenu2Entries));

}
MenuBarBuilder.MakeWidget();

I’d appreciate it if anyone’d provide me a piece of sample codes creating a menu bar via FMenuBarBuilder.

Thank you in advance.

It’s not incorrect simply those example shows how to declare widgets but it does not show where oyu can use them as there many places. Most common way is use FMenuBarBuilder is menu extenders, which allows to add menu entries to existing sets, here some exmple i find:

here alos video with over view of extending editor, in mention extenders and how to find them:

I am interested in this also. I am trying to make a tool bar in a game and can’t use editor extenders since I am not using the unreal editor to display the game.

I am trying to make a menu bar outside of the editor for a game, and I have a solution for that. I did some more work and found an answer. Not a good answer, but an answer.

To start, good code practice say to either make your own class or a method to return a TSharedRef so you can put it in declarative slate code, like so:

ChildSlot
		[
			SNew(SBorder)
		[
			SNew(SOverlay)
			+ SOverlay::Slot()
			[
				SNew(SVerticalBox)
				+ SVerticalBox::Slot()
				[
					SMenu::MakeMenuBar()
				]	
			]
		]
		];

Then you make the bar and pass it back in some manner.

All the following code is in a function called MakeMenuBar

We need to set a command list and an extender. In our case, we aren’t actually extending a menu since this is not a module. But you can put an actual extender here.
your command list doesn’t need to have anything it it, but I think I have seen people use a command list to like up the FNewMenuDelegates or function that fire after the buttons are pressed. In any case, this works.

FUICommandList FUICL = FUICommandList();
	TSharedPtr<FUICommandList> TSPFUIC;
	TSharedPtr<FExtender> TSPFE;
	FMenuBarBuilder FMBB = FMenuBarBuilder(TSPFUIC,TSPFE,&FCoreStyle::Get(),FName("none"));

Then add the appropriate dropdowns or any other items you need.

FNewMenuDelegate FileDropDown = FNewMenuDelegate::CreateRaw(this, &SMenu::createFileDropDown);
	FMBB.AddPullDownMenu(FText::FromString("File"), FText::FromString("File actions are done here"),FileDropDown);

	FNewMenuDelegate EditDropDown = FNewMenuDelegate::CreateRaw(this, &SMenu::createEditDropDown);
	FMBB.AddPullDownMenu(FText::FromString("Edit"), FText::FromString("Edit actions on the currently shown data"), EditDropDown);

	FNewMenuDelegate ViewDropDown = FNewMenuDelegate::CreateRaw(this, &SMenu::createViewDropDown);
	FMBB.AddPullDownMenu(FText::FromString("View"), FText::FromString("Change View info"), ViewDropDown);

Then return the TSharedRef

return FMBB.MakeWidget();

Pros of this method:

  • I posted a response to this question.
  • It works

Cons of this method:
-I wrote the thing and I’m not sure how it all works.
-FUICommandlist should do something.
-This probably should be it’s own class, or somehow better organized.
-We can only statically call functions, I don’t know how to call functions with arguments.

If you want to complain about this answer and you haven’t posted your own answer, please see Pro #1