* Extension of the WidgetComponent to make it easy to have owning Actor context to the Widget. Assign the owner, now we have easy access in the widget itself USActorWidget* WidgetInst = Cast(Widget) UE_LOG(LogGame, Warning, TEXT("WidgetClass for %s does not derive from SActorWidget"), *GetNameSafe(GetOwner())) Suggest deriving from actor widget (not strictly required, but you lose the benefit of auto-assigning the Actor this way) If (!Widget->IsA(USActorWidget::StaticClass())) Base implementation creates the 'Widget' instance Void USActorWidgetComponent::InitWidget() SetCollisionEnabled(ECollisionEnabled::NoCollision) Set common defaults when using widgets on Actors USActorWidgetComponent::USActorWidgetComponent() The functionality we add here is to set the owning Actor on the SActorWidget class (will be covered in a bit) which is an exposed variable to Blueprint for use in the UMG Editor. The following is the code for SActorWidgetComponent and derives from Unreal’s WidgetComponent class. If you’re just interested in the concept and will us Blueprint you don’t need to do this. Make sure your project is prepared using the link above, it shouldn’t take too long. Required: Extend UserWidget for UMG Widgets The user “WCode” has Epic Wiki page for exactly that: For C++ to extend UMG, which is what we’ll be doing, you will have to prepare your project first. Preparing your C++ Projectīesides the super easy implementation for Blueprint, you could do the exact same in C++ or you could opt to make your own user widget and widgetcompont classes, I’ll quickly show you how that’s done. Look at the end of the post for some more context screenshots in case you’re not quite following this yet. In the above example we added the widget blueprint to a grenade actor so it can display the information on mouse-over when the player sees it in the world. ![]() Finally we fill in the OwningActor variable with the Actor “self” and we’re done! the Player pawn Blueprint from the image above) during BeginPlay, and we get the “User Widget Object” which we need to cast to the correct type. Now I access the Widget Component on the Actor that will own the Widget (eg. I created a fresh Widget Blueprint, added a variable called OwningActor of type Actor. Here is the basic principle of how you could achieve the same result in your Blueprint project in a quick and straight forward manner. You could do this in Blueprint too, just a little different. You may also not like the idea of the custom UserWidget base class for your UI elements moving forward (those are will be drawn in the world at least). Not everyone wants to touch C++, and it’s not really required. But first, let’s look at the simplest Blueprint implementation to get an understanding of what we’re trying to solve in the most basic way. The C++ snippets below show you how that’s done. This is great for retrieving the health of the Actor we are attached to for example. ![]() ![]() In the C++ Implementation below our custom widget component sets the Owner (an Actor) of the WidgetComponent as variable inside our custom UserWidget class so that we have instant access to the owning actor when working on the UI element. By default the UMG Widget has no context on what is it being drawn on top of (it doesn’t know which WidgetComponent it belongs to either) this is a problem for us as we have no way to find out what health the actor is at, or what name to display for that specific Actor (in the image below: “Spy” or “Soldier” based on a variable inside the player’s Pawn) To make it easy to get information on what the widget is attached to, we need to help it out a little. The sample code is done in C++, but keep reading as I show you a quick and easy Blueprint-only trick too! The following guide explains the concept of how to be able to fetch the information you desire for your in-world widgets such as health, player names, etc., you don’t need to follow this to the letter, so long as you understand the concept, and implement what suits your own projects best. It’s quite simple to do, and I hear a lot of questions about this, so today I’ll share you some tricks to make this even easier. Today I’d like to quickly show how you can add UI for things like health bars, nameplates, interaction prompts and more in Unreal Engine.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |