Posts RSS Comments RSS Digg Technorati Blinklist Furl reddit 105 Posts and 7 Comments till now
This wordpress theme is downloaded from wordpress themes website.

Archive for March, 2016 (2016/03)

UMG Widgets, Blueprints <-> C++, Turn Menu


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>();

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 🙂

image image image image image image image

Camera, Input, Turn, Marker, Highlighter

I implemented a few updates to get some of the basic game behavior systems working.

The camera uses a custom class that extends APawn (I read the Strategy Game example project does this, although I looked more at Player-Controlled Cameras in C++ Programming Tutorials).  So rather than move the camera directly, we move an invisible pawn.  The custom pawn has a UCameraComponent (which follows the invisible player) and a USpringArmComponent (which can adjust the camera’s view of the invisible player).  There’s also a second camera (a camera actor) for a static birds-eye view that shows the entire game board.

For keyboard/mouse input, I used the unreal editor’s project settings.  I added a turn marker to show the current figure’s turn.  I added a highlighter actor to highlight square spaces.  For now it highlights the turn figure’s square and the four orthogonal adjacent squares.  I added a simple data structure such that each room has square spaces (for now it iterates through highlighting each room’s squares).

I implemented a prototype turn system where it iterates through each hero then each monster.  For each turn, it iterates through states – transition (camera moves to next actor), dice roll, dice wait, end turn delay.

image image image image