x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to pass self-reference to UButton's OnClicked delegate (UMG)?

I've tried to create a custom OnClicked delegate for custom Button (inherited from UButton), that passes self-reference, but it is not exectued when I click a button.

Header:

 DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnButtonClickedRefDelegate, UExtendedButton*, ButtonRef);
     UCLASS()
     class DQS_API UExtendedButton : public UButton
     {
         GENERATED_UCLASS_BODY()
         
     public:
         UExtendedButton();
     
         /** Should be called when the button is clicked, also gives the reference to this... */
         UPROPERTY(BlueprintAssignable, Category = "Button|Event")
         FOnButtonClickedRefDelegate OnClickedReferenced;
     
         UFUNCTION()
         void OnSelfClicked();
     
         UFUNCTION()
         void DoSomething(UExtendedButton* ButtonRef);
     
     protected:
         /** Handle the actual click event from slate and forward it on */
         FReply SlateHandleClicked();
     
     };



Source:

 UExtendedButton::UExtendedButton(const FObjectInitializer& ObjectInitializer)
     : Super(ObjectInitializer)
 {
     OnClickedReferenced.AddDynamic(this, &UExtendedButton::DoSomething);
     OnClicked.AddDynamic(this, &UExtendedButton::OnSelfClicked);
 
     
 }
 
 void UExtendedButton::OnSelfClicked()
 {
     if (GEngine)
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, 
             FString("Hello :: ").Append(this->ButtonText->GetText().ToString())
         );
 
     OnClickedReferenced.Broadcast(this);
 }
 void UExtendedButton::DoSomething(UExtendedButton* ButtonRef)
 {
     OnClickedReferenced.Broadcast(ButtonRef);
 
     if (GEngine)
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red,
             FString("Do :: ").Append(ButtonRef->ButtonText->GetText().ToString())
         );
 }
 
 /* Cannot be overwritten since the original one isn't virtual... */
 FReply UExtendedButton::SlateHandleClicked()
 {
     OnClickedReferenced.Broadcast(this);
 
     return FReply::Handled();
 }


I couldn't set things right as in here, since I didn't expect to touch Slate at all, only UMG. What I want to achieve is this BP working: alt text

Product Version: UE 4.16
Tags:
more ▼

asked Jun 16 '17 at 01:41 PM in C++ Programming

avatar image

WHSolv
36 3 8 9

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Solved it. In UButton we have RebuildWidget(), which we should override for custom OnClicked. We are interested in MyButton = SNew(SButton) .OnClicked(BIND_UOBJECT_DELEGATE(FOnClicked, SlateHandleClicked))... , in which SlateHandleClicked should be OnClickedReferenced.Broadcast(this) instead of original one OnClicked.Broadcast(). To do it, I suppose, we need to have all the SlateHandle-functions (Clicked, Pressed, Hovered, etc.) implemented once more, or just to rewrite engine's functions to be virtual.

The complete code for RebuildWidget(), ( SlateHandleClicked() stays the same as in the question post):

 TSharedRef<SWidget> UExtendedButton::RebuildWidget()
 {
     MyButton = SNew(SButton)
         .OnClicked(BIND_UOBJECT_DELEGATE(FOnClicked, SlateHandleClicked))
         .OnPressed(BIND_UOBJECT_DELEGATE(FSimpleDelegate, SlateHandlePressed))
         .OnReleased(BIND_UOBJECT_DELEGATE(FSimpleDelegate, SlateHandleReleased))
         .OnHovered_UObject(this, &UExtendedButton::SlateHandleHovered) /* Be careful here... */
         .OnUnhovered_UObject(this, &UExtendedButton::SlateHandleUnhovered) /* And here... */
         .ButtonStyle(&WidgetStyle)
         .ClickMethod(ClickMethod)
         .TouchMethod(TouchMethod)
         .IsFocusable(IsFocusable)
         ;
 
     if (GetChildrenCount() > 0)
     {
         Cast<UButtonSlot>(GetContentSlot())->BuildSlot(MyButton.ToSharedRef());
     }
 
     return MyButton.ToSharedRef();
 }



more ▼

answered Jun 16 '17 at 05:34 PM

avatar image

WHSolv
36 3 8 9

avatar image Muzaheed Aug 01 '17 at 04:40 AM

i have been using your code snippet and did implement it in my project. but no child umg under this specific Umg class is not getting rendered. as u can see in this video: https://youtu.be/dTadlQX8I-o

any idea?

nad thanks for the code snippets

avatar image WHSolv Aug 01 '17 at 07:02 AM

You need to add initialisation in constructor UExtendedButton::UExtendedButton like ButtonText = CreateDefaultSubobject(_T("ButtonText"));.

avatar image Muzaheed Aug 01 '17 at 10:41 AM

not sure where this ButtonText coming from . but anyway thanks.

avatar image WHSolv Aug 01 '17 at 12:32 PM

ButtonText is a UTextBlock variable in the header of UExtendedButton, if you want it to be there. If you have added TextBlock in UMG-editor, then it should be there by default. Double check Appearence and Behavior categories, e.g., it could be white text on white background or text could have 0 alpha.

avatar image Muzaheed Aug 27 '17 at 06:11 AM

it turned out to be a issue caused by RebuildWidget(). now fixed

avatar image WHSolv Aug 27 '17 at 08:28 AM

What do you mean?

avatar image AlexWrj Oct 20 '18 at 01:44 PM

Hello! I've tride this method, but in runtime I catch an error, if I use RebuildWidget function. I've implemented all methods such as onclicked, onhoverd etc. What can be wrong here?

avatar image WHSolv Jan 13 '19 at 10:43 AM

It would be easier if I can replicate the particular issue. Can you share the code? If it's still relevant, though.

avatar image AlexWrj Jan 13 '19 at 02:31 PM

Thank you! I've already done with it. Just needed to implement all virtual methods including onhover etc. I ignored them first because I didn't need them. And when I implemented them, the errors disappeared.

avatar image WHSolv Jan 13 '19 at 02:35 PM

Glad to hear that. Have a nice day then.

avatar image AlexWrj Jan 14 '19 at 07:19 AM

Thanks, and you too, man!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question