Download

Passing a method to a Slate button's OnClicked()

I’m trying to bind a method (FReply SwitchOnClick()) to a Slate button’s OnClicked. Following this thread, I arrived at the following code:


SNew(SButton)
.OnClicked(this, &UForestObject::SwitchOnClick)

(UForestObject extends UWidget)

The resulting compiler error mentioned UForestObject not having a member ‘AsShared’, so I followed this thread and added another parent to it.


class FORESTOFTERROR_API UForestObject : public UWidget, public TSharedFromThis<UForestObject>

It’ll compile, but now I’m getting an editor crash with the following first few lines:


Assertion failed: SharedThis.Get() == this [File:d:\epic games\ue_4.15\engine\source\runtime\core\public\Templates/SharedPointer.h] [Line: 1217] 
UE4Editor_Core!FDebug::AssertFailed() [d:\build\++ue4+release-4.15+compile\sync\engine\source\runtime\core\private\misc\assertionmacros.cpp:349]
UE4Editor_ForestOfTerror_217!TSharedFromThis<UForestObject,0>::AsShared() [d:\epic games\ue_4.15\engine\source\runtime\core\public	emplates\sharedpointer.h:1221]

What’s the correct way to pass my method to this SButton’s OnClicked?

Of course, found it in UButton:


MyButton = SNew(SButton)
.OnClicked(BIND_UOBJECT_DELEGATE(FOnClicked, SlateHandleClicked))

Thought I had already checked it, my bad. :slight_smile: