BindWidget Null when using Create Widget

I have a widget class with pointers to components like buttons and textblocks with UPROPERTY bindwidget. When I call Create Widget from a different widget class, the pointers are null pointers. Is there something I have to do when creating a widget?

the UPROPERTY BindWidget flag binds automatically to widgets present in a UserWidget its WidgetTree from the start. Otherwise it likely does not automatically bind and you need to set the property yourself.

If you use BindWidget, you have to create respective components in the blueprint version of the widget derived from your widget class. Then, when you want to create widget, you need to use the blueprint class you’ve created, not the c++ class.

BindWidget will search for component of a certain class with a certain name, but it does not create such components for you.

If you don’t want to use blueprints, you don’t really need BindWidget, you can create the components directly in your c++ class.

1 Like

I recently started using unreal so I’m not really sure what WigetTree is. I assume it’s where the widget components are shown in the designer tab of the blueprint. I did solve the problem by creating a template and a pointer variable in the c++ class. I saw some tutorials that said that creating pointers with the BindWidget flag in the same name as the widget components will automatically bind them. If there are any simple ways to do this, please let me know. Thanks for the information!

Does that mean that I have to call the CreateWidget function for the blueprint class and not the c++ class? I was using BindWidget to create a C++ version of the Bluprint class and set it as the parent class. I did solve the problem for now by using templates but if there are any different ways to do this please let me know. Thank you!

Yes, you use your blueprint class to CreateWidget(), not your c++ class, because those components don’t exist in the c++ class.

Calling Blueprint classes in C++ seems a bit confusing but thanks. I’ll look up more on that

Number 5 tab “Hierarchy” is called a Widget Tree. Any widget placed manually on there is automatically bound to a property of the same name on the parent class if that property has the BindWidget flag.

Add an image widget on it from the editor panel called “MyImage” and create a c++ image property with the BindWidget flag named “MyImage” on the parent class of this UserWidget. It will be referenced automatically.

BindWidget is precisely used for that. You create a component in a blueprint, set it up as you like, and then bind to it in C++ to find it and work with it.
If you want to work exclusively in code, you don’t have to use BindWidget, but instead you’ll have to create these components in code. But they will be default components and you’ll have to set them up in code rather than in blueprints, and it’s quite tedious as compared to BP.

but I have to create the blueprint class in my code to use the c++property right? I think this was the part I messed up because I was getting null pointers until I made a template and set it to the blueprint class before calling create widget to my c++ class. If I have multiple children classes, this wouldn’t work I assume. Just for the knowledge, how can I call the CreateWidget function for blueprint classes in C++?

1 Like

I tried using Slate to create the components in C++ once and is was pure suffering. Thank the devs for making UMG :slight_smile:

It does work. Just remember widget trees can not be created if one is already inherited.

SomeWidget = CreateWidget(YourPlayerController, USomeWidget::StaticClass());

If I have multiple children classes, do I have to make BindWidget flags for every components in the children and create a template for them? Also, is there a difference when calling the CreateWidget function from a different widget class?

I tried this line of code in the beginning (USomeWidget being the parent c++ class and SomeWidget being a pointer), but the components were null pointers until I made a template and a pointer as a variable in the c++ class.

1 Like

Thanks! Seems like I can find a lot more information about Unreal C++ from this channel!

how can I call the CreateWidget function for blueprint classes in C++?

SomeWidget = CreateWidget(YourPlayerController, USomeWidget::StaticClass());

This is not correct, you need to pass the blueprint class, not C++ static class. If you pass the C++ static class, then none of the bindings will work.

One way to get the blueprint class is to add a TSubclassOf UPROPERTY and then pick the appropriate widget blueprint in the editor.

	UPROPERTY(EditAnywhere, Category = "UI")
		TSubclassOf<class UNoteWidget> noteWidgetClass;
UNoteWidget* noteWidget = CreateWidget<UNoteWidget>(YourPlayerController, noteWidgetClass);
2 Likes

This worked like a charm for me! Thank you Sir, you’re awesome :smiley:

You are right. looking back at my old post it seems I misread it and gave the c++ example.

I’d like to add to this post that these days I prefer to avoid using CreateWidget when I can.
I use a different approach where CreateWidget is used once to create a widget on the top layer (HUD). That widget acts as a “sub” HUD containing any widget menus and other information, acting as a “tree” of the full UI. This makes it possible to centralize UI behavior to the managing HUD class, including editor design, Z-depth ordering and input behavior. The designer can place / nest widgets below another on the editor panel, a “what you see is what you get” approach, without requiring code. This avoids the need for using CreateWidget where widgets are not created from code. Even if they are, added widgets get many of the same benefits.

My website gives in depth information on this approach (look for “sub HUD”).