How do I get the name of the current level?

So I haven’t seen a good answer to this question on the site. One answer showed that in C++ you could use GetWorld()->getMapName(). Could someone explain to me, like you would to someone who’s never used C++, how I would go about making a C++ class with a Blueprint Callable function that returns the level name? Thank you so much.

Hey man,

i really don’t know how much C++ you know, so i will keep it to a very basic setup. If you never used C++, please watch the Epic Games Youtube Tutorial Series on C++. This helps you getting started.

So back to your problem. I guess you want to create a function inside one of your classes and let a Blueprint derive from this class. Inside your Blueprint you want to be able to call your function and get the name of the CurrentLevel. (Sidenote: I don’t know if there isn’t already a function in BP with which you could get the Name).

So you might click on “Edit” and “Add Code to Project”. Now you would need select the BaseClass. Let’s say we create something from “AActor”, because you want to place your BP inside the Scene later on. You could also create something from Character ect, but please watch the tutorial if you don’t understand what i mean.

Now after you named your Class and it opened up inside Visual Studio, you have 2 Files for your class.

In my case it is “MyTestActor”. So inside the class it is reference with “AMyTestActor” because of the AActor base Class. So inside your “MyTestActor.h” (header file) you will want to define your function first. Later you will go to your “MyTestActor.cpp” and fill the function with something to do.

So here is the MyTestActor.h file:

#pragma once

#include "GameFramework/Actor.h"
#include "MyTestActor.generated.h"

/**
 * 
 */
UCLASS()
class PLAYGROUND_API AMyTestActor : public AActor
{
	GENERATED_UCLASS_BODY()

	UFUNCTION(BlueprintCallable, Category = "TestFunctions")
	FString GetCurrentMapName();
	
};

I only added:

UFUNCTION(BlueprintCallable, Category = "TestFunctions")
	FString GetCurrentMapName();

To it. The rest is default. What this does is define a function with the name “GetCurrentMapName” and a return value of “FString” because we want to return the name later. To make this function BlueprintCallable, you need to use

UFUNCTION(BlueprintCallable, Category = "TestFunctions")

Above the function. This will also be explained in the Tutorial Series. Every BlueprintCallable function needs a Category!

So now we need to go to our MyTestActor.cpp file and fill the function with something.

#include "Playground.h"
#include "MyTestActor.h"


AMyTestActor::AMyTestActor(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{

}


FString AMyTestActor::GetCurrentMapName()

{
	UWorld* MyWorld = GetWorld();
	FString CurrentMapName = MyWorld->GetMapName();	

	return CurrentMapName;

}

I only added:

FString AMyTestActor::GetCurrentMapName()

{
	UWorld* MyWorld = GetWorld();
	FString CurrentMapName = MyWorld->GetMapName();	

	return CurrentMapName;

}

So i get MyWorld with “GetWorld()” and save it in the Pointer “MyWorld”. After that, i create an FString “CurrentMapName”, that i later return, and fill it with the MapName by using “MyWorld->GetMapName();”.
You could also do this in one Step by just using:

return GetWorld()->GetMapName();

But doing it in some more steps will make it easy to reuse your Name inside that function as well as reusing the “MyWorld” and it will help you learning c++ a bit more.

Now you would compile the game again and create a Blueprint from this custom class. Inside the Custom class you now can call the function by just searching it in the contest menu and typing its name:

http://puu.sh/d8xuj/125ca44fe9.png

If you run into problems i really want you to first learn some basics with the tutorial series on Youtube.

3 Likes

Thank you so much!

So I did this, and it works perfectly in the editor, but I just tried to export and now the function isn’t working at all. Any ideas?