I am using UE 4.10.2, with the editor prebuilt for windows, with VS 2015, and Windows 10 as my OS.
The UE4 Programming Guide’s Coding Standards section on Namespaces (Epic C++ Coding Standard for Unreal Engine | Unreal Engine 5.2 Documentation) states that namespaces may be used to organize classes, functions, variables, with a few caveats.
These caveats don’t seem to stipulate that you can’t use UCLASS or USTRUCT definitions for your classes defined inside a namespace. They even require a namespace for enums.
However, when I attempt to compile a project in UE 4.10.2, on windows with a class defined using UCLASS as follows:
MyClass.h:
// MyClass.h
#pragma once
#include "Actor.h"
#include "MyClass.generated.h"
namespace MyNamespace {
UCLASS()
class MYPROJECT_API AMyClass : public AActor
{
GENERATED_BODY()
public:
AMyClass();
};
} // namespace MyNamespace
MyClasscpp:
// MyClass.cpp
#include "MyProject.h"
#include "MyClass.h"
namespace MyNamespace {
AMyClass::AMyClass()
{}
} // namespace Mynamespace
I get the following compile error:
CompilerResultsLog:Error: Error c:\users\nick\MyProject\source\MyProject\MyClass.h(4) : fatal error C1083: Cannot open include file: 'MyClass.generated.h': No such file or directory
I also attempted to compile with the “MyClass.generated.h” include inside the namespace (and no changes to the cpp), like so:
MyClass.h:
// MyClass.h
#pragma once
#include "Actor.h"
namespace MyNamespace {
#include "MyClass.generated.h"
UCLASS()
class MYPROJECT_API AMyClass : public AActor
{
GENERATED_BODY()
public:
AMyClass();
};
} // namespace MyNamespace
But then I got these errors:
CompilerResultsLog:Error: Error C:/Users/Nick/MyProject/Source/Stratagem/MyProject.h(38) : Expected an include at the top of the header: '#include "MyProject.generated.h"'
CompilerResultsLog:Error: Error Error: Failed to generate code for MyProjectEditor - error code: OtherCompilationError (5)
I don’t see any other reasonable way to arrange the includes and namespace (although I did try a few unreasonable ones, with no luck).
I found this forum thread: Using namespaces with UCLASS - C++ - Epic Developer Community Forums
The responses, from 2014, indicate that UCLASS/USTRUCT was most likely not supported with Unreal Build Tool at that time.
It appears to me that namespaces with UCLASS/USTRUCT still simply aren’t supported by Unreal Build Tool - is the documentation just incorrect/misleading or am I just missing something necessary to make this work? I fail to see how namespace could be said to “work” with UE if they can’t be used with UBT classes…