Download

Opening a UMG blueprint menu from c++

What’s the magic command for opening a UMG blueprint from c++? I have a simple UMG menu defined and re-parented to a c++ class derived from UUserWidget. But I can’t figure out how to open it.

CreateWidget<YourCppClass>(OwningWorld/OwningPlayerController, ReferenceToTheBlueprintClass);

Will create it, from there the calls are more or less the same, AddToViewport on the return of Create widget for example.

Thanks for the reply, but I’m sorry to admit that I don’t fully understand what told you me. What is the <YourCppClass>? I’m assuming it’s the parent class to the UMG blueprint. Could you also clarify the “ReferenceToTheBlueprintClass”? Where do I get the reference? (FClassFinder?)

Yes, YourCppClass is the template argument, you use your C++ base class there. The reference is the class to create, it’s expecting a UClass*, so you’ve got to some how locate the class you want to spawn. I’ve never used FClassFinder. I always use TSubClassOf<Blah> MyClass; as a Uproperty in my class and expose it in a blueprint so that it’s something I hookup in a blueprint asset, instead of something I hard code in a C++ file.

An Alternative

Dear Whammy,

You could also just send a request from C++ to Blueprints, and let Blueprints handle it

I call these CPP Requests, which I first invented while trying to work with Hourence’s Solus Item system without having to rewrite the system in C++.

With CPP Requests, the flow is reversed, and you are actually asking Blueprints to do something from C++!



UFUNCTION(BlueprintImplementableEvent, Category="Joy UMG", meta=(FriendlyName = "Joy Mech ~ CPP Request ~ Open Mesh Editor"))
virtual void CPPRequest_OpenMeshEditor(AJoySMA* JSMA);


So you’d put this event in one of your Blueprinted classes like player controller, and let your player controller BP open up your widget.

You still get to make this event happen whenever you want, via your code calculations and C++ class structures and work flow, but you still get to use all the convenient UMG nodes in BP!

In my opinion you get best of both worlds,

  1. Power to control when the UI gets loaded based on C++ calculations and conditions,

  2. Ease of use of UMG BP nodes

:slight_smile:

Rama

PS: I was just doing this a lot today, so it is a proven workflow for me, that I actually really like :slight_smile:

It is one of the core features of my project that is the demo for the book I am writing on UE4 C++

I did much the same thing Nick Darnell described above just the other day over here. I created a new UMG menu widget BP, added a reference to that widget type to my player controller BP (which is a subclass of my custom C++ player controller), and then created the widget in my custom C++ player controller.