x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: Not Selected
Tags:
more ▼

asked Nov 28 '14 at 03:33 AM in C++ Programming

avatar image

WaveParadigm
18 5 7 10

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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:

alt text

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

https://www.youtube.com/playlist?list=PLZlv_N0_O1gb5xvsc7VM7pfoRAKLuIcFi

more ▼

answered Nov 28 '14 at 08:28 AM

avatar image

eXi
6.8k 333 185 470

avatar image WaveParadigm Dec 01 '14 at 06:46 AM

Thank you so much!

avatar image WaveParadigm Jan 05 '15 at 04:20 AM

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?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question