Fetch loaded widget blueprint from c++

I created in Blueprint a Widget Blueprint HUD, loaded by the game mode. In C++, I would like to recover the widget Blueprint to play with is visibility. What method can give me access to a loaded widget blueprint?

You have to have a two things for that:

  1. A class that c++ know
  2. A pointer to your widget
  1. If you only need visibility, then having default UUserWidget is fine;
    If you want to access any custom data, then you should derive from UUserWidget, add custom data here and then derive new BP widget from your CPP widget. This way, even if you’ll create an instance of BP widget, with pointer to it you’ll be able to access data known to its CPP parent

  2. For getting pointer you have a few options:

  • a “global” variable with type of pointer-to-base-class-from-step1, on something like gameinstance or other object that you can easilly access knowing UWorld;
  • a function that accept pointer-to-base-class-from-step1 as a parameter, call it from blueprint passing your widget in;
  • GetAllObjectsOfClass()-type calls. Since it’ll return you all the objects of this class, it’ll be not a good idea to call it directly with UUserWidget as parameter, but can be ok for using with child class you created. I wouldn’t suggest this way, as searching for object you can easily get direct pointer to is dumb, but this way does exist and may be helpful in some cases

And after you got a pointer of type you need - you can do anything you need with it

1 Like