Download

Populating a UMG combo box from c++

I have a UMG menu that needs a combo box filled with the available screen resolutions. I have a list of resolutions, but I don’t know how to get them into the UMG combo box. I’m assuming you use OnGenerateWidget to do a call back to the parent c++ class. But I don’t know what’s supposed to happen past that.

You could create a blueprint implementable event within the c++ parent class with its argument being your list of resolutions. Then in the blueprint widget populate the combobox when this event is called.

Are you using UComboBox or UComboBoxString? I’d advise against the former as it’s still tagged experimental and is outright incomplete (in 4.5, at least). The basic reason being that the underlying Slate widget, SComboBox, uses template arguments to support any type, and UComboBox being a UObject cannot support template syntax.

In any event, OnGenerateWidget isn’t what you want, it’s a hook called for each combo box entry to modify what child widgets will be added for said entry. Although you might eventually do want to use it, for instance to add a check mark or other symbol next to the currently selected resolution.

Given a UComboBoxString, here’s how I would do it:

  • In your base C++ widget class, create a BlueprintReadWrite UComboBoxString* property
  • In your derived widget blueprint, use the Construct event to assign your combo box widget to this property
  • In your base C++ widget Construct_Implementation, iterate over your resolutions and use AddOption on the UComboBoxString* property for each resolution
  • Still in the Construct_Implementation, subscribe to the OnSelectionChanged event and hook your resolution switching logic there

In order to call both the blueprint Construct and your base class Construct_Implementation, you’ll have to tell the blueprint to call its parent implementation. This is done by right clicking the Construct event in your event graph and choosing “Add call to parent function”, which will create a node that calls the native implementation. Be sure to put this parent call after you assign the property, or it will be null in your native construct and you won’t be able to do any of this.

Note that this is a C++ centric solution, which is how I’ve been using UMG. For a more blueprint-oriented approach, you could instead just have C++ somehow expose a list of resolution strings to your derived blueprint, then loop through that with the AddOption node in your blueprint construct event. It all depends on how much logic you want done in native vs script.