Widget appearing after the player interacts with an object

Hello.
I’m trying to make a simple crafting system. Basically when you interact with an anvil a widget pops up. When you are done you click Cancel and the widget disappears.
How can I do this? I was thinking about adding the widget from the anvil’s C++ class, but I don’t know if it’s even possible.

Note: I tried to create a main widget with the anvil widget as a component and setting the visibility of said widget component on tick event after checking a boolean variable from the player class, but it didn’t work. I mead it did show up when the player interacted with an anvil and disappeared afterwards, but it was impossible to interact with it.

I guess it was impossible to interact with it because it was creating the widget every tick!
Where did you set up the interaction?
Here’s how I’d do it, but keep in mind that I’m not too experienced and that this is mostly pseudocode:

  • Player tries to interact

  • If interacting object is of class AYourAnvil:

    GetPC()->GetHUD() and cast to your HUD->GetYourWidget() and YourWidget->SetVisibility(ESlateVisibility::Visible)

  • When he clicks “Cancel”:

    YourWidget->SetVisibility(ESlateVisiblity::Hidden)

Note that you have to create your widget before you want to interact with it, what you can do is create the widget in your HUD/MainWidget on BeginPlay(), AddToViewport() and SetVisibility(ESlateVisibility::Hidden). Then save that widget in a reference variable because you need to access it through your PlayerController.

If you want to show some code or explain your code structure, I can give you more specific information!

I guess it’s not very efficient, but as I said, I’m not very experienced :slight_smile:

Oh My GOD I’m a MORON! Ignore my question.
The problem was, that I was adding the main widget to viewport on ReceiveDrawHUD. Now I do this on BeginPlay and everything works.

Thank you for your answer and sorry for taking your valuable time.

PS: Is changing the visibility on tick is a very bad thing?