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:
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.
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’?
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.
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.