Download

Using namespaces with UCLASS

Hello again everyone!

I’ve been refactoring my code, adjusting to the UE4 coding standard after doing some initial experimenting, but looking at my project right now, I can’t help but being worried about collisions in the global namespace. I started out simply prefixing my classes with ‘my’ after the Class-id capital (A, U, S, etc) but I quickly started disliking it since it breaks from the UE standard.

I would really like to be able to put all my own types in a base namespace to avoid the problem all together, and it seems like it would work reading this part:

However, I’ve been unable to get this to work on UCLASS files. It works fine with my custom math and algorithm code obviously, but if I try to do anything in the line of:



//Header
#pragma once

#include "GameFramework/GameState.h"
#include "StandardGameState.generated.h"

namespace MyProject
{
   UCLASS()
   class AStandardGameState : public AGameState
   {
   	GENERATED_UCLASS_BODY()

	/** World bounds for camera movement. */
	FBox WorldBounds;
   };
}

//Source
#include "MyProject.h"

namespace MyProject
{
   AStandardGameState::AStandardGameState(const class FPostConstructInitializeProperties& PCIP)
	   : Super(PCIP)
   {
        WorldBounds = FBox();
   }
}



As well as wrapping all the functions in the implementation file in the same ns just does not seem to work.
I get all kinds of compilation errors. I am by no means a C++ expert, so I’m suspecting that it might not even be a UE4 specific problem, but rather my lack of understanding of C++, but any insight anyone might have on why this isn’t working would be welcome no matter what the cause is.

My guess is that it’s related to the expansions of the UCLASS and GENERATED_UCLASS_BODY macros.
But I really don’t know :expressionless:

Best regards,
Temaran

The Unreal Header Tool (UHT) doesn’t work with namespaces at the moment, I don’t know if it’s something Epic is planning on fixing, if they don’t hopefully someone else will. I sure would love to see it working.

Edit: I should clarify that I’m talking about putting UCLASS()/USTRUCT() in namespaces, there’s nothing stopping you from putting regular “undecorated” C++ classes and structs in namespaces.

That explains it! Thank you for the insight.
I don’t have any collisions yet, so I guess I’ll have make do until it’s possible.

Best regards,
Temaran

I figured namespaces didn’t work so I didn’t try it. What a shame though

Decorated namespaces are only supported with UENUM at the moment. Whether support for UCLASS and USTRUCT is planned, I cannot say. If you really need to know, check on AnswerHub, thanks :slight_smile: