Download

Why does CreateWidget c++ version not behave the same as Create Class BP function?

I’m trying to move some BP stuff into code, specifically some Widget UMG stuff. I’ve got a widget reference in code (tested and verified) so I can trigger events and call BP functions from my code. What I want to do is rebuild alot of the BP functions in code. One of which is the creation of the widget, which uses the Bp function “Create” ExtendedBpClass “Widget”]. This BP node is used for spawning alot of stuff. It has the Class dropdown box where you can select the class you are wanting to spawn. For me I select the Extended user widget class that I’m getting ready to draw to the screen. After this you would generally “Add To Viewport” node which puts it up on the screen.

Problem is there is a C++ function called CreateWidget which I use to get the instance reference in code. I would imagine this function is the same but it’s not of course. So where is the C++ equivalent for the BP function above? Clearly this version does something else which prepares it for being “Add To Viewport”?

So basically you just call the AddToViewport function after creating your widget with the CreateWidget function. Here’s some code I’m using myself.



	if (UMGScoreboardClass)
	{
		scoreboard = CreateWidget<UScoreboardWidgetBase>(this, UMGScoreboardClass);
		scoreboard->AddToViewport();
        }


UPROPERTY(EditAnywhere, Category=“Categoryname”)
TSubclassOf<UUserWidget> WidgetBPReference;

Then,

UUserWidget* WidgetInstance = CreateWidget<UUserWidget>(GetWorld(), WidgetBPReference);
WidgetInstance->AddToViewport();

Before running the code, go to the details of your widget bp and set the reference. (I guess you have already done this)

Yeah I was looking at this all wrong. I thought when I created an instance of the blueprint class it would have the attributes and functions the actual blueprint version has. I realize now that I was wrong and that when I created a widget instance in code that it would only have the parent class of that blueprints functionality. Any members and functions that I added in the blueprint version are not accessible, which is why my code version appeared differently than the BP version. Thanks for the input though!