UE4 crashes when compiling ConstructorHelpers::FObjectFinder

Hello all,

I’m making a c++ class that inherits from playerstate so that i can make my own blueprint playstates with some modifications. The primary change is that i’m trying to include functionality for character classes (I.E. fighter, barbarian ect.) To do this i’m using a Datatable which holds some of the information for each class to be referenced (Things like how many HP the character should get per level of class and such which may need to be adjusted frequently for balancing). In order to reference my datatable im using:

 Class::Class()
 {

static ConstructorHelpers::FObjectFinder<UDataTable>FClassesDT(TEXT("/Game/DataTables/FClassesDT.FClassesDT"));
if (FClassesDT.Succeeded())
{
	ClassesDTRef = FClassesDT.Object;
	
}
else
{
	if (GEngine)
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("Some debug message!"));
}
 }

Where FClassesDT is my datatable and ClassesDTRef is a reference to a UDataTable:

 class TOPDOWN_API Class
 {
      .
      .
      .
 protected:

UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Class", meta = (AllowPrivateAccess = "true"))
	UDataTable * ClassesDTRef;
      .
      .
      .
 }

Whenever I compile through the editor or whenever VS finishes compiling EU4 crashes with message :

 Assertion failed: 
 [File:D:\Build\++UE4\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp] [Line: 
 3178] FObjectFinders can't be used outside of constructors to find /Game/DataTables/FClassesDT.FClassesDT

I might be mistaken but Isn’t Class::Class() the default constructor? Am I getting this error because I have children of Class?

 class TOPDOWN_API Barbarian : public Class
 {
 public:
     Barbarian();
 };

Any assistance would be appreciated!

Thanks.

EDIT:: The code seems to work as well after the engine is restart. It just crashes when compiling.

Hi.

UE4 uses C++ in a specific way. To write it properly you must use their architecture. This is why you must inherit your classes from at least UObject class. Also add UCLASS() and GENERATED_BODY() to the class declaration.

I think this might be something like

#include "MyClass.generated.h"

UCLASS()
class TOPDOWN_API UMyClass : public UObject
{
    GENERATED_BODY();

public:

    UMyClass();

    UPROPERTY()
    UDataTable* ClassesDTRef;
}

We can’t name our class “Class”, because UClass is already defined by UE4 and every UObject inherited class must have U at the beginning.

In this constructor you should be able to use FObjectFinders.

I also suggest to check Introduction to C++ Programming in UE4 | Unreal Engine Documentation to see how code in UE4 is written.

As far as I know UE4 supports multiple classes in one header, but it is a good practice to keep every UCLASS() in separate file.

Unresolved external symbols are usually caused by a lack of function/constructor body. Do you have cpp files with UBarbarian::UBarbarian() and UMyClass::UMyClass() constructors? They can be empty.

Yes. ConstructorHelpers are a part of UE4 framework and can’t be used in raw c++ objects.

Yup this solved my issue. So you should only use ConstructorHelpers inside of UCLASS objects?

I have a new issue relating to the same crashing which is now, in my new playerstate, when i go to instantiate an object that is a child of the UMyClasses object, the engine crashes on compile and on load with:

Assertion failed: [File:D:\Build++UE4\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp] [Line: 2498] UObject() constructor called but it’s not the object that’s currently being constructed with NewObject. Maybe you are trying to construct it on the stack, which is not supported.

 //In MyPlayerState.h
#include "CoreMinimal.h"
#include "MyClasses.h"
#include "GameFramework/PlayerState.h"
#include "MyPlayerState.generated.h"
UCLASS()
class TOPDOWN_API AMyPlayerState : public APlayerState
{
	GENERATED_BODY()
	
public:
	AMyPlayerState();

protected:

               ...

	UBarbarian	Barb;
};


 //in MyClasses.h
UCLASS()
class TOPDOWN_API UBarbarian : public UMyClass
{
	GENERATED_BODY()

public:
	UBarbarian();
};