Unresolved External with CreateInstance (from UGameplayStatics)

When compiling the following code

	ULevelStreaming* NewLevel = UGameplayStatics::GetStreamingLevel(GetWorld(), TargetLevel);
	NewLevel = NewLevel->CreateInstance(UniqueName);
	NewLevel->bShouldBeLoaded = true;
	NewLevel->bShouldBeVisible = true;
	NewLevel->LevelTransform = FTransform(
		FQuat(0.f, 0.f, 0.f, 0.f),
		FVector(0.f, 0.f, 0.f),
		FVector(0.f, 0.f, 0.f)
	);

I get the following errors:

error LNK1120: 1 unresolved externals
error LNK2019: unresolved external symbol “public: class ULevelStreaming * __cdecl ULevelStreaming::CreateInstance(class FString)” (?CreateInstance@ULevelStreaming@@QEAAPEAV1@VFString@@@Z) referenced in function “public: void __cdecl ADungeonLevelBlueprint::LoadLevel(class FName,class FString)” (?LoadLevel@ADungeonLevelBlueprint@@QEAAXVFName@@VFString@@@Z)

When i comment out NewLevel = NewLevel->CreateInstance(UniqueName); everything compiles fine.

I tried including #include “Engine/LevelStreaming.h” but that didn’t change anything.
Is this something that is going wrong on my end or is this be an issue with unreal?

Hi, if you need ULevelStreaming add ENGINE_API

class ENGINE_API ULevelStreaming : public UObject

and remove ENGINE_API from other methods;

Regards

Pierdek

Hey Pierdek,

I’m pretty new to Unreal and c++ programming but do i need to edit the Levelstreaming.h file?

When i add ENGINE_API and remove the ENGINE_API before the functions of ULevelstreaming I get following error

error : In LevelStreaming: MinimalAPI cannot be specified when the class is fully exported using a MODULENAME_API macro

Then when i remove MinimalAPI from the UCLASS macro

UCLASS(abstract, editinlinenew)
class ENGINE_API ULevelStreaming : public UObject

I get the following error:
error C2487: ‘GetPrivateStaticClass’ : member of dll interface class may not be declared with dll interface

I tried adding ENGINE_API before create instance because i can call the GetWorldAssetPackageFName() but that still resolved in unresolved externals.

ENGINE_API ULevelStreaming* CreateInstance(FString UniqueInstanceName);

Ok, if that doesn’t help you can always add your functionality to the UGameplayStatics;

So i managed to create a workaround for my specific problem but I have no idea if this will work as expected (We decided to take a different approach is our game).

I copied the code from ULevelStreaming::CreateInstance and adapted it to my own class that needed it (in my case it inherits from ALevelScriptActor).

Below is the implementation.

ULevelStreaming* ADungeonLevelBlueprint::CreateLevelInstance(ULevelStreaming* level, FString& InstanceUniqueName)
{
	ULevelStreaming* StreamingLevelInstance = nullptr;

	UWorld* InWorld = level->GetWorld();
	if (InWorld)
	{
		// Create instance long package name
		FString InstanceShortPackageName = InWorld->StreamingLevelsPrefix + FPackageName::GetShortName(InstanceUniqueName);
		FString InstancePackagePath = FPackageName::GetLongPackagePath(level->GetWorldAssetPackageName()) + TEXT("/");
		FName InstanceUniquePackageName = FName(*(InstancePackagePath + InstanceShortPackageName));

		// check if instance name is unique among existing streaming level objects
		const bool bUniqueName = (InWorld->StreamingLevels.FindMatch(ULevelStreaming::FPackageNameMatcher(InstanceUniquePackageName)) == INDEX_NONE);

		if (bUniqueName)
		{
			StreamingLevelInstance = Cast<ULevelStreaming>(StaticConstructObject(level->GetClass(), InWorld, NAME_None, RF_Transient, NULL));
			// new level streaming instance will load the same map package as this object
			StreamingLevelInstance->PackageNameToLoad = (level->PackageNameToLoad == NAME_None ? level->GetWorldAssetPackageFName() : level->PackageNameToLoad);
			// under a provided unique name
			StreamingLevelInstance->SetWorldAssetByPackageName(InstanceUniquePackageName);
			StreamingLevelInstance->bShouldBeLoaded = false;
			StreamingLevelInstance->bShouldBeVisible = false;
			StreamingLevelInstance->LevelTransform = level->LevelTransform;

			// add a new instance to streaming level list
			InWorld->StreamingLevels.Add(StreamingLevelInstance);
		}
		else
		{
			UE_LOG(LogStreaming, Warning, TEXT("Provided streaming level instance name is not unique: %s"), *InstanceUniquePackageName.ToString());
		}
	}

	return StreamingLevelInstance;
}
UCLASS(abstract, editinlinenew)
class ULevelStreaming : public UObject

You can remove ENGINE_API fixed MinimalAPI issuse