ConstructorHelper in function cause a crash

When editor crashed after called function the debuger make exception and show me this string
UE_CLOG(!GIsInConstructor, LogUObjectGlobals, Fatal, TEXT(“FObjectFinders can’t be used outside of constructors to find %s”), ObjectToFind);

// Fill out your copyright notice in the Description page of Project Settings.

#include "MyRunner.h"
#include "GeneratorFunctionLibrary.h"



//Happy Message
FString UGeneratorFunctionLibrary::GetHappyMessage()
{

	static ConstructorHelpers::FObjectFinder<UDataTable> GameObjectLookupDataTable_BP(TEXT("DataTable'/Game/ThirdPerson/GameObjectLookupTable.GameObjectLookupTable'"));
	
	return FString("Victory! Victory BP Library Works!");
}

I tried add _C to the end of path but it’s doesn’t help.

Hello, Ibragim

Please note that you can use StaticLoadObject function:

StaticLoadObject( UClass* Class, UObject* InOuter, const TCHAR* Name, const TCHAR* Filename, uint32 LoadFlags, UPackageMap* Sandbox, bool bAllowObjectReconciliation)

If you like to learn more about Dynamic Object Loading in Unreal Engine 4, please go here:

Hope this helped!

Have a great day!

Hello,
Can you please give an example of use StaticLoadObject in my case?

I can’t understand how to use StaticLoadObject in my case. I did find any example.

You can add the following to your header file:

FORCEINLINE UDataTable* LoadObjFromPath(const FName& Path)
	{
		if (Path == NAME_None) return NULL;

		return Cast<UDataTable>(StaticLoadObject(UDataTable::StaticClass(), NULL, *Path.ToString()));
	}

(If you need to use templates, you can add this instead of the first version:

template <typename ObjClass>
	FORCEINLINE ObjClass* LoadObjFromPath(const FName& Path)
	{
		if (Path == NAME_None) return NULL;
		
		return Cast<ObjClass>(StaticLoadObject(ObjClass::StaticClass(), NULL, *Path.ToString()));
	}

)

Then please go to the Editor, right-click on the asset in the Content tab and select “Copy Reference”.
Paste the code as function parameter and that’s it:

UDataTable* TestDataTable = LoadObjFromPath<UDataTable>(TEXT("DataTable'/Game/MyDataTable.MyDataTable'"));

Hi Ibragimlvanov,

I concur with 's answer but I would like to add that I have reproduced the crash that you experienced and, even if your implementation is wrong, the editor should avoid crashing in this situation. I’ve reported the crash and for your reference the bug number is UE-15816.

Have a nice day

Thank you, but I can’t compile it.

.H

    #pragma once
    
    #include "StrucGenerator.generated.h"
    
    FORCEINLINE UDataTable* LoadObjFromPath(const FName& Path)
    {
    	if (Path == NAME_None) return NULL;
    
    	return Cast<UDataTable>(StaticLoadObject(UDataTable::StaticClass(), NULL, *Path.ToString()));
    }
    /** Structure to store the lookup of GameObjects for use in a UDataTable */
    USTRUCT(Blueprintable)
    struct FStrucGenerator : public FTableRowBase
    {
    	GENERATED_USTRUCT_BODY()
    
    	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GO")
    		UDataTable* TestDataTable;
    	FStrucGenerator()
    	{
    		UDataTable* TestDataTable = LoadObjFromPath<UDataTable>(TEXT("DataTable'/Game/ThirdPerson/TableLocalGenerator.TableLocalGenerator'"));
    		Option = 1;
    		Position.Set(0, 0);
    	}
    };

1>c:\users\documents\unreal projects\myrunner\source\myrunner\StrucGenerator.h(41): error C2275: ‘UDataTable’ : illegal use of this type as an expression
1> C:\Program Files\Epic Games\4.7\Engine\Source\Runtime\Engine\Classes\Engine/DataTable.h(28) : see declaration of ‘UDataTable’
1>C:\Users\Documents\Unreal Projects\MyRunner\Source\MyRunner\StrucGenerator.h(41): error C2275: ‘UDataTable’ : illegal use of this type as an expression
1> C:\Program Files\Epic Games\4.7\Engine\Source\Runtime\Engine\Classes\Engine/DataTable.h(28) : see declaration of ‘UDataTable’

Hey, did you ever find the solution to get it to compile?

Remove &ltUDataTable&gt if you NOT use the template one.

UDataTable* TestDataTable = LoadObjFromPath(TEXT(“DataTable’/Game/ThirdPerson/TableLocalGenerator.TableLocalGenerator’”));