How to create widget with variable in C++

Hi,
I’m trying to use the CreateWidget function in C++ to create a blueprint widget, but have run into an issue with passing in a variable.
This is the variable in the blueprint called InventoryWindow:

337822-1.png

And this is how I create the widget in the blueprint version of the inventory component:

337823-2.png

The C++ component is attached to a blueprint that passes in the inventory window blueprint as my engine was crashing when I tried to use FClassFinder and is stored as InventoryWindowClass. Here is my C++ code relating to the inventory window:

InventoryComponent.h:

337824-3.png

InventoryComponent.cpp:

How should I edit my current code to produce the same result as the blueprint?

1 Like

Hello! I used to add special Init() method to all C++ widgets and call it just after CreateWidget to pass all params… Only after that I add it to viewport, so it is always consistent when viewed…

I haven’t converted the widget to c++. Can I not pass parameters into a blueprint widget?

Hi there!

By default you can’t do a one-liner equivalent like in Blueprint. What you would need to do is ensure your variables are set before the Widget is first added to the Viewport (or parent widget, etc…). The node in Blueprint works by doing the same thing, just visually bundled together.

There’s actually an outstanding issue with Widget Expose on Spawn’s that I’ve found. If you use Expose on Spawn variables on User Widgets, they won’t be available until after the Initialize event has been called and finished on the Blueprint.

Hope this helps!

  • Simian
1 Like