Call an actor's function when a UI button is pressed.

Hi all,

I have a UI button set up in my WidgetBlueprint, with a relative OnClicked event. I am creating and displaying the WidgetBlueprint from my GameMode blueprint. I want to call a function from an ActorBlueprint (non-player, non-pawn) whenever the OnClicked event for said button is fired, WITHOUT using Event Ticks.

I tried with casting and local variables for a few hours but I am getting nowhere, and it sure feels like it should not be this hard to pull off.

Any help is appreciated, thanks!

Hello,

There is a blueprint Node Called “Get Actor of Class” and also “Get All Actor Of Class”
image

You can use that node for finding an actor in the world that matching the Actor Class.

There is also another way to do this by registering the actor you want to move in the widget itself.

Flow Example:

  1. Create a variable in the widget to containing the actor (It can be an array)
  2. In the CustomActor (Your actor that you want to do action) Begin Play > Get Game Mode > Get the widget from game mode > And Set / Add the variable you created in the widget by referencing Self (This actor itself).
  3. Later in the OnClick event > Check the variable that already set in the step 2 from the CustomActor class, is it valid or not, if its valid, you can move the actor or do some behavior there.

I hope I can give you some idea!

Thanks! Getting the actor in the widget and calling an event directly from there works for now, as I have only 1 actor of that class, but I would rather have an event called from the widget so that multiple actors can subscribe to it.

Similar to the other way you mentioned, I was trying get the widget from the actor after casting to the game mode. The cast to GameMode works fine and the resulting GM_Level variable is Valid, while the Arena UI, which I am setting in the game mode is not. Any idea why that may be?

If you want multiple actors to handle the widget event you can create an event dispatcher in the widget.

So when you trigger a widget event, you then Call the dispatcher. As it stands, no-one will register this event, for that to occur you need to create a Bind to it.

The following example shows how to bind it. So when my dispatcher is activated my CustomEvent_0 fires and prints the string.

NB the widget needs to be able to be referenced from any actor binding to it. My example is simple in that I create the widget, therefore i know about it. In reality you may need to store a reference to the widget in the GameMode, PlayerController or GameInstance (or another actor), eg

As to why your ArenaUI is not valid, it could be a race condition? Your GameMode may exist, but by the time you reference your ArenaUI, it may not have been actually created yet. Again you can use dispatchers in the widget to inform the world that you are properly created (eg in OnInitialized), or if time is not that critical, add a small delay before getting the widget reference.

yes @Dee_Kay_Bee is right, you can use a delay before getting the widget reference. Sometime, small amount of delay time is working (I usually do delay for .1s for this kind of behavior)

Thanks!