UMG controlling a Blueprint

Hello there,

I am having some troubles with UMG and Blueprint and it feels like a newbish question …

Currently I am trying to set up a system to change the Material of an object by clicking on a UMG button in the HUD… But I somehow cannot find any documentation on how to have UMG work with another Blueprint.

Basically I have a Blueprint that has the Static Mesh inside of it, a Box Collider that activates the input and adds the Widget to Viewport and also adds some informative TextRender…

Now I get it to look like I want it for now, but I cannot connect the Buttons with the Material…
What do I need to do?

Primarily I cannot access the StaticMesh of the Blueprint within the UMG Graph Editor… Or would that be the wrong way anyway?
I want to have
onClickedEvent Button 1 –> assign Material 1 on the Static Mesh Component
onClickedEvent Button 2 –> assign Material 2 on the Static Mesh Component
.
.
.

I only found a way too complicated way… why can’t I just access the SetMaterial node of another Blueprint or get the OnClickedEvents into the static mesh Blueprint… what am I not doing…

Thanks alot!

Watch blueprint communication tutorial.

All your problems are from fact that unreal uses object oriented programming, for most of things.
So you need reference to another object, then cast to its type then unreal knows what is inside referenced memory.

You somehow must tell UMG reference pointers to both static meshes.
Easiest way is if player controller finds those meshes and stores reference.
Then UMG can get owner player controller, and read references from there.

And watch blueprint communication.
Then dig into references and pointers, you should understand how that stuff works or you are into plenty of pain later.

Sadly I am completely lost… I now watched a couple of Blueprint Communication tutorials… but they never mention UMG…

This cannot be so complicated…
I want something to happen when i click/touch a UMG button… where can I find a tutorial for this?
The documentation leaves me clueless and I cannot find anything on Youtube and I cannot think of any more search terms… please help! <3

EDIT:// Ok, just found the Zak Parrish tut… Let’s see if he can help me^^

UMG is no different then any other Blueprint. You need to have the reference and cast it correctly.

Solution to make something happen when you click button:

Split this into two parts:
first: “simulating” (or calling) event in player controller when you click button in umg
second: doing something in level when event happens.

First:
Create your own player controler blueprint class. Set it in game mode so game uses your controller not default one.
Then create event in player controller, name it for eg. “button 1 pressed”.
setup button 1 in umg. get that on pressed event in umg. in that event create node “get owner controller”, from it cast to “your player controller”
now you can get your event in player controller “button 1 pressed” add call to it from cast node.
Test if it works, easiest is to add print string to event in player controller, you should see print working every time you press that button.
you have now half of solution.

second:
communicating from player controller to actor in game:
create actor blueprint, add your box there.
create event in that box actor. for eg. “do it box!”
now in player controller instead of that printing test node do this:
get all actors of class (and pick box blueprint class you just made)
from that you get array as result, get element 0 (write get and you should get node, set index to 0 for now)
from result cast to your box blueprint
then find event “do it box!” add it to graph.
now add print node inside box actor, place one box in level.
test if it works

Ps.
dynamically adding umg widgets when you have hard time with references is not advised yet.
If you do that described above communication, i can write how to do dynamic umg booxes, but communication to player controller may be mess (or a bit advanced).

Hey Nawrot, thank you for helping me!

I only have a tiny problem understanding one of the things you wrote:

I have set up the Player controller Blueprint [BP_Character] and Gamemode [BP_GameMode],
I created a custom event called “Button 1 pressed”
I went into UMG, added the OnPressed Event for Button 01
and here is where you lost me :frowning:

Which “Get owner” node do you mean because Get owner controller does not seem to exist - at least I cannot find it (UE4.11) … Do you mean “get Controller” or “Get owner (character)”…?
I tried around but did not get anywhere…

I would entirely fall in love with you, if you could really quickly stitch something like this quickly and dirty together and send me a screenshot or two…

Sorry for the newbism but this is completely beyond my understanding … at the moment! But I understand the concept of having to reference things around now , but I dont quite know the nodes yet… not even close haha!

Thank you once again!

In umg when you do get owner (or owning player controller) you should get reference to player controller, or pawn.
Sorry i cannot write exact names not at my unreal pc now. And cannot do screens now.

Edit.
inside UMG graph, write “controller” there should be required node,
if not write “pawn” it gives fewer nodes, controller node should be in same category as pawn node is.

If your game is singleplayer, you can get player controller (0) but that is terrible way to reference player.

Ok i made tiny project for you:

Thank you! Will look into it, and on first glance this looks quite understandable! Thank you very much!

If you have questions ask them today, I may get into something else and forget what i did in this one.

Ok i am stuck … i have been trying around to get what I want to do working. Your setup is a little more sophisticated than I actually need it.

I know I need exactly three buttons.
Each button triggers one material on a staticMesh- for example a white rough, a blue metallic, and a black shiny material (irrelevant examples) on a cube.

As I see it:

  1. Your PlayerController creates the HUD widget
  2. In the HUD widget is a custom event that creates a button widget and parents it to a vertical box
  3. The Player Controller checks how many boxes are in the scene and repeats the Button creation custom event as often, thereby creating the buttons in the HUD.
  4. Your BP_BoxyBox lets the box meshes rotate at random, creates a random color (via a function) ,an instanced material and assigns a random color to each box
  5. Your BP_BoxyBox has a function called ChangeColor that, depending on if the input parameter reports as pressed it changes the randomly created color to a predetermined color
  6. Your UMG_Button checks wether there are any Boxys in the scene, and gives the buttons the corresponding random colors to the boxes.
    Now it gets interessting:
    7)A OnButtonPressed event triggers the ChangeColor with a pressed State/parameter, thereby changing the color.
    You added a Object reference variable and set it as a target, thereby communicating between the blueprints (i understand that yay :smiley: )
  7. A OnButtonReleased event trigger the ChangeColor with a unpressed state/parameter, thereby changing the color back to random.

Now thank you for this! I understand a great deal more about Blueprint communication now than before!

But I cannot get my thing to work…

  1. I created a PlayerController that creates a HUD and adds it to Viewport. I do not need the dynamic button creation but good to know how it works! I just created a isValid node but did not have anything coming out, or do I? cannot find any decent info about it…
  2. I created a BP_Cube and created this function:
    “Set Material 01 Function”
    <<Set Material >> = Target = the Cube // Material =Material_01
  3. The same thing I did with Material function 02 and 03

To Check wether these functions work, I enabled inputs and put them on keyboard shortcut and that works fine.

  1. I go into my UMG Hud, get the button 01, get my BP_Cube reference, put the function in the event graph and connect the OnPressedEvent into the SetMaterial function and add the BP_Cube reference as a target.

What else could I need to do? Does my function need an input value/parameter?

What just occured to me…
I am creating a mobile game… does on pressed/clicked not work/work differently for mobile?

Thanks alot!

Ok so in a moment of understanding I created the isValid Node, put the exec onButtonPressed, input object my BP reference, on Isvalid the Set Material function and not valid have it print a string (as you mentioned above before Nawrot.
And lo and behold, it prints my invalid text all the time… so I know the communication between my BP does not work yet… do I have to toggle something in the blueprints to make them react to each other?

I am looking through basically every thing I can find in your file and am obviously missing something… Thanks again for your help!

You need to have valid reference value. Havent looked into your project yet, (just woke up and i am in work now).

As for mobile games, touch interface does not play well with umg input. UMG is made for one touch and mouse pointer, and touch interface is multitiouch.
For it all to work nice you need to code tracking of all touch locations, detecting gestures (if you need them) there are some gestures in unreal, but they are either weird or i do not undestand them yet.
For eg with standard umg button, if you press it then drag touch event outside that button will get stuck. Sometimes such behavior is wanted but mostly not.

Do you have an idea why my reference value may be invalid?

Need to see your project for that, cannot look at it at work.

Ok thank you! I’ll continue looking for my mistake and tell you if I find it, then you can save your time, but otherwise I would be immensely thankful!
Have a great day now!

project does not function for me, when i click the icons they turn yellow, however the main block does not change

It turns grey and then back again. It does work for me…?

Yes it should turn green whatever button is pressed. Then go back to starting color.

This is very simple project meant to show blueprint communication with UMG and actors in level.
I didn’t do check for edge cases, or did not make sure all actors start with right colors etc.
Also mouse steals rotation movement making impossible to turn etc, but that was not focus of that example.

Edit:
When you folks get idea what this project does, I can make 2 following projects that are gradually more advanced:

  • doing same things but with dispatcher that are connectced to player controller
  • then dispatchers again but this time contained inside blueprintable component, that do all magic automagically, you just need to drop them on any actor (including meshes etc).

But because this is supposed to show how to do stuff, i will wait a bit, giving you time for basic stuff first.

EDIT:// No longer necessary ! Thanks for everyones Help!

Had no time yesterday, maybe tonight i can do it.