Download

Create widget based on mesh name

Hi, I’m thinking there’s probably a better way to achieve this…

I’ve got a Parent ‘Building’ BP which has an onclick event, with several specific Building child BPs in the level. I want it to load a UI Widget based on which child BP is clicked on. I’ve started to automate it by performing some string calcs on the mesh name. I’ve got a string that correctly matches the UI name, but I can’t connect it to the Create Widget function to define which widget to use…

Does anyone know how?

You need to select a widget type in the “Class” input of the Construct Widget node. Right now it says “NONE” because it doesn’t know what to construct.

The output will give you a widget instance. You can then call functions or set variables on it to update the text, if they exist. Probably you want to create your own custom widget for this.

Note, the widget won’t appear until you add it to the player’s viewport using Add to Viewport. Also, it might be better to have widget-creating code in some central, player-specific class, such as the PlayerController. If multiple actors in the level are creating widgets, it can get hard to keep track of them and you can quickly clutter up your UI if you’re not careful.

Thanks. I’m trying to pass it the widget name which is contained in the string (currently feeding into the Print String in the screenshot). My query is how I convert the string into the class name - is that even possible?

Ohhh, I see. In that case you need to associate each possible string with a widget class. You could do this with a Map variable, or with a Switch on String node.

However, if there’s a one-to-one correspondence between the child BP class and the widget it needs, it’s probably easiest to either “hardcode” the widget class in each child BP class definition, or store the widget class as a variable on the BP and connect it to the Construct Widget node. Or have the parent class include a widget class variable and give each child class a different default value for it.

Thanks. I was trying to avoid any hardcoding as this project could potentially be expanded to cover a level with 100s of different child BPs, so I’d prefer to work it out dynamically. Switch on String should work fine for now though. :slight_smile: