UFUNCTION macro is causing compile errors

I am trying to build a class that has UActorComponent as its parent. Whenever I use the UFUNCTION macro within the header file, the class will not build properly… it crashes while running the UnrealHeaderTool.
If I comment out the UFUNCTION line, I have no problems… But whenever I leave it in, I get the following errors:

  1. Error 1 error code: OtherCompilationError (5) C:\Users\\Documents\Unreal Projects\MyAIProject\Intermediate\ProjectFiles\Error MyAIProject
  2. Error 2 error MSB3073: The command ""C:\Program Files\Epic Games\4.9\Engine\Build\BatchFiles\Build.bat" MyAIProjectEditor Win64 Development "C:\Users\\Documents\Unreal Projects\MyAIProject\MyAIProject.uproject" -rocket -waitmutex" exited with code -1. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets 38 5 MyAIProject

Here is a sample of the header file:

#pragma once

#include "Components/ActorComponent.h"
#include "InventorySlot.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class MYAIPROJECT_API UInventorySlot : public UActorComponent
{
	GENERATED_BODY()

protected:

	AActor* Item;

	int StackSize;


public:

	UInventorySlot();

	// Called when the game starts
	virtual void BeginPlay() override;

	UFUNCTION(BlueprintCallable, Category = InventorySlot)
		UInventorySlot Copy();
	
	//UFUNCTION(BlueprintCallable, Category = InventorySlot)
		AActor* GetItem();
	
	//UFUNCTION(BlueprintCallable, Category = InventorySlot)
		int GetStackSize();
	
	//Sets the item to the new Actor value and sets the StackSize to the given value (forces a minimum value of 1) or 0 if NewItem is null
	//UFUNCTION(BlueprintCallable, Category = InventorySlot)
		void SetItem(AActor* NewItem, int NewStackSize);
	
	//UFUNCTION(BlueprintCallable, Category = InventorySlot)
		void SetStackSize(int NewStackSize = 1);
	
	//UFUNCTION(BlueprintCallable, Category = InventorySlot)
		bool IsFull(AActor* CompItem);
	
	////Checs the slot to see if it is empty. returns true if it is
	//UFUNCTION(BlueprintCallable, Category = InventorySlot)
		bool IsEmpty();
	
	////Adds an item to the slot and increments the stack size. Returns the new StackSize value
	//UFUNCTION(BlueprintCallable, Category = InventorySlot)
		int AddItem(AActor* NewItem);
	
	////Removes an item from the slot and updates the stack size
	//UFUNCTION(BlueprintCallable, Category = InventorySlot)
		AActor* RemoveItem();

};

As I said above, It works fine when the UPROPERTY lines are all commented out. But, if I leave even ONE of them un-commented, then it breaks.

I have looked for solutions in the forums, but every one I find has some slightly different problem or reason why it broke. And none of those solutions seem to fix it.
I even tried starting the whole bloody thing over from scratch, thinking that I had somehow corrupted the build or something. But, nothing seems to work.

Does anyone know what is going on here?!

Thanks in advance

Just as an FYI, I tried adding a UPROPERTY integer variable to the code, just to see if it caused a problem… and it caused the same issue as the UFUNCTION macro.

sample:
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = “InventorySlot”)
int testInt;

After further poking around, I found that the return type of the function seems to affect whether or not it throws an error. Any function with a return type of ‘void’, ‘int’, or the class that I am declaring will not accept a UFUNCTION macro. But those that return a type of ‘bool’ or ‘AActor*’ seem to work just fine.

I can maybe understand why the function which returns the class that is being declared might have a problem due to recursion and whatnot (although it compiles fine without the macro)… But why not ‘void’ or ‘int’?

Did you actually define them in the source file? If you didn’t it’s normal to be getting LNK errors (unresolved externals)

You shouldn’t use ‘int’ in UPROPERTY()
And just FYI, unsigned can’t hold negative values, while signed can

Click “Output” tab and see exact error, it should be above OtherCompilerError. Don’t trust VS error list, it really buggy with UE4 code tools

1>------ Build started: Project:
MyAIProject, Configuration:
Development_Editor x64 ------ 1>
Parsing headers for MyAIProjectEditor
1> Running UnrealHeaderTool
“C:\Users\Documents\Unreal
Projects\MyAIProject\MyAIProject.uproject”
“C:\Users\Documents\Unreal
Projects\MyAIProject\Intermediate\Build\Win64\MyAIProjectEditor\Development\UnrealHeaderTool.manifest”
-LogCmds=“loginit warning, logexit warning, logdatabase error” -rocket
-installed 1> C:/Users//Documents/Unreal
Projects/MyAIProject/Source/MyAIProject/InventorySlot.h(42)
: Unrecognized type ‘int’ - type must
be a UCLASS, USTRUCT or UENUM 1>Error
: Failed to generate code for
MyAIProjectEditor - error code:
OtherCompilationError (5) 1>
UnrealHeaderTool failed for target
‘MyAIProjectEditor’ (platform: Win64,
module info:
C:\Users\Documents\Unreal
Projects\MyAIProject\Intermediate\Build\Win64\MyAIProjectEditor\Development\UnrealHeaderTool.manifest).
1>C:\Program Files
(x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets(38,5):
error MSB3073: The command
““C:\Program Files\Epic
Games\4.9\Engine\Build\BatchFiles\Build.bat” MyAIProjectEditor Win64 Development
“C:\Users\Documents\Unreal
Projects\MyAIProject\MyAIProject.uproject”
-rocket -waitmutex” exited with code -1.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped

Above is the “build” output… The “debug” output doesn’t have anything in it.

It says " Unrecognized type ‘int’ - type must be a UCLASS, USTRUCT or UENUM"…
Why is ‘int’ being called an ‘Unrecognized type’?

OK. So, with this revelation, I tried removing any ‘int’ from the function declarations. This worked! So, it was not the return type that was causing the problem after all. And the ‘void’ doesn’t seem to cause a problem either.

However, it seems that any time there is an ‘int’ located in the function declaration, it breaks when I use the UFUNCTION (or UPROPERTY) macro.

I tested this issue with several different native c++ types and found that it works (i.e does NOT break) with float, and bool.
But, it DOES break with char, int, long, short, or double.

Hello Brunke76,

When using UE4 reflection, you need to declare if it is an int8, int16, int32 or int64 instead of just simply int.

If you are intending to make the UPROPERTY usable by blueprints (BlueprintReadWrite, BlueprintReadOnly, etc) you’ll need to use int32 as the other bit sizes are not supported by blueprint.

Sorry … Only just now saw this comment… I now see that this is probably the issue. Would explain the messages that I have been getting (mentioned below).

Has this always been the case? I could SWEAR that I have used ‘int’ in previous versions… Or, have I just gotten lucky all of this time and not actually used them in UFUNCTION declaration or UPROPERTY?

I think that this officially solves my issue! (since it fully explains the results that I have been getting. Both, with things breaking and NOT breaking).

So, how do I give you credit for the solution? Do you need to promote your comment to an ‘Answer’ instead if a comment?

Just as a side note… Apparently, int8, int16, in32, and int64 (as well as their unsigned counterparts) are valid for as UPROPERTY… But, UFUNCTION ONLY seems to support int32 (and not uint32 either). which is a little bit of a pain. But, whatever.

That’s because UE4 uses C++ default(native) float and bool but uses custom int(int8, 16, 32, 64 || uint 8, 16, 32, 64) and String(FString, FText, FName)

Also, bool can be declared by

UPROPERTY()
/** Desc */
uint32 bSomeBool;

Happy coding :smiley:

And enums only support uint8 as of now

UENUM()
enum class ESomeEnum : uint8
{
Enumerator0,
Enumerator1

// ...
}

I know this cuz when i tried uint32 in EPlayerRank, compiler told me :smiley: