In my code, it looks like I am able to add a delegate and the OnTextCommitted event runs (because I can see it in the Blueprint) but the delegate never gets called. As a test, I’ve added a separate call via blueprint to a similar method and that works but I don’t want to do it that way.
In case it’s relevant, my EditableText control is on a canvas with a few other controls. I have reparented the blueprint to my baseclass.
Here’s the baseclass:-
Header
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "MyBaseUserWidget.generated.h"
UCLASS()
class UE4FAIRYTALEV3_API UMyBaseUserWidget : public UUserWidget
{
GENERATED_BODY()
public:
UPROPERTY(meta = (BindWidget))
class UMultiLineEditableText* MultiLineEditableText_OUT;
UPROPERTY(meta = (BindWidget))
class UEditableText* EditableText_IN;
FText ft_TheStorySofar;
int testNumber = 0;
bool haveAddedEditableText_IN_Delegate = false; // TEMP bad way to add the delegate after killing the editor doing it other ways.
UMyBaseUserWidget(const FObjectInitializer& ObjectInitializer);
void AddDelegates();
// Do it via a C++ delegate
void DelegateCommitInputText(const FText& InText, ETextCommit::Type InCommitType);
// For test because I have not yet got the commit multi-delegate to call my function DelegateCommitInputText()
UFUNCTION(BlueprintCallable, Category = "fairyTale")
void BlueprintCommitInputText(const FText& InText);
// Optionally override the Blueprint "Event Construct" event
virtual void NativeConstruct() override;
// Optionally override the tick event
virtual void NativeTick(const FGeometry& MyGeometry, float InDeltaTime) override;
};
Body
#include "MyBaseUserWidget.h"
#include "Components/MultiLineEditableText.h"
#include "Components/EditableText.h"
UMyBaseUserWidget::UMyBaseUserWidget(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
}
void UMyBaseUserWidget::AddDelegates()
{
EditableText_IN->OnTextCommitted.RemoveDynamic(this, &UMyBaseUserWidget::DelegateCommitInputText);
EditableText_IN->OnTextCommitted.AddDynamic(this, &UMyBaseUserWidget::DelegateCommitInputText);
// Crude way to see if we past this way
testNumber = 20000;
}
void UMyBaseUserWidget::DelegateCommitInputText(const FText& InText, ETextCommit::Type InCommitType)
{
if (InCommitType == ETextCommit::OnEnter)
{
// Crude way to see if we also past this way
testNumber >>= 1;
ft_TheStorySofar = FText::FromString(FString::Printf(TEXT("%s
%s %s"), *(ft_TheStorySofar.ToString()), *(FString::FromInt(testNumber)), *(InText.ToString())));
FText tempText = FText::FromString(FString::Printf(TEXT("Via DELEGATE
%s"), *(ft_TheStorySofar.ToString())));
MultiLineEditableText_OUT->SetText(tempText);
}
}
void UMyBaseUserWidget::BlueprintCommitInputText(const FText& InText)
{
++testNumber; // <-- TEMP for test
ft_TheStorySofar = FText::FromString(FString::Printf(TEXT("%s
%s %s"), *(ft_TheStorySofar.ToString()), *(FString::FromInt(testNumber)), *(InText.ToString())));
FText tempText = FText::FromString(FString::Printf(TEXT("Via BLUEPRINT
%s"), *(ft_TheStorySofar.ToString())));
MultiLineEditableText_OUT->SetText(tempText);
}
void UMyBaseUserWidget::NativeConstruct()
{
// Do some custom setup
// Call the Blueprint "Event Construct" node
Super::NativeConstruct();
}
void UMyBaseUserWidget::NativeTick(const FGeometry& MyGeometry, float InDeltaTime)
{
// Make sure to call the base class's NativeTick function
Super::NativeTick(MyGeometry, InDeltaTime);
// Do your custom tick stuff here
if (haveAddedEditableText_IN_Delegate == false) // TEMP bad way to add the delegate
{
EditableText_IN->SetText(FText::FromString("Type stuff here"));
testNumber = 10000; // Test
AddDelegates();
haveAddedEditableText_IN_Delegate = true;
}
}
I currently add the delegate via AddDelegates() call using a crude one shot method in NativeTick(). I have also tried waiting for 100 ticks but it does nothing different (other than the 100 frame delay).
When I run it, I type something into the edit control and press Enter, the Blueprint section runs and I can see that it has previously been through the AddDelegates() call. For example, when I run it, I get something like:
The **20001 **is the value of **testNumber **which is set to **20000 **in AddDelegates() then incremented by 1 in BlueprintCommitInputText(). A crude but simple test to give me an indication that I have registered the delegate and that the event does fire.
So why can I not get the delegate to call the C++ DelegateCommitInputText() method?