Pressing a button with blueprints

Heya,

I know this has been asked multiple times, but there doesn’t seem to be a clear (or perhaps updated) answer. Simply put, I’d like to hover and press a UMG widget button through the use of blueprints, rather than having it be done by the mouse.

It seems that the answers I’ve found are overly-complicated for a task like this. Honestly, I might make a UE4 suggestion to add a ‘hover’ and ‘press’ toggle in blueprinting to make this whole thing a lot easier. Perhaps a ‘set state’ to switch between normal, hovered and pressed. Right now, it seems to be quite annoying to set up.

Thank you!

1 Like

What do you mean “rather than having it be done by the mouse”? Do you have your own controller or something?

@Hemlock - it’s about simulating pressing a button - all the bells and whistles associated with the action but without using the mouse, with states and all. As if you wanted to observe someone else operating the button (perhaps a tutorial sequence showing you what to click)

As far as I know (and I’ve seen it confirmed by Epic staff) you cannot invoke a button click. It’s simply not exposed to blueprint and called only when the mouse cursor interacts with the button.

Your best bet is to extend the existing button in slate and add the functionality you need yourself. I don’t think I’ve seen a workaround for this in BP.

Yeah. What I want to achieve is simply a simulation of a button press/hold/release on a button.

Necro. Is this still not exposed through BP? It would be nice to not only have the style, but also things like pressed sound etc … other option being strip that stuff from the button appearance where it currently lives nicely and make it all BP logic so it can be simulated per @Regentlord 's suggestion :confused:

Here is the solution I use to fix this issue. It requires a bit of C++, but it’s crazy simple.

  • create a new C++ that inherits from UBlueprintFunctionLibrary (any other class will do it also, just that that’s the cleanest way to make the functions accessible from anywhere);
  • add in your .Build.cs:
  • PublicDependencyModuleNames : UMG
  • PrivateDependencyModuleNames : UMG

in the header:

#include "Components/Button.h"
	UFUNCTION(BlueprintCallable, Category = "Basics | Utils", Meta = (WorldContext = "Context", ToolTip = "Triggers a 'OnClicked' event on a button"))
	static void TriggerButtonClick(UButton* Button);

	UFUNCTION(BlueprintCallable, Category = "Basics | UMG", Meta = (WorldContext = "Context", ToolTip = "Triggers a 'OnPressed' event on a button"))
	static void TriggerButtonPress(UButton* Button);

	UFUNCTION(BlueprintCallable, Category = "Basics | UMG", Meta = (WorldContext = "Context", ToolTip = "Triggers a 'OnReleased' event on a button"))
	static void TriggerButtonReleased(UButton* Button);

	UFUNCTION(BlueprintCallable, Category = "Basics | UMG", Meta = (WorldContext = "Context", ToolTip = "Triggers a 'OnHovered' event on a button"))
	static void TriggerButtonHovered(UButton* Button);

	UFUNCTION(BlueprintCallable, Category = "Basics | UMG", Meta = (WorldContext = "Context", ToolTip = "Triggers a 'OnUnhovered' event on a button"))
	static void TriggerButtonUnhovered(UButton* Button);

in your cpp:

void YourBlueprintFunctionLibrary::TriggerButtonClick(UButton* Button)
{
	if (!IsValid(Button))
	{
		return;
	}
	Button->OnClicked.Broadcast();
}

void YourBlueprintFunctionLibrary::TriggerButtonPress(UButton* Button)
{
	if (!IsValid(Button))
	{
		return;
	}
	Button->OnPressed.Broadcast();
}

void ULSCommonBlueprintFunctionLibrary::TriggerButtonReleased(UButton* Button)
{
	if (!IsValid(Button))
	{
		return;
	}
	Button->OnReleased.Broadcast();
}

void YourBlueprintFunctionLibrary::TriggerButtonHovered(UButton* Button)
{
	if (!IsValid(Button))
	{
		return;
	}
	Button->OnHovered.Broadcast();
}

void YourBlueprintFunctionLibrary::TriggerButtonUnhovered(UButton* Button)
{
	if (!IsValid(Button))
	{
		return;
	}
	Button->OnUnhovered.Broadcast();
}

Once compiled, you now be able to click on a button reference from Blueprint without having to use custom signals.

And voilà!

I hope it’ll help someone :slight_smile:

4 Likes