UMG Widgets, Blueprints <-> C++, Turn Menu
Background
UE4 supports three different standard ways to do a 2D GUI – HUD, Slate, and UMG. I looked at HUD (low-level direct pixel drawing in C++), then Slate (window UI framework in C++), then finally UMG (Blueprint extension of Slate with WYSIWYG, released in UE 4.5).
For the following reasons, I’d sort of avoided Blueprints scripting until I got to UMG. I’ve been using C++ since high school (4+ years in college, 11+ years working). Visual scripting is cute and nifty but not “real programming”. I’d seen examples of excessive Blueprints (eg full games or large mods). Editing code with a mouse seems inefficient and limiting. As of UE 4.10, Epic hasn’t made Blueprints as text (eg copy/paste, view/edit) a first class citizen. Epic’s own documentation says Blueprints is for designers, and others say it’s for rapid prototyping. Blueprints is an additional thing to learn.
But after I did some work with UMG and Blueprints, I now see UMG and Blueprints as just another example of how UE4 game development involves working in both C++ and Unreal Editor (and integrating work between the two). Overall, working with UE4 I find easy, efficient, powerful, and of course enjoyable! I still prefer C++, but Blueprints is neat and kind of addictive. I can see how Blueprints could be useful for designers and for rapid prototyping.
Communicate between Blueprints and C++
To call Blueprints from C++ (or vice versa), I did the following:
1) Create a C++ parent class for my Blueprint. Unreal Editor –> File –> New C++ Class, choose the appropriate parent class (eg UserWidget, Pawn, Player Controller, Game Mode)
2) Create the Blueprint class. Unreal Editor –> Content –> right-click to create blueprint (or User Interface –> Widget Blueprint)
3) Set the Blueprint’s parent to the C++ class (that we created in step 1). Edit the Blueprint –> Class Settings –> Class Options, Parent Class
This connects our Blueprint child class to our C++ parent class. Once we’ve setup this connection, we can then mark a C++ function as Blueprintcallable (Blueprint can call C++) or as a BlueprintImplementableEvent (C++ can call Blueprint), such as:
UFUNCTION(BlueprintCallable, Category = "Some Category")
void FuncCallFromBlueprints();
UFUNCTION(BlueprintImplementableEvent, Category = "Events", meta = (FriendlyName = "Some Event"))
void FuncCallFromCpp();
More fun with UMG, Blueprints, and C++
This enables our Blueprint script to call BlueprintCallable functions from its parent class. Eg my Turn Menu (UMG) Blueprint script can call BlueprintCallable UTurnMenuWidget::TurnMenuEndTurn() from its parent class UTurnMenuWidget. TurnMenuEndTurn() then calls a C++ function from the game:
void UTurnMenuWidget::TurnMenuEndTurn()
{
AHeroQuestGameMode* gameMode = GetWorld()->GetAuthGameMode<AHeroQuestGameMode>();
gameMode->EndTurn();
}
A reverse example is that my game logic C++ code calls BlueprintImplementableEvent TurnMenuShowEvent(). When my game logic Blueprint child class gets this event, it does Create Hq Turn Menu Bp Widget:
I ended up using Blueprints for my UMG menu system logic. The menus don’t impact performance, and UMG integrates more cleanly with Blueprints (than C++). The “Action” button opens a sub-menu. I did related logic to hide the Main menu and show the Actions menu in response to the button’s OnClicked event in a Blueprints script. I also added keyboard shortcuts for menu items such as “2” for “2: Action” using Blueprints scripts.
Turn Menu
I’ll end this post with some screen shots of UMG, Blueprints scripts, and the basic turn menu… And a video demo. “1: Move” rolls two movement dice. “2: Action” opens a sub-menu for actions, then you can press Escape to go back to the main menu. “4: Camera” lets you use the camera controls mentioned in my previous post. “5: End Turn” ends the turn, then the camera pans to the next character and starts the next turn. More to come soon 🙂
mepem37 :: 2016/03/12 (Saturday, March 12, 2016) :: HeroQuest, Unreal Engine :: No Comments »