I want to show a text widget right after successfully casting an ability. What's the…
- User
I want to show a text widget right after successfully casting an ability. What's the best location to do this in UE5 GAS?
- Assistant
In Unreal Engine 5 GAS (Gameplay Ability System), you can show a text widget after successfully casting an ability by using the following steps:
- First, create a UMG (Unreal Motion Graphics) widget for your text. This will be the text widget you want to display on the screen.
- In your Gameplay Ability or your Character's Ability System Component (ASC), use the
On Gameplay Ability Endedevent. This event is triggered when an ability has been successfully completed or canceled. - Check if the ability has been successfully completed by using the
Effect MSECpin from the event node and theWas Ability Successfulfunction. - If the ability was successful, communicate with the player controller or HUD to display the text widget.
- In your Player Controller or HUD, create a function to add the text widget to the viewport.
Here's a high-level example using Blueprint nodes:
- Create a custom event in your Character's ASC called "AbilitySuccess" and have an input for the Ability-to-Display Text mapping.
- After the "On Gameplay Ability Ended" event, check if the ability was successful. If it was, call the "AbilitySuccess" event and pass the mapping input value.
- In the Player Controller, create a function called "DisplayAbilityText". In this function, create a widget of the class you made in step 1, and add it to the viewport.
- In the Character's ASC event graph, create a reference to the Player Controller and call the "DisplayAbilityText" function when the "AbilitySuccess" event occurs.
This should display the text widget on the screen after successfully casting an ability using the Unreal Engine 5 Gameplay Ability System.
- User
Instead of the player controller ,can this be done in the player actor class?
- Assistant
Yes, you can implement the functionality in the player actor class (usually the Character class) instead of the Player Controller. Here is how you can do it:
- In your Character class, create a custom event called "AbilitySuccess" and have an input for the Ability-to-Display Text mapping.
- After the "On Gameplay Ability Ended" event, check if the ability was successful. If it was, call the "AbilitySuccess" event and pass the mapping input value.
- In your Character class, create a function called "DisplayAbilityText". In this function, create a widget of the UMG text widget class you made earlier, and add it to the viewport.
- In the Character's Ability System Component event graph, create a reference to the Character and call the "DisplayAbilityText" function when the "AbilitySuccess" event occurs.
With these steps, you will be able to display the text widget on the screen after successfully casting an ability in the Gameplay Ability System using your Character class instead of the Player Controller. Keep in mind that handling UI logic in the Character class may not be the most efficient or organized way to structure your code, but it is possible. It's generally recommended to handle UI logic within the Player Controller or a dedicated HUD class.
- User
Can you show me code of creating a widget using StaticClass() that has a binded widget within it? I want to do this to avoid having to do TSubclass<> of the widget because it is cumbersone to do so.
- Assistant
You can create a UMG Widget using the
StaticClass()function in C++ by following these steps:- First, include the required headers:
#include "Blueprint/UserWidget.h" #include "Runtime/UMG/Public/UMG.h"- Then, load your UMG widget into a
UClassby using theConstructorHelpers::FClassFinder:
ConstructorHelpers::FClassFinder<UUserWidget> WidgetFinder(TEXT("/Game/YOUR_WIDGET_PATH/YourWidget_BP")); UClass* WidgetClass = nullptr; if (WidgetFinder.Succeeded()) { WidgetClass = WidgetFinder.Class; }Replace
YOUR_WIDGET_PATHandYourWidget_BPwith the correct path and blueprint class name for your widget.- Next, check if the widget class is valid, create the widget, and add it to the viewport:
if (WidgetClass != nullptr) { UUserWidget* WidgetInstance = CreateWidget<UUserWidget>(GetWorld(), WidgetClass); if (WidgetInstance) { WidgetInstance->AddToViewport(); } }This code will create and display an instance of your UMG widget on the screen without requiring a
TSubclassOf<UUserWidget>. However, using theStaticClass()function is not the recommended way to create widgets, because it is not as efficient or as flexible as usingTSubclassOf<UUserWidget>. UsingConstructorHelpers::FClassFinderto find the widget's class during runtime may also have repercussions on performance, especially if done frequently. It is generally better to use theTSubclassOf<UUserWidget>approach, even if it may seem cumbersome. - User
Can you show me code where a text widget is displayed above my paper2d characters head in ue5?
- Assistant
Since Unreal Engine 5 currently does not have specific support for Paper2D, I'll provide you with a code solution that will work with standard Paper2D characters in Unreal Engine 4, which should also be applicable to UE5.
To display a text widget above a Paper2D character's head, follow these steps:
-
Create a UMG widget that contains a
TextBlockelement that you want to display above the character's head. Let's call this widgetW_TextAboveHead. -
In your Paper2D character's header (
.h) file, add the following includes:
#include "Blueprint/UserWidget.h" #include "Components/WidgetComponent.h"- In the character's header (
.h) file, add the following variables:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UI") TSubclassOf<UUserWidget> TextAboveHeadWidgetClass; UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "UI") UWidgetComponent* TextAboveHeadWidgetComponent;- In your Paper2D character's source (
.cpp) file, initialize the TextAboveHeadWidgetComponent in the constructor:
ATrackedCharacter() // Your custom constructor name could be different { // ... TextAboveHeadWidgetComponent = CreateDefaultSubobject<UWidgetComponent>(TEXT("TextAboveHeadWidgetComponent")); TextAboveHeadWidgetComponent->SetupAttachment(RootComponent); }- In your character's BeginPlay override function, create and attach the widget to your character:
void ATrackedCharacter::BeginPlay() { Super::BeginPlay(); if (TextAboveHeadWidgetClass) { TextAboveHeadWidgetComponent->SetWidgetClass(TextAboveHeadWidgetClass); TextAboveHeadWidgetComponent->SetRelativeLocation(FVector(0.f, 0.f, CharacterHeight)); // Set the Z value to the desired height above the character's head TextAboveHeadWidgetComponent->SetDrawSize(FVector2D(200.f, 50.f)); // Set the width and height of your widget, adjust it to your desired size TextAboveHeadWidgetComponent->SetWidgetSpace(EWidgetSpace::Screen); } }Replace
CharacterHeightwith the desired height above the character's head where you want the widget to appear.- In Blueprints or C++, you can then access the TextAboveHeadWidgetComponent and set the text of your TextBlock element to display the desired text.
Remember that this code is intended for Unreal Engine 4. If you are working in Unreal Engine 5, it's highly recommended to use the native Sprite and UI components, which may have different APIs than Paper2D. The general method for displaying a text widget above a character's head remains the same, but specific class names and functions may be different in UE5.
-