How to remove a widget?

Hey guys.

So I am trying to make that when you aim, a crosshair would appear and when you release the button I want it to disappear. The problem is that I only found a way how to remove all widgets, but that would be bad later in the future for the HUD.

0215410f3c744c2d8ec737eacb351201.png

Any ideas how to remove just the specific crosshair hud? Thanks in advance

“Create Hud” is a method on your third person character blueprint.
Thus, you need to add a “Remove Hud” method on that same blueprint.
In the “create” method, store the user widget you actually create and add to the viewport.
In the “remove” method, look at that stored widget, and remove that from the viewport, then set the variable to None so you know it’s not active anymore.

1 Like

Off the top of my head I’d have the widget on the screen at all times but bind its visability in the widget itself.

So on button press set bIsAiming true on release set bIsAiming false.

In the umg find its widget and bind its visability.

Get owning player work your way to get that bool. And return visible if true return hidden if false.

Hey, thanks for your input, but im a bit lost with the remove method. here’s how my create method looks.

Could you please show me how the remove should look? Im very new with this and I couldnt find a solution online that would work for me.

Ok, it was so simple that I feel stupid now. Thought it will be more difficult, but anyways I will post my solution

6 Likes

An alternative is to create a new variable called “TheCrosshair” on your character. Make it take a reference to a Maincrosshair Widget class. After the “Create Maincrosshair Widget” node, take the return value and assign to that TheCrosshair variable, before you attach it to the viewport.

Then, create a “Remove Hud” method in the same blueprint as creates the widget, and make it get the value of TheCrosshair and pass it to “Remove from Parent”

The way you found will work, too, as long as you don’t need a permanent reference to the widget. (You may want to change crosshair colors, draw charge-meters, or whatnot, depending on game mechanics – but solve that when and if you need it!)

Like what was mentioned before, its probably best to just toggle visibility of the widget rather than creating and destroying the widget many times.

In the new Version you use Remove from Parent (its basically the same as remove from Viewport) you need your UMG the same variable you add to your viewport
Anmerkung-20200204-231139.jpg