How do I access variable for Vertical box in User Widget?

Hey everyone, I created a main menu widget with three vertical boxes in a scroll box. Each of the vertical boxes is marked as a variable:

Next, I added the widget as a widget component to an actor blueprint. I am trying to figure out how to access the vertical boxes in order to add buttons to them from the blueprint’s construction script. But, everything I am trying is not working. Here is what I have so far:

I have a variable for the main menu, but when I drag off its blue pin, I don’t see any way to access the variables.

This has to be something simple. What am I missing?

It’s this. But I feel it may give you a royal pain in the neck sooner rather than later - I mean working with a Widget Component inside the actor’s CS. Must we do it this way?

Perhaps there’s another, more natural way - what are we making?

If you must do it this way, consider moving this to the actor’s Begin Play. It may work OK (depending on the end goal) in the CS but I’ve had mixed successes with a setup like this.

1 Like

Thank you! I thought there would be a function to get a variable by name or something similar.

I want the main menu to show the user a list of buttons, each shows the name
of a category of puzzles.
I have a Data Table with a large number of rows. Each row is for a puzzle. One of the columns is the puzzle category. I have this blueprint to pull the unique categories into an array:


I want to create the menu from the data, not hard-code it.

I am doing this in the Construction Script so I can see the UI in the editor before launching the game. If I move it to the Begin Play event, I am afraid I won’t see it until after launch (I could be wrong about this).

1 Like

You’re right. However, do note that something like this would normally be scripted inside the widget itself - that’s what Event Pre Construct is for - for previewing changes (sadly this works for the widget designer only, and does not propagate to the component and thus may not be ideal when working with World Space widgets):

In many cases this is the intedned workflow, the vertical boxes are already there, for example.

Good luck!

1 Like

Thank you for letting me know about the Pre-Construct event.

I agree this would be better done inside the widget since the variables will be accessible.
I am going to add the number of solved puzzles in the category to the button later. That will come from the game state, which (according to the dialog box) is not accessible from pre-construct.

So, I think I am going to have to stick to doing it in the Construction Script.

I tried to follow your example with this blueprint:

The menu is still empty.

I think I might need to instantiate the buttons so I tried this:

But, that did nothing and it seems convoluted.

What is MainMenuButton? How is it instantiated? If you wanted to construct native buttons:

image

But it’s better to make User Widgets instead.


As mentioned before, though - I doubt you’ll be able to preview this with actor Construction Script. I might be wrong.

It is a local variable of type MainMenuButton object reference.
You can see it in the lower left under Variables.

I have another user widget for MainMenuButton:

I don’t think it is ever instantiated. That is why I tried the second approach in my post above.

Creating a variable achieves little here - think of it as of storage for an object, we still need an object. You probably know this already.

Since you have a user widget to represent a button - create widgets. But you cannot create user widgets in the CS - that’s why I had my doubts. This belongs in the PreConstruct. Or can be executed with a Custom Event.

I believe you’re trying to force UMG to do something it cannot do, not out of the box. Not sure if dynamically constructed widgets mesh well with actor’s widget component taking advantage of construction script. Not if the goal is to preview it in viewport.

Sounds like a little debugging maintenance nightmare to me.


I may not fully understand the end goal but:

why Add Widget Component (many of them!) when the Main Menu is already there?

That is unfortunate. I guess I will move this to BeginPlay.

I don’t think that will work because I need access to the game state for information I want to put on the button and pre-construct does not give me access to the game state.

I guess I will have to give up on the idea of previewing it in the viewport.

I was just trying it to see if it would work. Like I said, that was a convoluted solution which did not work anyway.

I don’t get it. You’re previewing widgets in the viewport. The Game State singleton does not even exist at this point. What am I missing? One way or another, for as long as you know what needs to be done, I’m :cool: with that.

image

This is called in both editor and game so should work just fine during PIE.

Don’t assume I know what I am doing. I am a newbie and trying to learn this system.

I moved the blueprint to go from BeginPlay:

I don’t see anything in the screen when I hit the play button.

Looks like it should work unless:

  • the class variable is dodgy
  • the actor is not there
  • the array is empty
  • the vertical boxes are hidden
  • the buttons are hidden
  • you’re looking at the back of the widget

Can you double check with prints string whether stuff fires? And can you confirm you need this in World Space? I assume you do since you wanted a preview, because Screen Space preview is not really a thing at all, not in the viewport outside PIE.


Under UE5.2, if I:

I get a bunch of buttons:


Also, I think you could avoid a double loop if you:

Probably irrelevant if it’s just a handful of buttons. May matter if it’s thousands.

This is strange. I tried the play button again and this time I see it:

I must have missed it somehow.
I have to format it, but it it a start!

I like your single loop idea. I will try that.

Thank you for the help!

1 Like

If you ever need to up the quality, lower the scale of the Widget Component and compensate with its Draw Size.

1 Like

I tried the single loop idea:

The print is never called.
If I change the pin from the print to get called from the false condition, I get a bunch of prints to the screen.

I added a print to the two loop solution. Here is the blueprint:

The prints are called correctly. So, the Add Unique is working as expected, but it does not seem to be returning the index value.

I copied my project to a test project and started diagnosing why the AddUnique was not returning the index value.

I eventually figured out I had to delete my blueprint widget from the level and add it back again. Now, everything is working with the single loop solution.

Thank you for your help!

1 Like

I am trying to do this in a blueprint since I am adding content to the widget dynamically.

If I use this blueprint (Note the link from Add Child to Vertical Box to Set Draw Size is missing):

And I run my app, I get a button on the screen:

But if I add the link to change the draw size and scale:

I get an empty screen:

Did I use an incorrect function?

Hard to tell by just looking at these snippets. I’d start debugging it with less aggressive values.

I’m also not sure how well the widget components scale with dynamic settings, manual redraw and drawing at desired size used to be iffy. Test it manually:

image

Get the size working and only then apply it dynamically. Does it need to be scaled dynamically? Draw Size over 4k will probably not work at all, you’re multiplying it by a factor of 100 so that’s probably far beyond what the component can render.


If you need fidelity that exceeds 4k x 4x draw size, you’d need to split the rendering into multiple components.