SNew for constructor with arguments?

The following code works:

m_spMainMenu = SNew(MainMenu);

How would I call that, if the constructor of MainMenu had an argument.

For example:

MainMenu(int nExample)

You need to declere Slate arguments inside class decleration, here example from STooltip

class SLATE_API SToolTip
	: public SCompoundWidget
	, public IToolTip
{
public:

	SLATE_BEGIN_ARGS( SToolTip )
		: _Text()
		, _Content()
		, _Font(FCoreStyle::Get().GetFontStyle("ToolTip.Font"))
		, _ColorAndOpacity( FSlateColor::UseForeground())
		, _TextMargin(FMargin(8.0f))
		, _BorderImage(FCoreStyle::Get().GetBrush("ToolTip.Background"))
		, _IsInteractive(false)
	{ }

		/** The text displayed in this tool tip */
		SLATE_ATTRIBUTE(FText, Text)

		/** Arbitrary content to be displayed in the tool tip; overrides any text that may be set. */
		SLATE_DEFAULT_SLOT(FArguments, Content)

		/** The font to use for this tool tip */
		SLATE_ATTRIBUTE(FSlateFontInfo, Font)
		
		/** Font color and opacity */
		SLATE_ATTRIBUTE(FSlateColor, ColorAndOpacity)
		
		/** Margin between the tool tip border and the text content */
		SLATE_ATTRIBUTE(FMargin, TextMargin)

		/** The background/border image to display */
		SLATE_ATTRIBUTE(const FSlateBrush*, BorderImage)

		/** Whether the tooltip should be considered interactive */
		SLATE_ATTRIBUTE(bool, IsInteractive)

	SLATE_END_ARGS()

And in widget constructor you need to take those argument and apply them to proper varables, here another example from STooltip:

void SToolTip::Construct( const FArguments& InArgs )
{
	TextContent = InArgs._Text;
	bIsInteractive = InArgs._IsInteractive;
	Font = InArgs._Font;
	ColorAndOpacity = InArgs._ColorAndOpacity;
	TextMargin = InArgs._TextMargin;
	BorderImage = InArgs._BorderImage;
	
	SetContentWidget(InArgs._Content.Widget);
}

Note that all you arguments will have _ at beginning of the name and you do the same in deceleration of argument with defaults.
Also note that those varables in case of SLATE_ATTRIBUTE need to be TAttribute so in case of “Text” argument it need to be TAttribute<FText> TextContent. Attribute is a special type which allows you to bind functions and varable pointers, assuming you already used Slate i bet you used those bindings already, there also SLATE_ARGUMENT which is non-attribute argument and you can use normal types with it without TAttribute, but then ofcorse you lose ability to bind functions

Once you do those arguments, you use them same way as in other widgets like from example SNew(SToolTip).Text(“blabla”).IsInteractive(true) and so on.

Pro Tip for the future, don’t be shy to look in engine source code and see how other classes work, engine source code is best source of examples, if you would look inside header of any Slate widget class i bet you would figure yourself what to do.

1 Like

Thank you! This is very helpful.

It’s late here, perhaps that is the reason I am not able to change a class to use this functionality without the base class having the same functionality.

I’ll stay with method calls for now. :frowning: