UCLASS definition inside a project plugin - how to make it work?

I have a project that, besides the typical Source subfolder, has a Plugins subfolder. When using the ‘Add Code’ wizard inside the UE Editor to add a new class (e.g. derived from UAssetUserData) to my plugin, it won’t let me create the class inside the Plugins folder but insists that all source code for my project must exist within the Source folder (displaying an error message).

Is there a way to create a UCLASS inside my Plugins folder, possibly manually without using the wizard? I assume what I need to do is to somehow tell the UnrealHeaderTool to parse my new class.

Thanks!

1 Like

You can still use the wizard to create the files, then just move them into the Plugins folder (I’m assuming your plugin has its own folder/module structure already set up).
After that you just right-click on your .uproject file and click “Generate Visual Studio project files”.

Yeah, the unreal build tool should be able to find and parse all of your source files located within your source folder, even if they’re located within subfolders. I have loads of subfolders and don’t have any issues.

Just verify that the plugin source files you think you have a directory path within the source folder and not something else like an intermediate folder. Sometimes UE4 will want to put source files in there and thats wrong.

Also, it is 100% possible to manually create your own classes without using the editor to insert them. I actually prefer to do this myself. There are a few gotchas you have to pay attention to. I wrote up a quick workflow for doing this correctly. Here is my own documentation on how all this stuff works:

Building a UE4 project: What is happening behind the scenes?!

  1. Within the game editor, you add a class to the project and then you open it up in visual studio.
    1.0: File -> Add code to project
    1.0.1: Name the file
    1.0.2: IMPORTANT: Set the folder to the correct source file location! By default, this goes into an intermediate folder.
    1.1: If the file is brand new, it hasn’t been added to version control yet (right click -> Add).

  2. Notice that at the top of the header file is an #include line which says #include “MyFile.generated.h”]. This is a header file which doesn’t exist until it is built by the unreal build tool (UBT). The location of this generated header file is going to be within the intermediate folder. The generated header file is created every time you build the header file, so don’t modify it manually because your changes will get overwritten.

  3. When you build the project, here is what happens:
    3.0: A project build has been initialized
    3.1: The unreal build tool (UBT) will parse all of your header files and look for and expand UE4 specific macros (such as UPROPERTY(), UCLASS(), UENUM, GENERATED_BODY(), etc). UBT doesn’t care about whether or not a file is a part of your solution, it will use every file it finds to build your project. Based on the properties you set for each macro, the generated header file will reflect these values. If there are any errors, the visual studio output log will display the UE4 build errors. This is what the unreal team calls “Reflection”, which shares similarities to C# reflection.
    3.2: Once the UE4 generated file build completes, visual studio will build the binary executable files and launch & load them within the game editor.

  4. If you are using source control, such as perforce:
    4.0: If the file is brand new to the project, you’ll just want to use p4v to add the file to version control.
    4.1: If the file has been modified, you’ll just want to push the changes.
    4.2: If you want to move the file to another folder, here’s where it gets tricky! You have to use PERFORCE to move the file (NOT windows explorer). Next, you will probably want to update the visual studio solution explorer to reflect the updated file location. The one-file solution is to just remove the file from the solution and then re-add it using “Add existing file to solution”. The large, multi-file solution is to delete the project file and let the engine rebuild it by launching the editor and inserting a temporary class and opening it up in the editor (there’s gotta be a better way to do this…).

  5. If you are manually creating a header file from scratch, the UBT has some picky requirements on formatting:
    -The class must inherit from UObject or a derived class.
    -The class name must be prefixed with the capital letter of the object you’re inheriting from, so “AMyClass : public AActor”, or “UMyClass : public UObject”
    -You must include an API reference, with the following format: [Projectname]_API between the class keyword and the class name
    -Every class should include “GENERATED_BODY()” as the very first line within the class body
    -Every class should also use UCLASS() as an attribute macro above the class definition
    -The class name should match the name of the header file, minus the prefix
    -you must have #pragma once at the very top of the .h file
    -You must include a link to the generated header file the UBT builds (ie: #include “MyClass.generated.h”)

    So: you should have something like this:
    MyClass.h:

    #pragma once
    #include “MyClass.generate.h”

    UCLASS()
    class [YOURPROJECTNAME]_API UMyClass : public UObject
    {
    GENERATED_BODY()
    };

    Note: If your file doesn’t actually use any of the UE4 macros, the UBT will not create a generated .h file. This will cause your build to fail.

1 Like

Thanks so much for the fast replies! This is very helpful however does not solve my problem as my Plugins folder is not inside the Source folder - it a sibling folder of the Source folder. Therefore - it seems - the UnrealHeaderTool does not find the source files contained in it and does not generate the ‘generated’ header file for it.

Is there a way to point the UnrealHeaderTool to the Plugins folder so it’s parsing it as well as the Source folder?

Or, can I make the plugin it’s own module or will that open a whole other can of worms?

Thanks in advance!

My last post had the assumption that your plugin was setup as described in these links:
https://docs.unrealengine.com/latest/INT/Programming/Plugins/index.html
https://wiki.unrealengine.com/An_Introduction_to_UE4_Plugins

i.e. setup as it’s own module, with it’s own source folder and Build.cs

I don’t think so, all source should be within the Source folder. If you want to keep the plugin source in a different repository to the project source you could use a Git submodule, or create a directory junction (or the OSX/Linux equivalent) in the project Source directory.

Plugins are already made up of modules, so not sure what you mean by that.