How do I store a pointer to a BP class in C++?

I am stuck on the “Accessing UI Classes in C++” in C++ because the way they are doing you should apparently never do and I understand why so I am trying to do it the right way.
This is what I’ve tryed:

UPROPERTY(EditAnywhere)
TSubclassOf<UUserWidget> MainMenuWidgetRef;

I can select a class in the BP version of the game instance but it doesn’t work with what I’m trying to do with it.

if (!ensure(MainMenuWidgetRef != nullptr)) return;

	UE_LOG(LogTemp, Warning, TEXT("Found class %s"), MainMenuWidgetRef->GetName());

The ensure gives lots of errors.


How do I get BP classes to work with C++ preferably without a derived BP class of the C++ class game instance? And also without hard coding it which is the whole reason I’m not doing it the way they do it in the lecture.

Hi Oliver,
TSubclassOf does create a reference to the item but in the blueprint editor you have to assign it first before use. It should be a drop-down list of the relevant classes and blueprints that are based on UUserWidget.

What people say you should and should not do is highly subjective and as such, you have to form your own opinion. For the purpose of the course, don’t stray too much as it’s just going to cause you pain later when refactoring happens and you struggle to follow along. At the end, you’ll have the knowledge of what is trying to be done and it’ll make things easier to change once you’ve gone through everything.

I would like to follow along to the course but that way doesn’t seem to work.

ConstructorHelpers::FClassFinder<ATriggerPlatform> TriggerPlatformBPClass(TEXT("/Game/TriggerPlatform_BP.TriggerPlatform_BP"));
	if (!ensure(TriggerPlatformBPClass.Class != nullptr)) return;
	UE_LOG(LogTemp, Warning, TEXT("Found class %s"), *TriggerPlatformBPClass.Class->GetName());

image


That is why I want to not hard code it because if it can’t be found like it can’t now it could happen again.
Do you know the reason for it not finding the class? Its the same as in the lecture. Also how would you do this a different way? I’ve tryed and couldn’t get TSubclassOf to work with the rest of the code. I could set it in the BP class of the game instance but it doesn’t seem to work the same way. I can’t go .Class to get they class.

Nevermind I figured out the problem. The address shouldn’t have been /Game/TriggerPlatform_BP.TriggerPlatform_BP
It should have been:

/Game/TriggerPlatform_BP

Without the dot and everything after it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.