UnrealHeaderTool fails when UFUNCTION is declared

Hi,

Whenever I try to add a function* with UFUNCTION to my code I get a not very helpful error message:

’ * (note that function != method in this context, as in, a function NOT inside a class/struct)

1>------ Build started: Project: PowerTD, Configuration: Development_Editor x64 ------
2>------ Skipped Build: Project: UE4, Configuration: BuiltWithUnrealBuildTool Win32 ------
2>Project not selected to build for this solution configuration 
1>  Parsing headers for PowerTDEditor
1>  D:/libs/ue4/PowerTD/Source/PowerTD/MapLoader.h(10) : LogWindows:Error: Windows GetLastError: The operation completed successfully. (0)
1>Error : Failed to generate code for PowerTDEditor - error code: CrashOrAssert (3)
1>  UnrealHeaderTool failed for target 'PowerTDEditor' (platform: Win64, module info: D:\libs\ue4\PowerTD\Intermediate\Build\Win64\PowerTDEditor\Development\UnrealHeaderTool.manifest).
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets(38,5): error MSB3073: The command ""D:\ProgramFiles\Epic Games\4.8\Engine\Build\BatchFiles\Build.bat" PowerTDEditor Win64 Development "D:\libs\ue4\PowerTD\PowerTD.uproject" -rocket" exited with code -1.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 1 skipped ==========

I investigate the command, and tried to run the “UnrealHeaderTool” with verbose mode to see whats going on, but the error message still unhelpful:

D:\ProgramFiles\Epic Games\4.8\Engine\Binaries\Win64>UnrealHeaderTool.exe "D:\libs\ue4\PowerTD\PowerTD.uproject" "D:\libs\ue4\PowerTD\Intermediate\Build\Win64\PowerTDEditor\Development\UnrealHeaderTool.manifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -rocket -installed -verbose
LogCompile:Error: Exporting native class declarations for /Script/CoreUObject
LogCompile:Error: Exporting native class declarations for /Script/InputCore
LogCompile:Error: Exporting native class declarations for /Script/SlateCore
LogCompile:Error: Exporting native class declarations for /Script/Slate
LogCompile:Error: Exporting native class declarations for /Script/EngineMessages
LogCompile:Error: Exporting native class declarations for /Script/EngineSettings
LogCompile:Error: Exporting native class declarations for /Script/Engine
D:/libs/ue4/PowerTD/Source/PowerTD/MapLoader.h(25) : LogWindows:Error: Windows GetLastError: The operation completed successfully. (0)

The function with UFUNCTION I’m trying to add is a very simple one I got from the wiki: A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums

In the “.h” file I have:

UFUNCTION(BlueprintCallable, Category = "Sunshine")
void ActivateSunShine();

and in the “.cpp” file I have:

void ActivateSunShine() {}

Any idea what might be causing this error?

Ps.: I started a brand new blueprint project, generated the C++ files, then created a C++ class based on “UObject” using the UE4Editor. Then tried to add the UFUNCTION above when I got the error message. The project compiles fine without the UFUNCTION.

After some trial and error I figured out that the problem above was because I was trying to create a function. By creating a static method instead, and applying the “UFUNCTION” to it, then I can compile the code successfully and call it from my Blueprints.

So, this works:

UCLASS(BlueprintType)
class MY_API USunshine : public UObject
{
	GENERATED_BODY()
	
public:

    UFUNCTION(BlueprintCallable, Category = "Sunshine")
    static void ActivateSunShine();
};

The above question remains, why do I get that unhelpful error? Also, the Unreal documentation doesn’t seem to mention that I can’t use function and have to use methods inside a class. Am I missing something?

(I’m not marking this answer as correct yet because it doesn’t explain the question above, just a workaround).

Your .h file is good, no worries.
But .cpp is bad. Compiler says (generally) that ActivateSunShine exist, but isn’t declared properly.

void USunshine::ActivateSunShine() {}

That’s how it must be.
Actually, there you saying, that for the class USunshine you declaring body of the function ActivateSunShine. If you use “void ActivateSunShine() {}”, then… it is hard to say what you declaring. As I can remember it will be global static function… Maybe…
No need for static void there.
And just reminder: to accept the answer, click on tick at the left top corner of an answer. It will apear when you hover mouse near this corner. Otherwise question will be unresolved.

Thank you for the answer, but I don’t understand it completely. My answer actually works, I just haven’t marked it as accepted because there is no mention in the Unreal documentation saying that I can’t create functions and have to use methods. I’m waiting to see if someone can explain to me what that unhelpful error is, and why I cant use normal functions with “UFUNCTION”. Will update my answer to explain it.

You CAN use UFUNCTION with non-static functions. It main purpose of UFUNCTION actually.
.H:

UFUNCTION(BlueprintCallable, Category = "Sunshine")
 void ActivateSunShine();

.CPP:

void USunshine::ActivateSunShine() {}

This error does not describe that there is undeclared symbol. There lot of unhelpful information for me too. I just saw your declaration and there was little mistake.
How class works: you have two files: .h and .cpp.
In .h you declaring symbols (functions, methods, variables etc.), but they don’t have body, they empty inside. Why we can’t declare body in .h? We can, but method A cannot use method B, if method B declared below (literally).
In .cpp you determining body of functions, methods, and everything else what is static. Everything but non-static variables must be defined in .cpp. Method can do nothing, but it must have an entry point (there we can say, that method have an address).
Definition of method ActivateSunShine() is empty, but it have an entry point, it will affect stack, it will replace current memory position to self entry point, then return it back, where it was called. But this all is on assembler level…

Oh, I understand now what you are trying to say. I understand that I can create methods with “UFUNCTION”, but, what I’m trying to create is a function NOT inside a class/struct with “UFUNCTION”, and that is causing the error above.

I was having the same issue as you, and with a bit of toying around I think I might have gotten functions to compile how you want them to.

.H:

namespace USunshine {
    UFUNCTION(BlueprintCallable, Category = "Sunshine")
    void ActivateSunShine();
}

.CPP:

    void USunshine::ActivateSunShine() {}

Wrapping the declaration in a namespace seems to make that error go away.

Great, despite it compiling I don’t see my function appearing anywhere in the blueprint editor. When I search for the function name nothing comes up…

I think you aren’t allowed to create functions, your functions need to be part of a UCLASS, or else blueprints won’t recognize what you’re trying to do.

Thank you for taking a look at it, now I know I’m not the only one with this problem. And when using named namespace I got the same result as you. Code compiles fine when I put the function in a named namespace, but can’t call it from blueprints.