Consider a situation, when we have two persons with the same name, Zara, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area if they live in different area or their mother or father name, etc.

Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope. (Ref.


Let’s start by creating a C++ class based on Actor. After your C++ has done compiling we can create our namespace. Namespaces are created after your main scope. You know scope end when you see };. We create simple printing function.

// Our new namespace.
namespace MyNewSpace {
  void printSomething();

We declare it in .CPP. Notice that we don’t you class name anymore, but our namespace name.

void MyNewSpace::printSomething()
  UE_LOG(LogTemp, Warning, TEXT("Printing inside namespace"));

Inside our class BeginPlay we can run the function.


It is simple as that. Nothing more is needed to do to call basic functions. If we want to set variables inside our namespace, we need to set them static. So, inside our namespace we could create FString.

static FString StaticStringInsideNamespace;

Now we can set/get that same way we would call function.

MyNewSpace::StaticStringInsideNamespace = FString("Some new FString");

Last thing to know about is how to get reference to your class variables. When we are deling with namespaces, we lost all the references, because we are in the different scope. To get hold to our class, we could simply pass it as parameter. Let’s create a function inside our namespace.

void ClassReferenceFunction(AC_MyActor* MySelf);

When declare it and get the reference, we have control to all our variables and functions.

void MyNewSpace::ClassReferenceFunction(AC_MyActor* MySelf)


Now, we just need to call it somewhere. We do that in BeginPlay. We pass this as variable. This means us. The object of our class.


That’s it. Have fun creating namespaces and organizing your code a bit more :)!







Sähköpostiosoitettasi ei julkaista. Pakolliset kentät on merkitty *