How get the ULevel Reference?

Hi, in my World I have multiple subLevels and I use a c++ Object to manage them. This is my code:

.h

UCLASS()
class RPGPLATE_API ALM_Level_0_Handler : public AActor
{
	GENERATED_BODY()
	
public:
	ALM_Level_0_Handler();
	~ALM_Level_0_Handler();

	UFUNCTION(BlueprintCallable, Category = "Test")
	void SpawnTest(ULevel* level);

private:

};

.cpp

ALM_Level_0_Handler::ALM_Level_0_Handler(){}

ALM_Level_0_Handler::~ALM_Level_0_Handler(){}


void ALM_Level_0_Handler::SpawnTest(ULevel* level)
{
	FVector Location(0.0f, 0.0f, 0.0f);
	FRotator Rotation(0.0f, 0.0f, 0.0f);
	FActorSpawnParameters SpawnInfo;
	SpawnInfo.OverrideLevel = level;
	GetWorld()->SpawnActor<AMO_LowerItems>(Location, Rotation, SpawnInfo);
}

Now in one of my subLevel blueprint I call the function SpawnTest:

The problem is: I Don’t know how to get the reference to the level where is the node (the blueprint owner)

NOTE : the variable Test is the LM_Level_0_Handler.

Hello.

You can get currently loaded levels from UWorld (UWorld::GetLevels).
You’d have to iterate through these, and somehow figure out which one is the one you want to spawn you actor on.

If you want to be able to have a dropdown list of levels in blueprint, you could use TAssetPtr. It’s important to not use simple reference as it would cause that any blueprint having such node, would load whole level to memory whenever that blueprint would be opened. TAssetPtrs prevent from doing this (among other things).

Also - which is even more important - selecting level in such dropdown, would actually provide reference to a Level ASSET. So you’d still have to iterate through loaded levels of current world and for example compare their names to find the requested one.

Example function using TAssetPtr as param:

void Function(TAssetPtr<UWorld> InLevel);

Hope this helps.

1 Like

Sorry, for my late answer because I work on this projects only the Sunday. Thanks for your help.

No problem. And you’re welcome!

Happy coding. :stuck_out_tongue: