Download

UMG and C++

Hi,

i need a little bit of help, because i’m stuck:
I want to create a Scoreboard. My first step is to show up a simple “Test-Text” box on the screen.
I Created a Widget Blueprint called “Scoreboard” and added a Textbox by Following this Tutorial: https://wiki.unrealengine.com/UMG,_How_to_extend_a_UUserWidget::_for_UMG_in_C%2B%2B. My C++ Class is named “Scoreboard” with nothing in it, but the generated stuff.
Now is the point where i don’t know how to proceed.
The Code is like https://forums.unrealengine.com/showthread.php?52773-Tutorial-Snippet-Creating-a-UMG-Widget-in-C-and-delegate-example
PlayerHUD.h



...]
UCLASS()
class FPSPROJECT_API APlayerHUD : public AHUD
{
	GENERATED_UCLASS_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HUD)
	TSubclassOf<UScoreboard> ScoreboardClass;

	class UScoreboard* ScoreboardWidget;
...]
}


PlayerHUD.cpp



void APlayerHUD::BeginPlay()
{
	Super::BeginPlay();

	AStdCharacter* MyPawn = Cast<AStdCharacter>(GetOwningPawn());

	if (MyPawn)
	{
		ScoreboardWidget = CreateWidget<UScoreboard>(Cast <APlayerController>(MyPawn->Controller), ScoreboardClass);
	}
}


My Question is: How do i get Something into ScoreboardClass? And what? I would like to hardcode it in C++
I think my Widget is linked via the “Parent Class”, right?

Thanks in advantage for your help!

Okay, figured it out by myself:
Inside the PlayerController.cpp constructor:



static ConstructorHelpers::FObjectFinder<UBlueprint> ScoreboardClassOb(TEXT("WidgetBlueprint'/Game/UI/HUD/ScoreboardWidget.ScoreboardWidget'"));

	ScoreboardClass = (UClass*)ScoreboardClassOb.Object->GeneratedClass;


Not that complicated after all. But took me some hours…

That’s not safe to do in a shipping build, UBlueprints and UWidgetBlueprints are an editor only time concept, it wont work in a cooked game. The recommended approach is to expose your variables like you have in C++, but make the asset connections in blueprint derivatives. It will be much easier to setup and maintain the references correctly.

Do you have an example or a tutorial for this? Because i really can’t follow you. Sorry :frowning:

I dont think that i got you right, but do you mean something like this?



static ConstructorHelpers::FClassFinder<UScoreboard> ScoreboardClassOb(TEXT("/Game/UI/HUD/ScoreboardWidget"));
	ScoreboardClass = ScoreboardClassOb.Class;


I looked at the ShooterGameMode.cpp and they did the same for the DefaultPawnClass