Announcement

Collapse
No announcement yet.

Using namespaces with UCLASS

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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:

    https://docs.unrealengine.com/latest...tml#namespaces

    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:

    Code:
    //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 :|

    Best regards,
    Temaran
    Some of my projects related to UE4:

    https://www.unrealengine.com/marketp...emaran-widgets
    https://github.com/Temaran/UE4Webcamera
    https://github.com/Temaran/UE4RenderDocPlugin
    https://github.com/Temaran/UE4ShaderPluginDemo
    https://github.com/Temaran/EyeXforUE4 (Forked from Tobii. It was moved there from my github though )

    #2
    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.
    Last edited by enlight_2014; 05-12-2014, 07:52 AM.
    Contact: enlight in #unrealengine IRC channel on Freenode, or @macagonator on Twitter

    Comment


      #3
      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
      Some of my projects related to UE4:

      https://www.unrealengine.com/marketp...emaran-widgets
      https://github.com/Temaran/UE4Webcamera
      https://github.com/Temaran/UE4RenderDocPlugin
      https://github.com/Temaran/UE4ShaderPluginDemo
      https://github.com/Temaran/EyeXforUE4 (Forked from Tobii. It was moved there from my github though )

      Comment


        #4
        I figured namespaces didn't work so I didn't try it. What a shame though
        Godz for UT '99 / UT 2003

        Comment


          #5
          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
          Gerke Max Preussner | UE4 Programming Consultant

          Logo
          Follow me on Github | Goodreads | Linkedin | Pinterest | SlideShare | Twitter.
          Chat with me as gmpreussner on Discord.

          Comment

          Working...
          X