With deprecation of InitializeInputComponent how is it desired to create input components if we want to handle inputs only on the c++ side?
Our blueprint widget is empty and has no input nodes inside it’s graph, therefore “bAutomaticallyRegisterInputOnConstruction” will be false, which results in InputComponent not being created.
Now I know I can, for example, override something like “CreateInputComponent” and manually make NewObject for InputComponent and use that, which is exactly what “InitializeInputComponent” is doing.
How should we approach this without adding “placeholder” nodes in blueprint graphs or reimplementing initalizeInputComponent ourselfs for every widget?
Thanks for the opportunity to ask question!
[Attachment Removed]
Hi there,
there are a couple of ways you could tackle this. As you have said you could basically re-implement InitializeInputComponent in your own way, However I think a better method would be Setting bAutomaticallyRegisterInputOnConstruction manually & letting the widgets Initialize function handle the creation of the input component. This is the most straightforward way of achieving this while also adhering to the new lifecycle of the input component. There are a couple of easier ways to achieve this:
- If you have your own UUserWidget sub-class you can manually set bAutomaticallyRegisterInputOnConstruction to true in the constructor. I tested this in a couple of scenarios and it seems to be effective.
- 2. Since bAutomaticallyRegisterInputOnConstruction is being retrieved from the CDO in CreateInputComponent you could override UMyUserWidget::PostCDOCompiled and set the value there, e.g:
// .h
virtual void PostCDOCompiled(const FPostCDOCompiledContext& Context) override;
// .cpp
void UMyUserWidget::PostCDOCompiled(const FPostCDOCompiledContext& Context)
{
Super::PostCDOCompiled(Context);
bAutomaticallyRegisterInputOnConstruction = true;
}
This implementation should more or less replicate the functionality of the K2 Input Nodes when placed inside the event graph. For example as seen in UK2Node_EnhancedInputAction::ExpandNode:
// Widget blueprints require the bAutomaticallyRegisterInputOnConstruction to be set to true in order to receive callbacks
if (GetBlueprint()->IsA<UWidgetBlueprint>())
{
CompilerContext.AddPostCDOCompiledStep([](const UObject::FPostCDOCompiledContext& Context, UObject* NewCDO)
{
UUserWidget* Widget = CastChecked<UUserWidget>(NewCDO);
Widget->bAutomaticallyRegisterInputOnConstruction = true;
});
}
I hope one of those scenarios will be applicable to your situation.
Cheers,
Louis
[Attachment Removed]
Hey Louis, thanks for the reply, I did try to place it on construction but it didn’t work for me because it is then overriden by
DefaultWidget->bAutomaticallyRegisterInputOnConstruction = false;in
FWidgetBlueprintCompilerContext::CopyTermDefaultsToDefaultObject.
I didn’t think of UMyUserWidget::PostCDOCompiled so that is one way of doing it.
I ended up just making lib function that creates input component like the InitializeInputComponent does. and just call that instead of one from UUserWidget.
I think some kind of second variable that is also exposed in editor would do the job.
If either there are nodes in editor or user has set this second varitable on, it would push input component, otherwise don’t.
Anyway, we are using common UI, so majority of inputs are handled through it, we only still use this method because of hold inputs, etc,
[Attachment Removed]
Hi again,
Glad to hear you found a solution that works.
I will close this case for now, But if you require any further information on the issue, feel free to update the case.
Cheers,
Louis
[Attachment Removed]